Repository navigation
Bind the request body value kin-openapi validated in strict net/http servers - #2612
Merged
Merged
Conversation
…servers Closes: oapi-codegen#2605 Strict net/http, Chi and gorilla/mux servers decoded a JSON request body a second time after the validation middleware had already decoded and validated it. Besides the duplicated work, encoding/json matches member names case-insensitively, so a body such as {"role":"user","ROLE":"admin"} passed validation as role=user and reached the handler as Role="admin", bypassing the schema's enum, pattern, length, range and readOnly constraints. kin-openapi v0.151.0 added openapi3filter.Options.KeepValidatedValue, which leaves the validated value on the request body. The strict-http template now calls runtime.BindJSONBody, which binds that value into the request struct with exact member-name matching when it is present, and otherwise decodes the body with encoding/json exactly as before, so generated behaviour is unchanged for applications that do not set the option. kin-openapi is bumped to v0.152.0 in all three modules; the regenerated fixtures differ only in the decode line. An end-to-end test runs the strict stdhttp server behind an openapi3filter validator with and without the option. The README documents how to enable it with nethttp-middleware. Requires a runtime release that includes BindJSONBody. Co-Authored-By: Claude Fable 5.1 <[email protected]>
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
runtime.BindJSONBody, which the regenerated strict net/http servers call, shipped in github.com/oapi-codegen/runtime v1.8.0. Bump the pin in internal/test and examples so the fixtures compile against the released module instead of a local replace. Co-Authored-By: Claude Fable 5.1 <[email protected]>
nethttp-middleware v1.3.1 enables KeepValidatedValue out of the box, so the tip only needs the explicit option for older versions or hand-rolled openapi3filter setups. Also name the runtime version that ships BindJSONBody. Co-Authored-By: Claude Fable 5.1 <[email protected]>
mromaszewicz
marked this pull request as ready for review
October 23, 2026 06:34
mdtoc (readme-toc-check) does not parse a fenced code block nested in a blockquote and dropped every heading after the tip from the TOC. An indented block renders the same on GitHub and keeps the check green. Co-Authored-By: Claude Fable 5.1 <[email protected]>
Contributor
[Medium impact] The PR appears safe to merge; no actionable defect was found. SummaryStrict Chi, Gorilla, and stdhttp handlers now call
Tests were not run. Reviews (1) · Last reviewed commit: "README: use an indented code block insid..." · Reviewed by Greptile |
This was referenced 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]>
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
Closes #2605.
Strict net/http, Chi and gorilla/mux servers decoded a JSON request body a second time after the validation middleware had already decoded and validated it.
encoding/jsonmatches member names case-insensitively, so{"role":"user","ROLE":"admin"}passed validation asrole: userand reached the handler asRole: "admin", bypassing the schema's enum, pattern, length, range and readOnly constraints.kin-openapi v0.151.0 added
openapi3filter.Options.KeepValidatedValue, which leaves the validated value on the request body. This PR:internal/testandexamplesmodules (no fixture changes result from the bump itself);strict-http.tmpltoruntime.BindJSONBody(r, &body), which binds the validated value with exact member-name matching when present and otherwise decodes the body withencoding/jsonexactly as before, so nothing changes for applications that do not set the option;openapi3filtervalidator with and without the option;Echo, Gin, Iris and Fiber strict servers decode through their framework binders and are not changed here, as discussed in #2605.
Depends on
BindJSONBody), released and theruntimepin bumped ininternal/test/go.modandexamples/go.mod. Until then the regenerated fixtures do not compile against runtime v1.7.0, so this is a draft.🤖 Generated with Claude Code