FastAPI generates the "openapi.json" file and provides an interface to it.
For an experiment I need to replace this with a third party file.
from pathlib import Path
import json
app.openapi_schema = json.loads(Path(r"myopenapi.json").read_text())
when I put this code behind a endpoint, for example "/"
@app.get("/", include_in_schema=FULL_SCHEMA)
def read_root():
# code here
After calling the endpoint once, the loaded myopenapi.json is displayed for the "/docs" interface and the original is overwritten. The functionality has not changed, the old definitions still work.
I would like to be able to make the switch directly after FastAPI has completed the setup and all end points are created.
Putting this in the Startup code block doesn't work (async def lifespan(app: FastAPI):) - when this reaches yield the app.openapi _schema is not created yet.
Where is the right place to change the FastAPI app after generation?
FastAPI is started with the command:
uvicorn.run(app, host=SERVER_HOST, port=SERVER_PORT, workers=1)