Mounting sub-applications under `APIRouter`

Discussed in #8682

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.

Related