Repository navigation
Bind the validated request body value in all strict servers (2.8.x backport) - #2621
Merged
mromaszewicz merged 2 commits intoOct 23, 2026
Merged
mromaszewicz merged 2 commits into
mromaszewicz merged 2 commits into
Conversation
Refs: oapi-codegen#2605 Backport of oapi-codegen#2612, oapi-codegen#2613 and oapi-codegen#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]>
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
Contributor
[Medium impact] The PR appears safe to merge; no new actionable issues were found. SummaryBackports validated JSON body binding across all seven strict-server templates.
Reviews (2) · Last reviewed commit: "Regenerate boilerplate on Go 1.26" · Reviewed by Greptile |
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
Backport of #2612, #2613 and #2620 to
release/v2.8.x, for the 2.8.1 patch release. See #2605 for the problem.main's templates were refactored into shared skeletons after v2.8.0, so this is not a cherry-pick: the same changes are applied by hand to the seven 2.8.x strict templates (Echo v5 and Fiber v3 are still separate files on this branch), and everything is regenerated in one commit.
When a request validator built on kin-openapi's
openapi3filterruns withKeepValidatedValue, the strict handler used to decode the JSON body a second time through the framework binder.encoding/jsonmatches member names case-insensitively, so{"role":"user","ROLE":"admin"}validated asrole: userbut bound asRole: "admin". Each strict template now binds the value the validator kept, with exact member-name matching:runtime.BindJSONBodyjson.NewDecoder(insideBindJSONBody)runtime.BindJSONValueon theDefaultBinderpath onlyDefaultBinder.BindBody; a customecho.Binderalways getsctx.Bindruntime.BindJSONValue, thenbinding.Validator.ValidateStructctx.ShouldBindJSONruntime.BindJSONValue, thenApplication().Validatectx.ReadJSONruntime.BindJSONValueon the value fiber-middleware v1.3.0 / fiber-v3-middleware v0.3.0 store in the context underx-oapi-codegen-validated-bodyctx.BodyParser/ctx.Bind().BodyWithout a validated value, or when it does not fit the body type, every template does exactly what it did before, so error handling and empty-body behaviour are unchanged.
Dependency pins
runtimev1.8.0 ininternal/testandexamples(needed forBindJSONBody/BindJSONValue).internal/testonly, so the new tests can enableKeepValidatedValue. The root module's pin is unchanged.github.com/gin-gonic/gin/binding.Regenerated files
make generatewas run once over all three modules. The embedded swagger spec blobs change in every regenerated file becausecompress/flateoutput differs between the Go version that produced the committed files and Go 1.27, which built these. The code changes outside those blobs are only the binding blocks and the Gin import.Testing
validated_body_test.gofrom main for stdhttp, echo, echo5, gin, iris and fiber, each running the strict server with and without a validated value. There is no strict Fiber v3 test package on this branch, so Fiber v3 is covered by the regenerated examples compiling.go test ./pkg/codegen/, allinternal/test/servers/strict/...packages,go veton examples, root lint and the README TOC check pass locally.README gains the same validation-middleware tip as main.
🤖 Generated with Claude Code