Browse Source

Add ReDoc parameters support

pull/12955/head
Robert Stein 5 months ago
parent
commit
1caa0ecc39
No known key found for this signature in database GPG Key ID: 5FF6BC0BCE162A99
  1. 17
      fastapi/applications.py
  2. 17
      fastapi/openapi/docs.py

17
fastapi/applications.py

@ -444,6 +444,18 @@ class FastAPI(Starlette):
"""
),
] = "/redoc",
redoc_ui_parameters: Annotated[
Optional[Dict[str, Any]],
Doc(
"""
Parameters to configure ReDoc documentation (by default at `/docs`).
Use kebab-case for [parameters](https://redocly.com/docs/redoc/config).
Read more about it in the
[FastAPI docs for Metadata and Docs URLs](https://fastapi.tiangolo.com/tutorial/metadata/#docs-urls).
"""
),
] = None,
swagger_ui_oauth2_redirect_url: Annotated[
Optional[str],
Doc(
@ -833,6 +845,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
@ -1043,7 +1056,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)

17
fastapi/openapi/docs.py

@ -205,6 +205,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
@ -216,6 +226,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>
@ -245,7 +260,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>

Loading…
Cancel
Save