Repository navigation
Bind the validated request body value in strict Fiber servers - #2620
Merged
mromaszewicz merged 3 commits intoOct 23, 2026
Merged
Conversation
Follow-up to oapi-codegen#2605 for Fiber and Fiber v3. Fiber does not run on net/http, so fiber-middleware validates a converted copy of the request and the value kin-openapi leaves on that copy's body never reaches the handler. As of fiber-middleware v1.3.0 and fiber-v3-middleware v0.3.0 the middleware stores that value in the Fiber context under the c.Locals key "x-oapi-codegen-validated-body". The strict Fiber template now looks the value up under that key and binds it with runtime.BindJSONValue, matching member names exactly, instead of decoding the body again through ctx.BodyParser / ctx.Bind().Body, where case-insensitive matching let an unvalidated "ROLE" member bind to `role`. Without a value in the context, or when it does not fit the body type, the framework decode runs exactly as before. The key is a constant in the generated code so it does not import the middleware; the middleware defines the same string and both carry a comment to keep them in sync. Adds a test per Fiber version with a stand-in middleware that stores the value, covering the validated, absent and ill-fitting cases. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Refs: oapi-codegen#2605 Add Fiber to the README tip now that fiber-middleware v1.3.0 and fiber-v3-middleware v0.3.0 store the validated value in the Fiber context under ValidatedBodyLocalsKey, and explain why the middleware version, not the KeepValidatedValue option alone, is what makes this work for Fiber. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Contributor
[Medium impact] The PR appears safe to merge; no actionable issue was found. SummaryStrict Fiber v2 and v3 handlers now bind the validated JSON value stored in the Fiber context.
Reviews (1) · Last reviewed commit: "Document that strict Fiber servers bind ..." · Reviewed by Greptile |
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
Completes the strict-server half of #2605 for Fiber, following #2612 (net/http, Chi, gorilla/mux) and #2613 (Echo, Echo v5, Gin, Iris).
Fiber does not run on
net/http, sofiber-middlewareandfiber-v3-middlewarevalidate a converted copy of the request and the validated value kin-openapi leaves on that copy's body never reaches the handler. Since fiber-middleware v1.3.0 and fiber-v3-middleware v0.3.0 the middleware stores that value in the Fiber context under the keyx-oapi-codegen-validated-body. The strict Fiber template now reads it from there:validatedBodyLocalsKeyconstant with the same string, with a comment pointing at the middleware's exportedValidatedBodyLocalsKey, so generated code does not import the middleware.runtime.BindJSONValue(exact member-name matching). When there is no value, or it does not fit the request body type, it falls back toctx.BodyParser(Fiber v2) orctx.Bind().Body(Fiber v3) exactly as before.No behaviour changes for applications that do not use the middleware, or that run an older middleware version.
Testing
validated_body_test.goininternal/test/servers/strict/fiberand.../fiberv3: a stand-in middleware stores a value in the context. A validated value binds (123), no value falls back to the body (456), and an ill-fitting value also falls back (456).go test ./pkg/codegen/and both Fiber strict modules pass;make lintclean.README
The validation-middleware tip now lists Fiber, names the two middleware versions, and explains that the Fiber middleware version, rather than the
KeepValidatedValueoption alone, is what enables this.🤖 Generated with Claude Code