docs_url: Optional[str] = "/docs",
redoc_url: Optional[str] = "/redoc",
swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect",
+ swagger_ui_init_oauth: Optional[dict] = None,
**extra: Dict[str, Any],
) -> None:
self.default_response_class = default_response_class
self.docs_url = docs_url
self.redoc_url = redoc_url
self.swagger_ui_oauth2_redirect_url = swagger_ui_oauth2_redirect_url
+ self.swagger_ui_init_oauth = swagger_ui_init_oauth
self.extra = extra
self.dependency_overrides: Dict[Callable, Callable] = {}
openapi_url=openapi_url,
title=self.title + " - Swagger UI",
oauth2_redirect_url=self.swagger_ui_oauth2_redirect_url,
+ init_oauth=self.swagger_ui_init_oauth,
)
self.add_route(self.docs_url, swagger_ui_html, include_in_schema=False)
+import json
from typing import Optional
+from fastapi.encoders import jsonable_encoder
from starlette.responses import HTMLResponse
swagger_css_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css",
swagger_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
oauth2_redirect_url: Optional[str] = None,
+ init_oauth: Optional[dict] = None,
) -> HTMLResponse:
html = f"""
],
layout: "BaseLayout",
deepLinking: true
- })
+ })"""
+
+ if init_oauth:
+ html += f"""
+ ui.initOAuth({json.dumps(jsonable_encoder(init_oauth))})
+ """
+
+ html += """
</script>
</body>
</html>
--- /dev/null
+from fastapi import FastAPI
+from starlette.testclient import TestClient
+
+swagger_ui_init_oauth = {"clientId": "the-foo-clients", "appName": "The Predendapp"}
+
+app = FastAPI(swagger_ui_init_oauth=swagger_ui_init_oauth)
+
+
+@app.get("/items/")
+async def read_items():
+ return {"id": "foo"}
+
+
+client = TestClient(app)
+
+
+def test_swagger_ui():
+ response = client.get("/docs")
+ assert response.status_code == 200
+ print(response.text)
+ assert f"ui.initOAuth" in response.text
+ assert f'"appName": "The Predendapp"' in response.text
+ assert f'"clientId": "the-foo-clients"' in response.text
+
+
+def test_response():
+ response = client.get("/items/")
+ assert response.json() == {"id": "foo"}