[Python] Add additional scheme in FastAPI docs (HTTP or HTTPS)

Discussão em 'Python' iniciado por Stack, Setembro 11, 2024.

  1. Stack

    Stack Membro Participativo

    I am making some fastAPI routes which automatically generates the documentation, In documentation I want to add an option to select schemes (HTTP or HTTPS).

    I tried adding CORS to the API:

    app = FastAPI()

    # Implement CORS
    origins = [
    "http://localhost",
    "http://localhost:8000",
    "https://example.com"
    ]

    app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
    )


    But the schemes aren't showing up.

    Also added custom config:

    def custom_openapi():
    if app.openapi_schema:
    return app.openapi_schema
    openapi_schema = get_openapi(
    title="My FastAPI App",
    version="1.0.0",
    description="This is a FastAPI app that supports HTTP and HTTPS",
    routes=app.routes,
    )
    # Add support for both HTTP and HTTPS schemes
    openapi_schema["schemes"] = ["http", "https"]
    app.openapi_schema = openapi_schema
    return app.openapi_schema


    app.openapi = custom_openapi


    but this also didn't work.

    Expected result is to get this on the upper left of the docs:

    [​IMG]

    • Image is taken from SwaggerUI docs

    How can I achieve this?

    Continue reading...

Compartilhe esta Página