igorbenav / FastAPI-boilerplate

An extendable async API using FastAPI, Pydantic V2, SQLAlchemy 2.0, PostgreSQL and Redis.
MIT License
632 stars 76 forks source link

Swagger documentation not getting generated #117

Closed jvijaybhaskar closed 9 months ago

jvijaybhaskar commented 9 months ago

Describe the bug or question The swagger documentation is not appearing while running the app locally.

To Reproduce

  1. Ensure the pre-requisites are up and running (db, redis)
  2. Create and add the suggested configuration in .env file for running the application locally
  3. Start the application locally using the below command and open the following URL in a browser: http://127.0.0.1:8000/docs

    poetry run uvicorn src.app.main:app --reload

Description On opening the swagger endpoint, the following message is shown on the browser: "No operations defined in spec!"

Screenshots

image
igorbenav commented 9 months ago

Are you using the production environment?

igorbenav commented 9 months ago

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:

If 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)
jvijaybhaskar commented 9 months ago

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)
igorbenav commented 9 months ago

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.

luca-medeiros commented 9 months ago

Same for me. Doing some debbuging

luca-medeiros commented 9 months ago

@igorbenav https://github.com/igorbenav/FastAPI-boilerplate/commit/2a60325acbe9a82cbcd70266d8f183e7c7a5e260 in this commit we lost application.include_router(router)

luca-medeiros commented 9 months ago

ill upload a fix now

igorbenav commented 9 months ago

Nice one, @luca-medeiros!

jvijaybhaskar commented 9 months ago

Thanks @luca-medeiros and @igorbenav!