Skip to content

Bind the validated request body value in all strict servers (2.8.x backport) - #2621

Merged
mromaszewicz merged 2 commits into
oapi-codegen:release/v2.8.xfrom
mromaszewicz:fix/validated-body-2.8.x
Oct 23, 2026
Merged

mromaszewicz merged 2 commits into
oapi-codegen:release/v2.8.xfrom
mromaszewicz:fix/validated-body-2.8.x

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

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 openapi3filter runs 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 {"role":"user","ROLE":"admin"} validated as role: user but bound as Role: "admin". Each strict template now binds the value the validator kept, with exact member-name matching:

Framework Binds via Falls back to
net/http, Chi, gorilla/mux runtime.BindJSONBody json.NewDecoder (inside BindJSONBody)
Echo, Echo v5 runtime.BindJSONValue on the DefaultBinder path only DefaultBinder.BindBody; a custom echo.Binder always gets ctx.Bind
Gin runtime.BindJSONValue, then binding.Validator.ValidateStruct ctx.ShouldBindJSON
Iris runtime.BindJSONValue, then Application().Validate ctx.ReadJSON
Fiber, Fiber v3 runtime.BindJSONValue on the value fiber-middleware v1.3.0 / fiber-v3-middleware v0.3.0 store in the context under x-oapi-codegen-validated-body ctx.BodyParser / ctx.Bind().Body

Without 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

  • runtime v1.8.0 in internal/test and examples (needed for BindJSONBody / BindJSONValue).
  • kin-openapi v0.152.0 in internal/test only, so the new tests can enable KeepValidatedValue. The root module's pin is unchanged.
  • Gin generated code now imports github.com/gin-gonic/gin/binding.

Regenerated files

make generate was run once over all three modules. The embedded swagger spec blobs change in every regenerated file because compress/flate output 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.go from 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/, all internal/test/servers/strict/... packages, go vet on examples, root lint and the README TOC check pass locally.

README gains the same validation-middleware tip as main.

🤖 Generated with Claude Code

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]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner October 23, 2026 18:49
@mromaszewicz mromaszewicz added bug Something isn't working notable changes Used for release notes to highlight these more highly labels Oct 23, 2026
@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
Addedgithub.com/​getkin/​kin-openapi@​v0.152.074100100100100
Updatedgithub.com/​oapi-codegen/​runtime@​v1.5.0 ⏵ v1.8.094 -3100100100100

View full report

@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 new actionable issues were found.

Summary

Backports validated JSON body binding across all seven strict-server templates.

  • Strict handlers bind the request body value that passed validation.

Reviews (2) · Last reviewed commit: "Regenerate boilerplate on Go 1.26" · Reviewed by Greptile

Comment thread pkg/codegen/configuration.go Resolved
@mromaszewicz
mromaszewicz merged commit 7e9bbce into oapi-codegen:release/v2.8.x Oct 23, 2026
15 checks passed
@mromaszewicz
mromaszewicz deleted the fix/validated-body-2.8.x branch October 23, 2026 20:33
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.

1 participant