Browse Source

Add ReDoc parameters support

test: add test for redoc parameters

test: split tests into separate files

chore: add suggested changes to docstring
pull/12955/head
Robert Stein 9 months ago
parent
commit
ed7afae7b8
No known key found for this signature in database GPG Key ID: 5FF6BC0BCE162A99
  1. 14
      fastapi/applications.py
  2. 17
      fastapi/openapi/docs.py
  3. 14
      tests/test_local_docs.py

14
fastapi/applications.py

@ -444,6 +444,15 @@ class FastAPI(Starlette):
"""
),
] = "/redoc",
redoc_ui_parameters: Annotated[
Optional[Dict[str, Any]],
Doc(
"""
Parameters to configure ReDoc documentation (by default at `/redocs`).
Use kebab-case for [parameters](https://redocly.com/docs/redoc/config).
"""
),
] = None,
swagger_ui_oauth2_redirect_url: Annotated[
Optional[str],
Doc(
@ -833,6 +842,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 +1053,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>

14
tests/test_local_docs.py

@ -65,3 +65,17 @@ 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

Loading…
Cancel
Save