Skip to content

fix: enable mounting sub-applications under APIRouter#14793

Open
jonathan-fulton wants to merge 1 commit intofastapi:masterfrom
jonathan-fulton:fix/issue-10180-router-mount
Open

fix: enable mounting sub-applications under APIRouter#14793
jonathan-fulton wants to merge 1 commit intofastapi:masterfrom
jonathan-fulton:fix/issue-10180-router-mount

Conversation

@jonathan-fulton
Copy link
Contributor

Summary

Fixes #10180

When mounting a sub-application on an APIRouter with a prefix, the mount wasn't accessible at the expected path. The router's prefix wasn't being applied to mounts.

Changes

  1. Override mount() in APIRouter to automatically apply the router's prefix
  2. Handle Mount routes in include_router() to apply the include prefix

Usage

Enables patterns like:

router = APIRouter(prefix='/api')
router.mount('/subapp', FastAPI())
app.include_router(router)
# Sub-app is now accessible at /api/subapp/...

Note: Mounts must be added before include_router() is called.

Testing

Added comprehensive tests in tests/test_router_mount.py

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
@codspeed-hq
Copy link

codspeed-hq bot commented Feb 1, 2026

Merging this PR will not alter performance

✅ 20 untouched benchmarks


Comparing jonathan-fulton:fix/issue-10180-router-mount (b641244) with master (c9629e0)1

Open in CodSpeed

Footnotes

  1. No successful run was found on master (0892440) during the generation of this report, so c9629e0 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@YuriiMotov
Copy link
Member

I think we should wait for Sebastian to take a decision on whether we want to support this.
I think we might want to support this for StaticFiles only..
Apps can be mount to the root app with prefix, and, IMO it will be clearer

@jonathan-fulton
Copy link
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 mount() on the root app with a prefix is cleaner. The main use case I was targeting was making it easier to mount StaticFiles under an APIRouter, which is a common pattern for organizing route groups with their own static assets.

Happy to adjust the scope based on Sebastian's guidance!

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.

Mounting sub-applications under APIRouter

2 participants