Skip to content

Propagate the request replaced by AuthenticationFunc to the next handler - #96

Open
draarivpatel-ui wants to merge 1 commit into
oapi-codegen:mainfrom
draarivpatel-ui:fix/auth-request-context-propagation
Open

draarivpatel-ui wants to merge 1 commit into
oapi-codegen:mainfrom
draarivpatel-ui:fix/auth-request-context-propagation

Conversation

@draarivpatel-ui

Copy link
Copy Markdown

Fixes #60

Problem

An AuthenticationFunc that attaches claims to the request by replacing RequestValidationInput.Request (the only channel openapi3filter exposes) had no effect: after ValidateRequest, the middleware served the original *http.Request to the next handler, so the new context never reached it. The validated body left by KeepValidatedValue on the replacement request was lost the same way.

Fix

On successful validation, serve RequestValidationInput.Request when it differs from the incoming request, in both validation paths (OapiRequestValidator / ErrorHandler and ErrorHandlerWithOpts). RequestURI is a plain field rather than part of the shared *url.URL, so the restored value is copied onto the replacement when Options.Prefix stripped it.

Test

Added TestAuthenticationFuncRequestPropagation, which runs an AuthenticationFunc that replaces input.RequestValidationInput.Request with a WithContext copy carrying a value, and asserts the downstream handler sees the value and the original path. It covers both code paths and fails before the fix (got <nil>) in each. go test -cover ./... passes (84.0%).

An AuthenticationFunc that attaches claims via
RequestValidationInput.Request = r.WithContext(...) had no effect:
after validation the middleware served the original request, so the
context values, and any body left by KeepValidatedValue on the
replacement request, never reached the handler.

Serve the request left on the RequestValidationInput instead, keeping
the restored RequestURI when a Prefix is configured.

Fixes oapi-codegen#60
@draarivpatel-ui
draarivpatel-ui requested a review from a team as a code owner November 24, 2026 20:08
Copilot AI balanced review requested due to automatic review settings November 24, 2026 20:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

middleware.OapiRequestValidatorWithOptions does not keep request context set in authentication middleware

2 participants