diff --git a/fastapi/applications.py b/fastapi/applications.py index 4af1146b0d..d5829ad03c 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -1104,7 +1104,14 @@ class FastAPI(Starlette): async def openapi(req: Request) -> JSONResponse: root_path = req.scope.get("root_path", "").rstrip("/") schema = self.openapi() - if root_path and self.root_path_in_servers: + if self.root_path_in_servers and "servers" not in schema: + if root_path: + schema = dict(schema) + schema["servers"] = [{"url": root_path}] + else: + schema = dict(schema) + schema["servers"] = [{"url": "/"}] + elif root_path and self.root_path_in_servers: server_urls = {s.get("url") for s in schema.get("servers", [])} if root_path not in server_urls: schema = dict(schema)