Skip to content

Bind the validated request body value in strict Echo, Gin and Iris servers - #2613

Merged
mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/validated-body-frameworks
Oct 23, 2026
Merged

mromaszewicz merged 2 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/validated-body-frameworks

Conversation

@mromaszewicz

@mromaszewicz mromaszewicz commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

Summary

Follow-up to #2605 and #2612 for the strict servers that still decoded the JSON request body a second time through their framework binder. encoding/json matches member names case-insensitively, so behind a validation middleware {"role":"user","ROLE":"admin"} passed validation as role: user and reached the handler as Role: "admin".

When a request validator built on kin-openapi's openapi3filter ran with KeepValidatedValue (the default in echo-middleware v1.2.1, echo-v5-middleware v0.2.1, gin-middleware v1.2.1 and iris-middleware v1.2.1), the strict handler now binds the validated value with runtime.BindJSONValue, matching member names exactly, instead of decoding the body again.

  • Echo / Echo v5: on the DefaultBinder path the validated value is tried first and otherwise BindBody runs exactly as before, so Echo's 400 wrapping of decode errors and its empty-body handling are unchanged. When the application has installed a custom echo.Binder, the handler still defers to it entirely through ctx.Bind, since such a binder may do more than decode (call ctx.Validate, for example); those applications keep their current behaviour and do not get the validated value.
  • Gin: otherwise ctx.ShouldBindJSON runs as before. After a validated bind, binding.Validator.ValidateStruct still runs, as ShouldBindJSON did after decoding; Gin generated code now imports github.com/gin-gonic/gin/binding for that.
  • Iris: otherwise ctx.ReadJSON runs as before. After a validated bind, Application.Validate still runs, as ReadJSON did.

Without a validated value, or when it does not fit the body type, each template falls back to the framework decode, so nothing changes for applications without a validator or with an older middleware.

Fiber and Fiber v3 are not covered. fiber-middleware validates a converted copy of the request, so the validated value is not reachable from the handler until the middleware exposes it (for example via c.Locals). The README tip says so.

