Mounting sub-applications under APIRouter · fastapi/fastapi · Discussion #8682
First Check
- I added a very descriptive title to this issue.
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google "How to X in FastAPI" and didn't find any information.
- I already read and followed all the tutorial in the docs and didn't find an answer.
- I already checked if it is not related to FastAPI but to Pydantic.
- I already checked if it is not related to FastAPI but to Swagger UI.
- I already checked if it is not related to FastAPI but to ReDoc.
Commit to Help
- I commit to help with one of those options 👆
Example Code
from fastapi import FastAPI, APIRouter from starlette.testclient import TestClient app = FastAPI() api_router = APIRouter(prefix="/api") @api_router.get("/app") def read_main(): return {"message": "Hello World from main app"} app.include_router(api_router) subapi = FastAPI() @subapi.get("/sub") def read_sub(): return {"message": "Hello World from sub API"} api_router.mount("/subapi", subapi) client = TestClient(app) assert client.get('/api/app').status_code == 200 # this next assert fails assert client.get('/api/subapi/sub').status_code == 200
Description
Is it possible to mount a sub-application under an APIRouter? APIRouter itself has a mount function and accepts similar arguments to mounting a sub-application on a FastAPI instance, but I can't get the routing to actually work (nor can i get the openapi docs or spec to come back from that I would assume are the correct URLs.
The docs for sub applications note that the sub-application will have it's root_path correctly set, and I've tried a few combinations of manually setting the root_path on the subapi instance, but to no avail.
Operating System
macOS
Operating System Details
No response
FastAPI Version
0.70.0
Python Version
3.9.7
Additional Context
No response