Skip to content

Commit 8505b71

Browse files
zamiramirtiangolo
authored andcommitted
✨ Add support for setting Swagger UI initOAuth configs (clientId, appName) (fastapi#499)
1 parent 78272ac commit 8505b71

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed

fastapi/applications.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ def __init__(
3737
docs_url: Optional[str] = "/docs",
3838
redoc_url: Optional[str] = "/redoc",
3939
swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect",
40+
swagger_ui_init_oauth: Optional[dict] = None,
4041
**extra: Dict[str, Any],
4142
) -> None:
4243
self.default_response_class = default_response_class
@@ -57,6 +58,7 @@ def __init__(
5758
self.docs_url = docs_url
5859
self.redoc_url = redoc_url
5960
self.swagger_ui_oauth2_redirect_url = swagger_ui_oauth2_redirect_url
61+
self.swagger_ui_init_oauth = swagger_ui_init_oauth
6062
self.extra = extra
6163
self.dependency_overrides: Dict[Callable, Callable] = {}
6264

@@ -98,6 +100,7 @@ async def swagger_ui_html(req: Request) -> HTMLResponse:
98100
openapi_url=openapi_url,
99101
title=self.title + " - Swagger UI",
100102
oauth2_redirect_url=self.swagger_ui_oauth2_redirect_url,
103+
init_oauth=self.swagger_ui_init_oauth,
101104
)
102105

103106
self.add_route(self.docs_url, swagger_ui_html, include_in_schema=False)

fastapi/openapi/docs.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1+
import json
12
from typing import Optional
23

4+
from fastapi.encoders import jsonable_encoder
35
from starlette.responses import HTMLResponse
46

57

@@ -11,6 +13,7 @@ def get_swagger_ui_html(
1113
swagger_css_url: str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css",
1214
swagger_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
1315
oauth2_redirect_url: Optional[str] = None,
16+
init_oauth: Optional[dict] = None,
1417
) -> HTMLResponse:
1518

1619
html = f"""
@@ -42,7 +45,14 @@ def get_swagger_ui_html(
4245
],
4346
layout: "BaseLayout",
4447
deepLinking: true
45-
})
48+
})"""
49+
50+
if init_oauth:
51+
html += f"""
52+
ui.initOAuth({json.dumps(jsonable_encoder(init_oauth))})
53+
"""
54+
55+
html += """
4656
</script>
4757
</body>
4858
</html>

tests/test_swagger_ui_init_oauth.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from fastapi import FastAPI
2+
from starlette.testclient import TestClient
3+
4+
swagger_ui_init_oauth = {"clientId": "the-foo-clients", "appName": "The Predendapp"}
5+
6+
app = FastAPI(swagger_ui_init_oauth=swagger_ui_init_oauth)
7+
8+
9+
@app.get("/items/")
10+
async def read_items():
11+
return {"id": "foo"}
12+
13+
14+
client = TestClient(app)
15+
16+
17+
def test_swagger_ui():
18+
response = client.get("/docs")
19+
assert response.status_code == 200
20+
print(response.text)
21+
assert f"ui.initOAuth" in response.text
22+
assert f'"appName": "The Predendapp"' in response.text
23+
assert f'"clientId": "the-foo-clients"' in response.text
24+
25+
26+
def test_response():
27+
response = client.get("/items/")
28+
assert response.json() == {"id": "foo"}

0 commit comments

Comments
 (0)