File tree Expand file tree Collapse file tree 3 files changed +42
-1
lines changed Expand file tree Collapse file tree 3 files changed +42
-1
lines changed Original file line number Diff line number Diff line change @@ -37,6 +37,7 @@ def __init__(
37
37
docs_url : Optional [str ] = "/docs" ,
38
38
redoc_url : Optional [str ] = "/redoc" ,
39
39
swagger_ui_oauth2_redirect_url : Optional [str ] = "/docs/oauth2-redirect" ,
40
+ swagger_ui_init_oauth : Optional [dict ] = None ,
40
41
** extra : Dict [str , Any ],
41
42
) -> None :
42
43
self .default_response_class = default_response_class
@@ -57,6 +58,7 @@ def __init__(
57
58
self .docs_url = docs_url
58
59
self .redoc_url = redoc_url
59
60
self .swagger_ui_oauth2_redirect_url = swagger_ui_oauth2_redirect_url
61
+ self .swagger_ui_init_oauth = swagger_ui_init_oauth
60
62
self .extra = extra
61
63
self .dependency_overrides : Dict [Callable , Callable ] = {}
62
64
@@ -98,6 +100,7 @@ async def swagger_ui_html(req: Request) -> HTMLResponse:
98
100
openapi_url = openapi_url ,
99
101
title = self .title + " - Swagger UI" ,
100
102
oauth2_redirect_url = self .swagger_ui_oauth2_redirect_url ,
103
+ init_oauth = self .swagger_ui_init_oauth ,
101
104
)
102
105
103
106
self .add_route (self .docs_url , swagger_ui_html , include_in_schema = False )
Original file line number Diff line number Diff line change
1
+ import json
1
2
from typing import Optional
2
3
4
+ from fastapi .encoders import jsonable_encoder
3
5
from starlette .responses import HTMLResponse
4
6
5
7
@@ -11,6 +13,7 @@ def get_swagger_ui_html(
11
13
swagger_css_url : str = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@3/swagger-ui.css" ,
12
14
swagger_favicon_url : str = "https://fastapi.tiangolo.com/img/favicon.png" ,
13
15
oauth2_redirect_url : Optional [str ] = None ,
16
+ init_oauth : Optional [dict ] = None ,
14
17
) -> HTMLResponse :
15
18
16
19
html = f"""
@@ -42,7 +45,14 @@ def get_swagger_ui_html(
42
45
],
43
46
layout: "BaseLayout",
44
47
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 += """
46
56
</script>
47
57
</body>
48
58
</html>
Original file line number Diff line number Diff line change
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" }
You can’t perform that action at this time.
0 commit comments