Skip to content

Keep the validated request body value by default - #16

Closed
mromaszewicz wants to merge 1 commit into
oapi-codegen:mainfrom
mromaszewicz:feat/keep-validated-value
Closed

mromaszewicz wants to merge 1 commit into
oapi-codegen:mainfrom
mromaszewicz:feat/keep-validated-value

Conversation

@mromaszewicz

Copy link
Copy Markdown
Member

Summary

Enables kin-openapi's openapi3filter.Options.KeepValidatedValue by default, with an opt-out, for consistency with the other oapi-codegen middlewares.

With the flag on, openapi3filter leaves the value it decoded and validated on the validated request's body behind a ValidatedValue() any method, which oapi-codegen strict servers are being changed to bind (oapi-codegen/oapi-codegen#2605). This middleware validates a net/http request produced by adaptor.ConvertRequest, a copy, so Fiber handlers do not yet observe the value and there is no behaviour change for them. The README and the field's doc comment say so. Exposing it to Fiber handlers (for example via c.Locals) is a follow-up.

Changes:

  • New Options.DiscardValidatedValue turns the flag back off. It overrides Options.Options.KeepValidatedValue.
  • The caller's openapi3filter.Options is copied, never mutated; nil options now get {KeepValidatedValue: true}.
  • Unit tests for the option mapping, plus table cases showing valid and invalid requests behave the same with the default and with the opt-out.
  • README section on the setting.

Requires kin-openapi v0.151.0 or later; main is on v0.152.0.

🤖 Generated with Claude Code

oapi-codegen strict servers are being changed (oapi-codegen/oapi-codegen#2605)
to bind the request body value that openapi3filter actually validated, rather
than decoding the raw body a second time. Decoding twice lets a request slip
past validation: encoding/json matches object members case-insensitively, so
{"role":"user","ROLE":"admin"} validates as role: user but binds as
Role: "admin".

kin-openapi's openapi3filter.Options.KeepValidatedValue makes ValidateRequest
leave the decoded, validated value on the request body behind a
ValidatedValue() any method. Enable it by default, as in the other
oapi-codegen middlewares, and add Options.DiscardValidatedValue as an opt-out
for callers that want to drop the decoded value after validation (e.g. to
limit memory retention for very large bodies). The opt-out overrides
Options.Options.KeepValidatedValue. The middleware now passes a copy of the
caller's openapi3filter.Options, so the caller's struct is never mutated;
with no Options it passes &openapi3filter.Options{KeepValidatedValue: true}.

In this middleware the setting is currently inert for Fiber handlers:
ValidateRequestFromContext validates an *http.Request built by
adaptor.ConvertRequest, which is a fresh request whose body reads from a copy
of the fasthttp body bytes. Nothing openapi3filter writes to that request
reaches the Fiber handler, which still reads c.Body(). The default and
opt-out are added now for consistency and for a later change that exposes
the validated value to handlers; the doc comment and README say so.

Tests cover the option mapping (nil, zero, opt-out, override of
KeepValidatedValue, other fields preserved, caller's struct not mutated) and
check that valid and invalid requests behave identically with the default
and with DiscardValidatedValue, with handlers seeing the original body.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mromaszewicz mromaszewicz added the documentation Improvements or additions to documentation label Oct 23, 2026
@mromaszewicz

Copy link
Copy Markdown
Member Author

I changed my mind, I don't want a non-functional flag for symmetry with other middleware.

@mromaszewicz
mromaszewicz deleted the feat/keep-validated-value branch October 23, 2026 02:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant