Fix incoming: scope["route"].path loses include_router() prefixes #16177
Replies: 1 comment
|
Let's not create duplicates and track it in #16051 |
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Summary
Fix a regression introduced in FastAPI 0.137.0 where HTTP routes added with
include_router()lose their include-time prefixes inrequest.scope["route"].pathandrequest.scope["route"].path_format.For example, a route registered as:
/users/{user_id}and included with:
prefix="/api"should expose:
/api/users/{user_id}but currently exposes:
/users/{user_id}.This regression was reported in Discussion #16051.
Why this matters
scope["route"]is commonly used by middleware and instrumentation for logging, tracing, and metrics.When included routes lose their prefixes, distinct endpoints such as:
/users/{id}/orders/{id}can be incorrectly represented by the same route label.
Root cause
Three routing paths were still writing the original, unprefixed
APIRouteintoscope["route"]even when an effective include-time route context was available:APIRoute.matches()_IncludedRouter._handle_selected()APIRouter.app()low-priority route handlingThe effective prefixed path already exists in
RouteContext/_EffectiveRouteContext, but it was not being exposed throughscope["route"].Fix
Use the existing
RouteContextfacade when an effective route context applies.This allows
scope["route"]to expose the effective.pathand.path_formatwhile preserving access to the underlying route through.original_route.Tests
Added focused regression tests covering:
include_router()with a prefixscope["route"]The new tests fail without the fix and pass with it.
Full test suite:
Related discussion
This PR addresses the regression reported in #16051.
Notes
For included HTTP routes,
scope["route"]now exposes aRouteContextinstead of the originalAPIRoute. Attribute access such as.path,.path_format,.name, and.endpointcontinues to work, while.original_routeprovides access to the underlying route object.PR: #16176
All reactions