Double path to docs #11751
-
First Check
Commit to Help
Example Codeimport uvicorn
from fastapi import FastAPI, APIRouter
router = APIRouter(prefix='/v1')
@router.get('/healthcheck', tags=['Healthcheck'])
def check_service():
return {'status': 'OK'}
def create_app() -> FastAPI:
api = FastAPI(
title='Test App',
root_path='/api',
root_path_in_servers=False,
)
api.include_router(router)
return api
app = create_app()
if __name__ == '__main__':
uvicorn.run(
'test:app',
host='0.0.0.0',
port=5000,
reload=True
)DescriptionActually, I am not sure if it is a FastAPI issue. It can be on the Swagger UI side but I haven't found any related issues in their repository, so that's why I've decided to open the discussion here. The problem is that when I use root_path parameter in FastAPI application I get documentation accessible on 2 paths: default /docs and root_path+/docs. Currently, I see 2 ways how I can handle this issue:
Let me clarify that if I use: I reckon that it would be best to have opportunity to switch off default /docs path if we have root_path parameter specified. I don't insist that it should be a default setting but maybe we could control it via additional parameter. Feel free to express your opinion on the topic. Operating SystemLinux Operating System DetailsNo response FastAPI Version0.111.0 Pydantic Version1.10.10 Python Version3.11.9 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
When you use As I understand this is done this way to make the app compatible with different ASGI servers (not sure about this). To make it accessible by only normal paths you can remove that parameter from import uvicorn
from fastapi import FastAPI, APIRouter
router = APIRouter(prefix='/v1')
@router.get('/healthcheck', tags=['Healthcheck'])
def check_service():
return {'status': 'OK'}
def create_app() -> FastAPI:
api = FastAPI(
title='Test App',
# root_path='/api',
root_path_in_servers=False,
)
api.include_router(router)
return api
app = create_app()
if __name__ == '__main__':
uvicorn.run(
'test:app',
host='0.0.0.0',
port=5000,
root_path="/api",
reload=True
)Now, you |
Beta Was this translation helpful? Give feedback.
When you use
root_path='/api'parameter inFastAPI()it will make all your endpoint accessible via two paths: 1) normal (http://localhost:5000/v1/healthcheck) and 2) prefixed (http://localhost:5000/api/v1/healthcheck).As I understand this is done this way to make the app compatible with different ASGI servers (not sure about this).
To make it accessible by only normal paths you can remove that parameter from
FastAPI()and pass it touvicorn.run()(you should run your app behind the stripping path proxy, otherwise it will fail to show docs):