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:
- Image is taken from SwaggerUI docs
How can I achieve this?
1
Schemes is only available with swagger 2.0 (https://swagger.io/docs/specification/2-0/basic-structure/)
Now, with current version of FastAPI, it’s using swagger 3.0 and it’s using Servers (https://swagger.io/docs/specification/basic-structure/) for adding an option to select schemes.
def custom_openapi():
if app.openapi_schema:
return app.openapi_schema
openapi_schema = get_openapi(
title="Custom title",
version="2.5.0",
summary="This is a very custom OpenAPI schema",
description="Here's a longer description of the custom **OpenAPI** schema",
routes=app.routes,
)
openapi_schema["servers"] = [
{
"url": "http://api.example.com/v1",
"description": "Main (production) server"
},
{
"url": "http://staging-api.example.com",
"description": "Internal staging server for testing"
}
]
openapi_schema["info"]["x-logo"] = {
"url": "https://fastapi.tiangolo.com/img/logo-margin/logo-teal.png"
}
app.openapi_schema = openapi_schema
return app.openapi_schema
Result: