Skip to content

fix: bind only the request body in echo v5 strict handlers#2444

Merged
mromaszewicz merged 2 commits into
mainfrom
fix/issue-1757
Jul 9, 2026
Merged

fix: bind only the request body in echo v5 strict handlers#2444
mromaszewicz merged 2 commits into
mainfrom
fix/issue-1757

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

The echo v5 strict handler template used ctx.Bind, which binds path values (and, for some methods, query parameters) into the request body struct before binding the body itself, so a path parameter sharing a name with a body field would overwrite it.

Generated code now type-switches on the binder installed on the Echo instance: with the default binder, only the body is bound via the package-level echo.BindBody; a custom binder is deferred to entirely, since the echo.Binder interface does not expose body-only binding.

Strict mode for echo v5 previously had no test coverage, so this also adds internal/test/servers/strict/echo5, mirroring the echo v4 strict server tests against the shared strict-schema spec.

This is a follow-up to PR #2063, which fixed the same problem for echo v4, and extends the fix for issue #1757 to echo v5.

The echo v5 strict handler template used ctx.Bind, which binds path
values (and, for some methods, query parameters) into the request body
struct before binding the body itself, so a path parameter sharing a
name with a body field would overwrite it.

Generated code now type-switches on the binder installed on the Echo
instance: with the default binder, only the body is bound via the
package-level echo.BindBody; a custom binder is deferred to entirely,
since the echo.Binder interface does not expose body-only binding.

Strict mode for echo v5 previously had no test coverage, so this also
adds internal/test/servers/strict/echo5, mirroring the echo v4 strict
server tests against the shared strict-schema spec.

This is a follow-up to PR #2063, which fixed the same problem for
echo v4, and extends the fix for issue #1757 to echo v5.
@mromaszewicz
mromaszewicz requested a review from a team as a code owner July 9, 2026 22:04
@mromaszewicz mromaszewicz added the bug Something isn't working label Jul 9, 2026
@greptile-apps

greptile-apps Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR fixes issue #1757 for the echo v5 strict server backend: ctx.Bind was binding path and query parameters into the JSON request body struct before decoding the body itself, so any path parameter sharing a name with a body field would overwrite it. The fix follows the same approach already applied to echo v4 in PR #2063.

  • Template fix (strict-echo5.tmpl): The JSON body binding now type-switches on the installed binder. With the default *echo.DefaultBinder, only the body is bound via the package-level echo.BindBody; a custom binder is deferred to entirely. The io.EOF guard for optional JSON bodies (missing in the pre-PR template) is also added, bringing parity with the echo v4 template.
  • New test server (echo5/): A full echo v5 strict-server fixture is added, mirroring the existing echo/ layout with configs, generated code, and hand-written implementation — giving echo v5 the test coverage it previously lacked.
  • Schema and regression test: A new /same-name-param-and-body-property/{name} endpoint is added to the shared spec with a sameName schema carrying x-oapi-codegen-extra-tags: param: name; the shared testImpl gains a targeted sub-test that asserts the path parameter value is not copied into the body field, exercised across all tested backends.

Confidence Score: 5/5

Safe to merge — the change is narrowly scoped to the echo v5 strict template and adds test infrastructure; no existing generated API surfaces are altered.

The template change is a direct, well-understood parallel to the already-merged echo v4 fix. The io.EOF guard for optional bodies is present, all backends implement the new endpoint, the regression test correctly targets the overwrite scenario, and generated file diffs are proportional to the schema addition.

No files require special attention.

Important Files Changed

Filename Overview
pkg/codegen/templates/strict/strict-echo5.tmpl Core fix: replaces ctx.Bind with echo.BindBody (package-level, body-only) for the DefaultBinder path, and adds the io.EOF guard for optional JSON bodies — aligns with the echo v4 template's approach
internal/test/servers/strict/strict-schema.yaml Adds /same-name-param-and-body-property/{name} endpoint with a sameName schema that carries a param:"name" extra tag — exactly the construction that triggers the overwrite bug under echo's full Bind
internal/test/servers/strict/strict_test.go Adds TestEcho5Server wiring and a SameNameParamAndBodyProperty regression sub-test (shared across all backends via testImpl) verifying that path-param "path-value" is not copied into the body's Name field
internal/test/servers/strict/echo5/server.gen.go New generated file for the echo v5 strict test server; SameNameParamAndBodyProperty correctly uses echo.BindBody + io.EOF guard for the optional body; proportional to the schema addition
internal/test/servers/strict/echo5/server.go New echo v5 strict-server test implementation; mirrors other backends' server.go; SameNameParamAndBodyProperty echoes the body back, correctly returning an empty response when Body is nil
internal/test/servers/strict/chi/server.gen.go Regenerated to add SameNameParamAndBodyProperty; change is proportional and consistent with the spec addition, no unrelated drift detected
internal/test/servers/strict/client/client.gen.go Adds SameName type and SameNameParamAndBodyProperty client methods; change is additive and consistent with the new schema endpoint

Reviews (2): Last reviewed commit: "fix: bring echo v5 optional-body handlin..." | Re-trigger Greptile

Comment thread pkg/codegen/templates/strict/strict-echo5.tmpl
Comment thread internal/test/servers/strict/strict_test.go
…add issue #1757 regression test

Two review follow-ups:

Add the optional-body guard from the echo v4 strict template to the
echo v5 template, so a binding error caused by an empty body
(errors.Is(err, io.EOF), which unwraps through echo v5's
ErrBadRequest.Wrap) is tolerated when the request body is not required,
leaving the body pointer nil instead of failing the request.

Add a targeted regression test for issue #1757 to the shared strict
server test suite: a new endpoint whose path parameter shares its name
with a request body property, exercised by testImpl across every
framework. The body property carries a param tag via
x-oapi-codegen-extra-tags, which is what makes Echo's full Bind copy a
same-named path parameter into the body struct — Echo's binder skips
fields without param/query/form tags, so plain generated structs need
an extra tag to be affected.
@mromaszewicz
mromaszewicz merged commit e643336 into main Jul 9, 2026
28 checks passed
@mromaszewicz
mromaszewicz deleted the fix/issue-1757 branch July 9, 2026 22:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant