Skip to content

Bind the request body value kin-openapi validated in strict net/http servers - #2612

Merged
mromaszewicz merged 5 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2605
Oct 23, 2026
Merged

mromaszewicz merged 5 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/issue-2605

Conversation

@mromaszewicz

@mromaszewicz mromaszewicz commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

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/json matches member names case-insensitively, so {"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. This PR:

  • bumps kin-openapi to v0.152.0 in the root, internal/test and examples modules (no fixture changes result from the bump itself);
  • changes the decode line in strict-http.tmpl to runtime.BindJSONBody(r, &body), which binds the validated value with exact member-name matching when present and otherwise decodes the body with encoding/json exactly as before, so nothing changes for applications that do not set the option;
  • adds an end-to-end test running the strict stdhttp server behind an openapi3filter validator with and without the option;
  • documents how to enable it with nethttp-middleware in the README.

Echo, Gin, Iris and Fiber strict servers decode through their framework binders and are not changed here, as discussed in #2605.

Depends on

🤖 Generated with Claude Code

…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]>
@socket-security

socket-security Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updatedgithub.com/​oapi-codegen/​runtime@​v1.7.0 ⏵ v1.8.094 -2100100100100

View full report

@mromaszewicz mromaszewicz added the bug Something isn't working label Oct 23, 2026
mromaszewicz and others added 2 commits October 22, 2026 23:32
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
mromaszewicz marked this pull request as ready for review October 23, 2026 06:34
@mromaszewicz
mromaszewicz requested a review from a team as a code owner 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]>
@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; no actionable defect was found.

Summary

Strict Chi, Gorilla, and stdhttp handlers now call runtime.BindJSONBody instead of decoding JSON directly.

  • Strict HTTP handlers can bind the JSON value that validation already checked.

mromaszewicz explicitly identifies fallback decoding without KeepValidatedValue as intentional compatibility behavior. The author also explicitly defers framework-specific binders outside the net/http family.

Tests were not run.

Reviews (1) · Last reviewed commit: "README: use an indented code block insid..." · Reviewed by Greptile

@mromaszewicz
mromaszewicz merged commit cbd4a9f into oapi-codegen:main Oct 23, 2026
16 checks passed
@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.

Strict net/http server: decode JSON request bodies once, using the value kin-openapi validated

1 participant