Skip to content

Bind the validated request body value in strict Fiber servers - #2620

Merged
mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/validated-body-fiber
Oct 23, 2026
Merged

mromaszewicz merged 3 commits into
oapi-codegen:mainfrom
mromaszewicz:fix/validated-body-fiber

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

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, so fiber-middleware and fiber-v3-middleware validate 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 key x-oapi-codegen-validated-body. The strict Fiber template now reads it from there:

  • The generated code defines an unexported validatedBodyLocalsKey constant with the same string, with a comment pointing at the middleware's exported ValidatedBodyLocalsKey, so generated code does not import the middleware.
  • When the context holds a value, the handler binds it with runtime.BindJSONValue (exact member-name matching). When there is no value, or it does not fit the request body type, it falls back to ctx.BodyParser (Fiber v2) or ctx.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

  • New validated_body_test.go in internal/test/servers/strict/fiber and .../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).
  • Regenerated fixtures: strict fiber and fiberv3, smoke, multicontent, text_non_string, response_cast.
  • go test ./pkg/codegen/ and both Fiber strict modules pass; make lint clean.

README

The validation-middleware tip now lists Fiber, names the two middleware versions, and explains that the Fiber middleware version, rather than the KeepValidatedValue option alone, is what enables this.

🤖 Generated with Claude Code

mromaszewicz and others added 3 commits October 23, 2026 00:22
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]>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner October 23, 2026 18:37
@mromaszewicz mromaszewicz added bug Something isn't working notable changes Used for release notes to highlight these more highly labels Oct 23, 2026
@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 issue was found.

Summary

Strict Fiber v2 and v3 handlers now bind the validated JSON value stored in the Fiber context.

  • Strict Fiber handlers bind the JSON value saved by validation middleware.

Reviews (1) · Last reviewed commit: "Document that strict Fiber servers bind ..." · Reviewed by Greptile

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

1 participant