]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:sparkles: Add support for setting Swagger UI initOAuth configs (clientId, appName...
authorZamir Amir <40475662+zamiramir@users.noreply.github.com>
Thu, 3 Oct 2019 23:41:04 +0000 (01:41 +0200)
committerSebastián Ramírez <tiangolo@gmail.com>
Thu, 3 Oct 2019 23:41:04 +0000 (18:41 -0500)
fastapi/applications.py
fastapi/openapi/docs.py
tests/test_swagger_ui_init_oauth.py [new file with mode: 0644]

index 28cbd319b79e99e0d4a7ebe335dc92598a17ede4..143c5c64b3a6ab0c281fdb9a70c760b928636491 100644 (file)
@@ -37,6 +37,7 @@ class FastAPI(Starlette):
         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
@@ -57,6 +58,7 @@ class FastAPI(Starlette):
         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] = {}
 
@@ -98,6 +100,7 @@ class FastAPI(Starlette):
                     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)
index 024ae269fa67d2331a61f74a94bb5334ec1137b3..9aa09afff43772086f5d362fee44116a11561746 100644 (file)
@@ -1,5 +1,7 @@
+import json
 from typing import Optional
 
+from fastapi.encoders import jsonable_encoder
 from starlette.responses import HTMLResponse
 
 
@@ -11,6 +13,7 @@ def get_swagger_ui_html(
     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"""
@@ -42,7 +45,14 @@ def get_swagger_ui_html(
         ],
         layout: "BaseLayout",
         deepLinking: true
-    })
+    })"""
+
+    if init_oauth:
+        html += f"""
+        ui.initOAuth({json.dumps(jsonable_encoder(init_oauth))})
+        """
+
+    html += """
     </script>
     </body>
     </html>
diff --git a/tests/test_swagger_ui_init_oauth.py b/tests/test_swagger_ui_init_oauth.py
new file mode 100644 (file)
index 0000000..37af939
--- /dev/null
@@ -0,0 +1,28 @@
+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"}