fix: enable mounting sub-applications under APIRouter#14793
Open
jonathan-fulton wants to merge 1 commit intofastapi:masterfrom
Open
fix: enable mounting sub-applications under APIRouter#14793jonathan-fulton wants to merge 1 commit intofastapi:masterfrom
jonathan-fulton wants to merge 1 commit intofastapi:masterfrom
Conversation
Added support for mounting sub-applications on APIRouter with proper
prefix handling:
1. Override mount() in APIRouter to automatically apply the router's
prefix to the mount path
2. Handle Mount routes in include_router() to apply the include prefix
This allows patterns like:
router = APIRouter(prefix='/api')
router.mount('/subapp', FastAPI())
app.include_router(router)
Which correctly routes requests to /api/subapp/...
Note: Mounts must be added before include_router() is called, as
include_router copies routes at call time. This is consistent with
how other routes work.
Fixes fastapi#10180
Member
|
I think we should wait for Sebastian to take a decision on whether we want to support this. |
Contributor
Author
|
@YuriiMotov Thanks for the feedback! That makes sense - I'll wait for Sebastian's decision on whether this should be supported more broadly or limited to StaticFiles. I agree that for full apps, using Happy to adjust the scope based on Sebastian's guidance! |
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 #10180
When mounting a sub-application on an
APIRouterwith a prefix, the mount wasn't accessible at the expected path. The router's prefix wasn't being applied to mounts.Changes
mount()inAPIRouterto automatically apply the router's prefixMountroutes ininclude_router()to apply the include prefixUsage
Enables patterns like:
Note: Mounts must be added before
include_router()is called.Testing
Added comprehensive tests in
tests/test_router_mount.py