Description
As FastAPI is a subclass of Starlette, it is possible to mount any ASGI-compliant app using app.mount; specifically, I have mounted a Starlette endpoint and it works as expected. However, it does not show in the documentation on /docs -- am I missing something, or it is something that needs to be implemented yet? Thank you!
OK, I found https://fastapi.tiangolo.com/tutorial/sub-applications-proxy/#mount-the-sub-application, but this again works only on a FastAPI instance and not on general ASGI apps. I guess this makes sense, but I would still like to see documentation being built properly based on the full route structure...
@berislavlopac do you mean the automatic docs?
The automatic docs, OpenAPI, JSON Schema, etc. are more or less the main feature of FastAPI, a lot of the FastAPI internal code is there to support those docs.
As that code is not there in other ASGI applications, there's no way to generate docs automatically for them.
Yes, I figured it out, which is why I closed the issue. I might be a neat approach to have the documentation functionality implemented as a mixin or something similar, so it could be reused on any ASGI app. :thinking:
@tiangolo stumbled upon same problem recently, my case is slightly different - starlette.routing.Mount gets completely ignored by the docs generation routine:
from fastapi import FastAPI
from fastapi.routing import APIRoute
from starlette.routing import Mount
from starlette.responses import UJSONResponse
import handlers
routes = [
Mount('/vars', routes=[
APIRoute('/', handlers.get_vars, methods=['GET'], response_class=UJSONResponse),
APIRoute('/', handlers.create_vars, methods=['POST'], status_code=204, response_class=UJSONResponse),
APIRoute('/values', handlers.get_values, methods=['GET'], response_class=UJSONResponse),
APIRoute('/values', handlers.save_values, methods=['POST'], response_class=UJSONResponse)
]),
Mount('/labels', routes=[
APIRoute('/records', handlers.save_label_records, methods=['POST'], response_class=UJSONResponse)
])
]
app = FastAPI(
debug=config.get('DEBUG'),
redoc_url=None, # disable redoc
default_response_class=UJSONResponse,
routes=routes
)
Any way to workaround this?
Another issue is that I have to specify response_class=UJSONResponse every time, but that's not a big deal.
@dmig-alarstudios did you find any workaround ??
@awemulya, like @tiangolo mentioned above, it will work when you mount FastAPI instance, it is a feature in FastAPI. Does app.mount()syntax doesn't work for you?
Most helpful comment
@tiangolo stumbled upon same problem recently, my case is slightly different -
starlette.routing.Mountgets completely ignored by the docs generation routine:Any way to workaround this?
Another issue is that I have to specify
response_class=UJSONResponseevery time, but that's not a big deal.