This commit is contained in:
Robert Stein 2025-12-16 21:07:31 +00:00 committed by GitHub
commit 7e7edc73a6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 2 deletions

View File

@ -453,6 +453,15 @@ class FastAPI(Starlette):
"""
),
] = "/redoc",
redoc_ui_parameters: Annotated[
Optional[Dict[str, Any]],
Doc(
"""
Parameters to configure ReDoc documentation (by default at `/redoc`).
Use kebab-case for [parameters](https://redocly.com/docs/redoc/config).
"""
),
] = None,
swagger_ui_oauth2_redirect_url: Annotated[
Optional[str],
Doc(
@ -868,6 +877,7 @@ class FastAPI(Starlette):
self.root_path_in_servers = root_path_in_servers
self.docs_url = docs_url
self.redoc_url = redoc_url
self.redoc_ui_parameters = redoc_ui_parameters
self.swagger_ui_oauth2_redirect_url = swagger_ui_oauth2_redirect_url
self.swagger_ui_init_oauth = swagger_ui_init_oauth
self.swagger_ui_parameters = swagger_ui_parameters
@ -1128,7 +1138,9 @@ class FastAPI(Starlette):
root_path = req.scope.get("root_path", "").rstrip("/")
openapi_url = root_path + self.openapi_url
return get_redoc_html(
openapi_url=openapi_url, title=f"{self.title} - ReDoc"
openapi_url=openapi_url,
title=f"{self.title} - ReDoc",
redoc_ui_parameters=self.redoc_ui_parameters,
)
self.add_route(self.redoc_url, redoc_html, include_in_schema=False)

View File

@ -206,6 +206,16 @@ def get_redoc_html(
"""
),
] = True,
redoc_ui_parameters: Annotated[
Optional[Dict[str, Any]],
Doc(
"""
Configuration parameters for ReDoc
It defaults to None.
"""
),
] = None,
) -> HTMLResponse:
"""
Generate and return the HTML response that loads ReDoc for the alternative
@ -217,6 +227,11 @@ def get_redoc_html(
Read more about it in the
[FastAPI docs for Custom Docs UI Static Assets (Self-Hosting)](https://fastapi.tiangolo.com/how-to/custom-docs-ui-assets/).
"""
config_string = ""
if redoc_ui_parameters:
for key, value in redoc_ui_parameters.items():
config_string += f' {key}="{value}"'
html = f"""
<!DOCTYPE html>
<html>
@ -246,7 +261,7 @@ def get_redoc_html(
<noscript>
ReDoc requires Javascript to function. Please enable it to browse the documentation.
</noscript>
<redoc spec-url="{openapi_url}"></redoc>
<redoc spec-url="{openapi_url}"{config_string}></redoc>
<script src="{redoc_js_url}"> </script>
</body>
</html>

View File

@ -65,3 +65,20 @@ def test_google_fonts_in_generated_redoc():
openapi_url="/docs", title="title", with_google_fonts=False
).body.decode()
assert "fonts.googleapis.com" not in body_without_google_fonts
def test_generated_redoc_with_parameters():
body_with_parameters = get_redoc_html(
openapi_url="/docs",
title="title",
with_google_fonts=False,
redoc_ui_parameters={"disable-search": "true"},
).body.decode()
assert 'disable-search="true"' in body_with_parameters
def test_generated_redoc_without_parameters():
body_without_parameters = get_redoc_html(
openapi_url="/docs", title="title", with_google_fonts=False
).body.decode()
assert 'disable-search="true"' not in body_without_parameters