docs: clarify router-agnostic middleware runs after route resolution - #1088
Open
stareezy-1 wants to merge 1 commit into
Open
docs: clarify router-agnostic middleware runs after route resolution#1088stareezy-1 wants to merge 1 commit into
stareezy-1 wants to merge 1 commit into
Conversation
The UseMiddleware doc comment claimed the middleware stack executes before route matching, but the stack runs inside the matched operation's handler chain: the adapter's router resolves the route first, so unmatched requests (404) never enter it. Correct the comment and add a test locking in the actual behavior (issue danielgtaylor#933).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #933
Problem
The
UseMiddlewaredoc comment claims the middleware stack executes before route matching:This is not what happens. Router-agnostic middleware runs inside the matched operation handler chain (
api.Middlewares().Handler(...)), so the adapter router resolves the route first. An unmatched request is answered by the router with a404and never enters the middleware chain:Why docs and not a behavior change: middleware receives a
huma.Contextbound to the matched operation (ctx.Operation()), which cannot exist before route resolution. Moving the chain before routing would require a context redesign across all adapters. Router-specific middleware is the supported way to run pre-routing logic (see the middleware docs).Fix
UseMiddlewaredoc comment to describe the actual behavior: the stack runs after the adapter router matches the request to a registered operation, and unmatched requests do not pass through it.Tests
TestMiddlewareDoesNotRunForUnmatchedRouteadded tohuma_test.go:GET /unmatched404, middleware flag stays falseGET /matchedAll existing tests continue to pass (
go test -race ./...).