Skip to content

Keep the validated request body value by default - #55

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 to bind the value that
request validation decoded instead of decoding the body a second time
(oapi-codegen/oapi-codegen#2605). Decoding twice allowed a validation
bypass: encoding/json matches object members case-insensitively, so a
body such as {"role":"user","ROLE":"admin"} validates as role "user"
but binds as Role "admin".

kin-openapi's openapi3filter.Options.KeepValidatedValue leaves the
decoded, validated body on the validated request's Body behind a
ValidatedValue() method. Enable it by default, consistently with the
other oapi-codegen middlewares. The filter options are now built by a
filterOptions helper from a copy of the caller's Options.Options, so the
caller's struct is never modified; with no options at all, the
middleware now passes {KeepValidatedValue: true} instead of nil.

Add Options.DiscardValidatedValue as the opt-out, for instance to limit
memory retention for very large request bodies. It overrides any
KeepValidatedValue set in Options.Options.

In this middleware the setting has no visible effect yet: validation
runs on a net/http request converted from the fiber context by
adaptor.ConvertRequest, which is a copy, so fiber handlers never see
anything openapi3filter attaches to its body. Exposing the value to
handlers (for example via c.Locals) is left to a later change; the
Options field doc comment and README say so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mromaszewicz
mromaszewicz requested a review from a team as a code owner October 23, 2026 01:43
@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