🐛 Fix scope['route'].path/.path_format to include include_router() prefixes - #16176
Open
SAURABHSALVE wants to merge 1 commit into
Open
🐛 Fix scope['route'].path/.path_format to include include_router() prefixes#16176SAURABHSALVE wants to merge 1 commit into
SAURABHSALVE wants to merge 1 commit into
Conversation
SAURABHSALVE
force-pushed
the
fix/scope-route-path-includes-prefix
branch
2 times, most recently
from
August 10, 2026 11:35
7de83b4 to
5dae385
Compare
…efixes Regression since 0.137.0 (PR fastapi#15745): include_router() prefixes were lost from scope["route"].path and .path_format on HTTP routes, while WebSocket routes retained them. This caused inconsistent behavior and metrics/tracing label collapse when multiple included routers had identically-named endpoints. Root cause: APIRoute.matches(), _IncludedRouter._handle_selected(), and APIRouter.app() were writing the original unprefixed APIRoute into scope["route"] even when an _EffectiveRouteContext carrying the full prefixed path applied. Fix: Use the existing public RouteContext facade (already used by iter_route_contexts) to proxy .path/.path_format to the effective context while preserving access to .original_route. Fixes: fastapi#16051
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 regression since 0.137.0 where HTTP routes added via
include_router()hadrequest.scope[route].path/.path_formatlose all include-time prefixes, while WebSocket routes retained them (inconsistent behavior).Closes #16051
Root Cause
When an
_EffectiveRouteContext(carrying the full prefixed path) is present during routing, three sites infastapi/routing.pystill wrote the original, unprefixedAPIRouteintoscope[route]:APIRoute.matches()_IncludedRouter._handle_selected()APIRouter.app()low-priority branchSolution
Use the existing public
RouteContextfacade (defined foriter_route_contexts()) to wrap the route object when an effective context applies.RouteContextproxies.path/.path_formatto the effective context while preserving.original_routefor escape-hatch access.Testing
test_route_scope.py:include_router()with prefixNote
scope[route]type changes fromAPIRoutetoRouteContextfor included routes only (direct app routes unaffected). Attribute access is proxied; existing code accessing.path/.nameetc. works unchanged.isinstance(scope[route], APIRoute)checks would need adjustment to use.original_routeif needed, but this is precedent—WebSocket routes under include already return rebuilt instances.