Testing

  • New end-to-end test in each of the Echo, Echo v5, Gin and Iris strict packages, running the strict server behind an openapi3filter validator with and without KeepValidatedValue (same shape as the stdhttp test from Bind the request body value kin-openapi validated in strict net/http servers #2612).
  • Regression test in both Echo packages: a custom binder that decodes and then calls ctx.Validate against a rejecting validator still produces 422 and is invoked exactly once, with and without KeepValidatedValue.
  • Existing strict, buffering, response-body and codegen unit tests pass; make generate is clean; golangci-lint reports no issues.

🤖 Generated with Claude Code

…rvers

Follow-up to oapi-codegen#2605 for the remaining frameworks that have the *http.Request
in hand. When a request validator built on kin-openapi's openapi3filter ran
with KeepValidatedValue (the default in echo-middleware v1.2.1,
echo-v5-middleware v0.2.1, gin-middleware v1.2.1 and iris-middleware
v1.2.1), the strict handler binds the validated value with
runtime.BindJSONValue, matching member names exactly, instead of decoding
the body a second time through the framework binder, where encoding/json's
case-insensitive matching let an unvalidated "ROLE" member bind to `role`.

Without a validated value, or when it does not fit the body type, each
template falls back to exactly what it did before (echo.DefaultBinder /
ctx.Bind, ctx.ShouldBindJSON, ctx.ReadJSON), so error handling, HTTP error
wrapping and empty-body behaviour are unchanged. Gin's binding.Validator
and Iris's Application.Validate still run on the bound value, as they did
after the framework decode. Gin generated code now imports
github.com/gin-gonic/gin/binding for that.

Fiber and Fiber v3 are not covered: fiber-middleware validates a converted
copy of the request, so the validated value is not reachable from the
handler until the middleware exposes it.

Adds an end-to-end test per framework running the strict server behind an
openapi3filter validator with and without the option, and extends the
README tip.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner October 23, 2026 07:04
@mromaszewicz mromaszewicz added the bug Something isn't working label Oct 23, 2026
@greptile-apps

greptile-apps Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The PR appears safe to merge; the custom Echo binder issue is fixed.

Summary

Strict Echo, Gin and Iris handlers now bind retained validated JSON values instead of decoding them again.

  • Strict Echo, Gin, and Iris handlers use the JSON value their validator checked.

Reviews (2) · Last reviewed commit: "Keep custom Echo binders in charge of th..." · Reviewed by Greptile

Comment thread pkg/codegen/configuration.go Resolved
Review feedback on oapi-codegen#2613: when an application installs its own echo.Binder,
the strict handler used to defer to it entirely, and the binder may do more
than decode (call ctx.Validate, for example). Using the validated value in
that case skipped those checks whenever BindJSONValue succeeded.

Only take the validated value on the DefaultBinder path, where it replaces
nothing but the JSON deserialization, and keep ctx.Bind for custom binders
exactly as before. Applies to Echo v4 and v5 through the shared template.

Adds a regression test in both Echo packages: a custom binder that decodes
and then calls ctx.Validate against a rejecting validator must still
produce 422, and must be invoked exactly once, with and without
KeepValidatedValue.

Co-Authored-By: Claude Fable 5.1 <[email protected]>
@mromaszewicz
mromaszewicz merged commit dd3569f into oapi-codegen:main Oct 23, 2026
16 checks passed
@mromaszewicz
mromaszewicz deleted the fix/validated-body-frameworks branch October 23, 2026 07:21
@mromaszewicz mromaszewicz added the notable changes Used for release notes to highlight these more highly label Oct 23, 2026
mromaszewicz added a commit that referenced this pull request Oct 23, 2026
…ckport) (#2621)

* Bind the validated request body value in all strict servers

Refs: #2605

Backport of #2612, #2613 and #2620 to release/v2.8.x as one change, applied
by hand to the 2.8.x templates (strict-echo5 and strict-fiber-v3 are still
separate files here) and regenerated in one go.

When a request validator built on kin-openapi's openapi3filter ran with
KeepValidatedValue, the strict handler used to decode the JSON body a second
time through the framework binder. encoding/json matches member names
case-insensitively, so a body such as {"role":"user","ROLE":"admin"}
validated as role=user but bound as Role="admin". Each strict template now
binds the value the validator kept, matching member names exactly:

- net/http, Chi, gorilla/mux: runtime.BindJSONBody(r, &body)
- Echo, Echo v5: runtime.BindJSONValue on the DefaultBinder path only; a
  custom echo.Binder still gets ctx.Bind, since it may do more than decode
- Gin: runtime.BindJSONValue, then binding.Validator.ValidateStruct as
  ShouldBindJSON would; generated Gin code imports gin/binding for that
- Iris: runtime.BindJSONValue, then Application().Validate as ReadJSON would
- Fiber, Fiber v3: the value stored in the Fiber context by fiber-middleware
  v1.3.0 / fiber-v3-middleware v0.3.0 under "x-oapi-codegen-validated-body"
  (the key is duplicated in the generated code so it does not import the
  middleware)

Without a validated value, or when it does not fit the body type, every
template falls back to exactly what it did before, so error handling and
empty-body behaviour are unchanged. Requires runtime v1.8.0, pinned in
internal/test and examples; internal/test also moves to kin-openapi v0.152.0
so the new tests can enable KeepValidatedValue. The root module's
kin-openapi pin is unchanged.

Tests: the validated_body_test.go files from main for stdhttp, echo, echo5,
gin, iris and fiber. There is no strict Fiber v3 test package on this
branch, so that one is covered by the regenerated examples compiling.

The embedded swagger spec blobs change across the regenerated files because
compress/flate output differs between the Go version that produced them and
Go 1.27, which built them here. Extends the README validation-middleware tip.

Co-Authored-By: Claude Fable 5.1 <[email protected]>

* Regenerate boilerplate on Go 1.26

---------

Co-authored-by: Claude Fable 5.1 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working notable changes Used for release notes to highlight these more highly

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant