Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mounting sub-applications under APIRouter #10180

Open
Kludex opened this issue Aug 31, 2023 Discussed in #8682 · 8 comments
Open

Mounting sub-applications under APIRouter #10180

Kludex opened this issue Aug 31, 2023 Discussed in #8682 · 8 comments
Labels
bug Something isn't working

Comments

@Kludex
Copy link
Sponsor Collaborator

Kludex commented Aug 31, 2023

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

@Kludex Kludex added question Question or problem question-migrate bug Something isn't working and removed question Question or problem question-migrate labels Aug 31, 2023
@Kludex Kludex changed the title Mounting sub-applications under APIRouter Mounting sub-applications under APIRouter Aug 31, 2023
@Kludex
Copy link
Sponsor Collaborator Author

Kludex commented Aug 31, 2023

Here we should either raise on APIRouter.mount or make it work.

@achaayb
Copy link

achaayb commented Aug 31, 2023

I dont see a reason of binding two FastAPI instances, perhaps in your usecase you're trying to merge two applications, in this case you should have a service that extracts endpoints and mounts them into an APIRouter

@baskervilski
Copy link

baskervilski commented Aug 31, 2023

@Kludex , have you tried actually running the app and making a request to the endpoint of the subapp?

I have recently also struggled with a very similar setup, where I wanted to mount a v1 API and v2 API after a certain prefix, where I figured I should make a router to "hold" the /myapp/api/ prefix, then just mount to /v1 and /v2.

Long story short, I couldn't make it work that way, so I just mounted v1 app to myapp/api/v1 and v2 to myapp/api/v2 directly to the "root" app, no routers between.

EDIT: Also, I'm far from an expert, but I have a feeling that the TestClient class doesn't really "collaborate" with mounts... That's why I asked if you have tried to actually run the app and reach the endpoints.

@LorhanSohaky
Copy link
Contributor

Maybe related #4657

@darkb0ts
Copy link

the issue is likely related to the way you are defining the routes within the sub-application (subapi). The routing paths within subapi should not include the prefix that you intend to use when mounting it under api_router.

@LorhanSohaky
Copy link
Contributor

Even without including the prefix the problem occurs, as I reported on the issue #4656 making the doc link to be wrong

@mdczaplicki
Copy link

Any update on this? It's needed.

@mdczaplicki
Copy link

https://github.com/tiangolo/fastapi/blob/master/fastapi/routing.py#L812
This if-elsology doesn't provide a scenario for Mount.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

6 participants