-
Notifications
You must be signed in to change notification settings - Fork 210
Description
request.url_for fails to find the correct route when titiler is mounted as a sub application (https://github.com/developmentseed/titiler/blob/nonTMSReader/titiler/endpoints/factory.py#L1013):
from fastapi import FastAPI
from titiler.endpoints.stac import STACTiler
app = FastAPI()
titiler_app = FastAPI()
titiler_app.include_router(STACTiler().router)
app.mount("/titiler", titiler_app)This is because request.url_for uses the router of the request's current scope which in this case is app and not titiler_app. Because sub applications are independent of the parent app, the titiler routes are not actually registered on app, so starlette can't find the route and raises starlette.routing.NoMatchFound.
A solution that works is replacing calls to request.url_for with self.router.url_path_for which has the same signature (its called under the hood by request.url_for) but only searches the specific router, ensuring that route resolution will always work no matter how the app is mounted.