Swagger2 support for FastAPI
Reason behind this library:
Few API GW services like Google Cloud API GW still support only Swagger 2.0 spec. Since FastAPI only supports OAS3, it is a challenge. Converting from OAS3 to Swagger 2.0 requires some manual steps which would hinder CI/CD.
Python 3.8+
- 0.0.3 - FastAPI >= 0.79.0, <= 0.98.0
- 0.1.1 - FastAPI >= 0.99.0, <= 0.99.1
- 0.2.4 - FastAPI >= 0.100.0
$ pip install fastapi_swagger2
from typing import Union
from fastapi import FastAPI
from fastapi_swagger2 import FastAPISwagger2
app = FastAPI()
FastAPISwagger2(app)
@app.get("/")
def read_root():
return {"Hello": "World"}
@app.get("/items/{item_id}")
def read_item(item_id: int, q: Union[str, None] = None):
return {"item_id": item_id, "q": q}
This adds following endpoints:
- http://localhost:8000/swagger2.json
- http://localhost:8000/swagger2/docs
- http://localhost:8000/swagger2/redoc
import os
import yaml
from app.main import app
URL = os.environ["CLOUD_RUN_URL"]
app.servers.append(URL)
spec = app.swagger2()
spec['x-google-backend'] = {'address': URL}
print(yaml.dump(spec))
$ pip install "/path/to/fastapi_swagger2/repo[test,all]"