Serving openapi.json from subpath
My backend is served from subpath /api, so urls look like this:
/api/search
/apu/users
/api/audit
So if I open /api/openapi.json I get the OpenAPI spec. And if I open /api/docs I get the swagger UI but it shows error:
Because it tries to load /openapi.json which is served by frontend.
Settings spec_path to something like /api/openapi.json does not help, because then spec url becomes /api/api/openapi.json and swagger tries to get it from /api/openapi.json which is 404 in my app.
Any way to serve spec from subpath?
So, what I've achieved so far:
app = APIFlask(__name__, instance_relative_config=True, spec_path="/api/spec")
app.config["LOCAL_SPEC_PATH"] = "openapi.json"
app.config["SYNC_LOCAL_SPEC"] = True
@app.route("/spec")
def spec():
with open("openapi.json", "r") as f:
return f.read(), 200, {"content-type": "application/json"}
Now I can open swagger UI without errors, however all request from swagger to backend are also not prefixed i.e. /audit and not /api/audit.
Can you provide a minimal case?
My current solution for now that works:
application = APIFlask(
__name__,
instance_relative_config=True,
spec_path="/api/spec",
enable_openapi=settings.DEBUG,
docs_ui="rapidoc"
)
@application.route("/spec")
def spec():
return redirect("/api/api/spec")
So, I'm redirecting requests from /api/spec to /api/api/spec so that swagger can reach openapi.json. Looks hacky but works.