Repository navigation
Bind the validated request body value in strict Echo, Gin and Iris servers - #2613
Merged
mromaszewicz merged 2 commits intoOct 23, 2026
Merged
Conversation
…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]>
Contributor
[Medium impact] The PR appears safe to merge; the custom Echo binder issue is fixed. SummaryStrict Echo, Gin and Iris handlers now bind retained validated JSON values instead of decoding them again.
Reviews (2) · Last reviewed commit: "Keep custom Echo binders in charge of th..." · Reviewed by Greptile |
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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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/jsonmatches member names case-insensitively, so behind a validation middleware{"role":"user","ROLE":"admin"}passed validation asrole: userand reached the handler asRole: "admin".When a request validator built on kin-openapi's
openapi3filterran withKeepValidatedValue(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 withruntime.BindJSONValue, matching member names exactly, instead of decoding the body again.DefaultBinderpath the validated value is tried first and otherwiseBindBodyruns exactly as before, so Echo's 400 wrapping of decode errors and its empty-body handling are unchanged. When the application has installed a customecho.Binder, the handler still defers to it entirely throughctx.Bind, since such a binder may do more than decode (callctx.Validate, for example); those applications keep their current behaviour and do not get the validated value.ctx.ShouldBindJSONruns as before. After a validated bind,binding.Validator.ValidateStructstill runs, asShouldBindJSONdid after decoding; Gin generated code now importsgithub.com/gin-gonic/gin/bindingfor that.ctx.ReadJSONruns as before. After a validated bind,Application.Validatestill runs, asReadJSONdid.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
openapi3filtervalidator with and withoutKeepValidatedValue(same shape as the stdhttp test from Bind the request body value kin-openapi validated in strict net/http servers #2612).ctx.Validateagainst a rejecting validator still produces 422 and is invoked exactly once, with and withoutKeepValidatedValue.make generateis clean; golangci-lint reports no issues.🤖 Generated with Claude Code