Skip to content

Keep the validated request body value and expose it via c.Locals - #56

Merged
mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:feat/validated-body-locals
Oct 23, 2026
Merged

mromaszewicz merged 1 commit into
oapi-codegen:mainfrom
mromaszewicz:feat/validated-body-locals

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Summary

Enables openapi3filter.Options.KeepValidatedValue by default, as the other oapi-codegen validation middlewares now do, and makes the validated body value reachable from Fiber handlers.

Fiber does not run on net/http, so the validator works on a converted copy of the request (adaptor.ConvertRequest). kin-openapi leaves the decoded, validated value on that copy's body, which handlers never see. After a successful validation the middleware now copies the value into the Fiber context:

  • ValidatedBodyLocalsKey = "x-oapi-codegen-validated-body" is the c.Locals key.
  • ValidatedBody(c) returns the value and whether one was stored.
  • Options.DiscardValidatedValue is the opt-out and overrides Options.Options.KeepValidatedValue. The caller's options struct is copied before the default is applied.

The body bytes seen by the handler (c.Body()) are unchanged.

Why

oapi-codegen strict Fiber servers will bind that value with exact member-name matching instead of decoding the body a second time, which closes the gap where encoding/json's case-insensitive member matching lets a request validate as one value but bind as another. The generated code defines the same key string independently so it does not import this module; a comment on both sides says to keep them in sync.

Testing

  • TestOapiRequestValidatorKeepValidatedValue covers nil options, empty options, DiscardValidatedValue overriding an explicit KeepValidatedValue, and ExcludeRequestBody, asserting the stored value and that the raw body is unchanged.
  • make tidy, make test, make lint pass locally.

README gains a "Validated request body" section.

🤖 Generated with Claude Code

Enable openapi3filter.Options.KeepValidatedValue by default, as the other
oapi-codegen validation middlewares do, with Options.DiscardValidatedValue
as the opt-out. Fiber does not run on net/http, so the validator works on a
converted copy of the request and the value it leaves on that copy's body
is not reachable from handlers. After validation the middleware now copies
the value into the Fiber context under ValidatedBodyLocalsKey
("x-oapi-codegen-validated-body"), readable with ValidatedBody(c).

oapi-codegen strict Fiber servers bind that value with exact member-name
matching instead of decoding the body a second time, which closes the gap
where encoding/json's case-insensitive matching let an unvalidated "ROLE"
member bind to `role`. The key is defined independently in the generated
code so it does not import this module; keep the two in sync.

The body bytes seen by handlers are unchanged. The caller's options struct
is copied before the default is applied.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner October 23, 2026 16:58
@mromaszewicz mromaszewicz added the enhancement New feature or request label Oct 23, 2026
@mromaszewicz
mromaszewicz merged commit cf60f15 into oapi-codegen:main Oct 23, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant