mirror of https://github.com/tiangolo/fastapi.git
✨ Add support for setting Swagger UI initOAuth configs (clientId, appName) (#499)
This commit is contained in:
parent
78272ac1f3
commit
8505b716af
|
|
@ -37,6 +37,7 @@ class FastAPI(Starlette):
|
||||||
docs_url: Optional[str] = "/docs",
|
docs_url: Optional[str] = "/docs",
|
||||||
redoc_url: Optional[str] = "/redoc",
|
redoc_url: Optional[str] = "/redoc",
|
||||||
swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect",
|
swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect",
|
||||||
|
swagger_ui_init_oauth: Optional[dict] = None,
|
||||||
**extra: Dict[str, Any],
|
**extra: Dict[str, Any],
|
||||||
) -> None:
|
) -> None:
|
||||||
self.default_response_class = default_response_class
|
self.default_response_class = default_response_class
|
||||||
|
|
@ -57,6 +58,7 @@ class FastAPI(Starlette):
|
||||||
self.docs_url = docs_url
|
self.docs_url = docs_url
|
||||||
self.redoc_url = redoc_url
|
self.redoc_url = redoc_url
|
||||||
self.swagger_ui_oauth2_redirect_url = swagger_ui_oauth2_redirect_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.extra = extra
|
||||||
self.dependency_overrides: Dict[Callable, Callable] = {}
|
self.dependency_overrides: Dict[Callable, Callable] = {}
|
||||||
|
|
||||||
|
|
@ -98,6 +100,7 @@ class FastAPI(Starlette):
|
||||||
openapi_url=openapi_url,
|
openapi_url=openapi_url,
|
||||||
title=self.title + " - Swagger UI",
|
title=self.title + " - Swagger UI",
|
||||||
oauth2_redirect_url=self.swagger_ui_oauth2_redirect_url,
|
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)
|
self.add_route(self.docs_url, swagger_ui_html, include_in_schema=False)
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
|
import json
|
||||||
from typing import Optional
|
from typing import Optional
|
||||||
|
|
||||||
|
from fastapi.encoders import jsonable_encoder
|
||||||
from starlette.responses import HTMLResponse
|
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_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",
|
swagger_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png",
|
||||||
oauth2_redirect_url: Optional[str] = None,
|
oauth2_redirect_url: Optional[str] = None,
|
||||||
|
init_oauth: Optional[dict] = None,
|
||||||
) -> HTMLResponse:
|
) -> HTMLResponse:
|
||||||
|
|
||||||
html = f"""
|
html = f"""
|
||||||
|
|
@ -42,7 +45,14 @@ def get_swagger_ui_html(
|
||||||
],
|
],
|
||||||
layout: "BaseLayout",
|
layout: "BaseLayout",
|
||||||
deepLinking: true
|
deepLinking: true
|
||||||
})
|
})"""
|
||||||
|
|
||||||
|
if init_oauth:
|
||||||
|
html += f"""
|
||||||
|
ui.initOAuth({json.dumps(jsonable_encoder(init_oauth))})
|
||||||
|
"""
|
||||||
|
|
||||||
|
html += """
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
|
||||||
|
|
@ -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"}
|
||||||
Loading…
Reference in New Issue