Closed jvijaybhaskar closed 9 months ago
Are you using the production environment?
By design, the default of the production environment is not generating the docs:
ENVIRONMENT
can be one of local
, staging
and production
, defaults to local, and changes the behavior of api docs
endpoints:
/docs
, /redoc
and /openapi.json
available/docs
, /redoc
and /openapi.json
available for superusers/docs
, /redoc
and /openapi.json
not availableIf you want to change this behavior, go to this part of the create_application function in the setup.py file:
if isinstance(settings, EnvironmentSettings):
if settings.ENVIRONMENT != EnvironmentOption.PRODUCTION:
docs_router = APIRouter()
if settings.ENVIRONMENT != EnvironmentOption.LOCAL:
docs_router = APIRouter(dependencies=[Depends(get_current_superuser)])
@docs_router.get("/docs", include_in_schema=False)
async def get_swagger_documentation() -> fastapi.responses.HTMLResponse:
return get_swagger_ui_html(openapi_url="/openapi.json", title="docs")
@docs_router.get("/redoc", include_in_schema=False)
async def get_redoc_documentation() -> fastapi.responses.HTMLResponse:
return get_redoc_html(openapi_url="/openapi.json", title="docs")
@docs_router.get("/openapi.json", include_in_schema=False)
async def openapi() -> dict[str, Any]:
out: dict = get_openapi(title=application.title, version=application.version, routes=application.routes)
return out
application.include_router(docs_router)
Are you using the production environment?
I was using the "local" in the .env
config.
# ------------- environment -------------
ENVIRONMENT="local"
As an interim solution, i added the line highlighted below to setup.py
. I hope it is okay.
if isinstance(settings, EnvironmentSettings):
if settings.ENVIRONMENT != EnvironmentOption.PRODUCTION:
docs_router = APIRouter()
if settings.ENVIRONMENT != EnvironmentOption.LOCAL:
docs_router = APIRouter(dependencies=[Depends(get_current_superuser)])
...
...
application.include_router(docs_router)
# Adding router object for swagger docs
application.include_router(router)
Can you please send me your docker-compose, Dockerfile and .env (if .env is not possible, just list what it should contain). I'll try to replicate it here to see what's going on.
Same for me. Doing some debbuging
@igorbenav
https://github.com/igorbenav/FastAPI-boilerplate/commit/2a60325acbe9a82cbcd70266d8f183e7c7a5e260
in this commit we lost application.include_router(router)
ill upload a fix now
Nice one, @luca-medeiros!
Thanks @luca-medeiros and @igorbenav!
Describe the bug or question The swagger documentation is not appearing while running the app locally.
To Reproduce
Start the application locally using the below command and open the following URL in a browser: http://127.0.0.1:8000/docs
Description On opening the swagger endpoint, the following message is shown on the browser: "No operations defined in spec!"
Screenshots