Skip to content

Keep the validated request body value by default - #49

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

mromaszewicz merged 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.

With the flag on, the request body that reaches the handler exposes the value the validator decoded and validated through a ValidatedValue() any method. The body itself stays fully readable. oapi-codegen strict servers are being changed (oapi-codegen/oapi-codegen#2605) to bind that value instead of decoding the body a second time, which closes a validation bypass: encoding/json matches member names case-insensitively, so {"role":"user","ROLE":"admin"} validates as role: user but binds as Role: "admin". Defaulting the flag here makes that fix effective without configuration.

Changes:

  • New Options.DiscardValidatedValue turns the flag back off, for example to limit memory retention for very large bodies. It overrides Options.Options.KeepValidatedValue.
  • The caller's openapi3filter.Options is copied, never mutated; nil options now get {KeepValidatedValue: true}.
  • Tests cover the default, empty options, the opt-out (including overriding an explicit KeepValidatedValue: true), and ExcludeRequestBody, and check the body still reads the original bytes.
  • README documents the default, the opt-out, and that middleware which replaces the request body (decompression, size limiting, logging wrappers) belongs before the validator, since it would otherwise hide the validated value.

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

🤖 Generated with Claude Code

oapi-codegen strict servers decode the request body a second time after
this middleware has validated it. Because encoding/json matches object
members case-insensitively, a body such as {"role":"user","ROLE":"admin"}
validates as role: user but binds as Role: "admin", bypassing validation.
oapi-codegen/oapi-codegen#2605 changes strict servers to bind the value
that was validated instead, which kin-openapi exposes when
openapi3filter.Options.KeepValidatedValue is set: the request body gains
a ValidatedValue() any method returning the decoded, validated value,
while remaining readable with its original bytes.

For that fix to take effect without extra configuration, the middleware
now enables KeepValidatedValue by default, both for
OapiRequestValidator(spec) and when Options are supplied. The caller's
openapi3filter.Options is copied rather than mutated. The new
Options.DiscardValidatedValue opts out, for example to limit memory
retention for very large request bodies, and overrides
Options.Options.KeepValidatedValue.

Since the middleware validates ctx.Request() in place, any middleware
registered after it that replaces the request body (decompression, size
limiting, logging wrappers) hides the validated value from handlers. The
README documents that such middleware belongs before the validator.

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 enhancement New feature or request label Oct 23, 2026
@mromaszewicz
mromaszewicz merged commit 146c954 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