From ed7afae7b863db2c92bfeabe1f472e12d6e154e1 Mon Sep 17 00:00:00 2001 From: Robert Stein Date: Tue, 19 Nov 2024 19:51:32 +0100 Subject: [PATCH 1/3] :sparkles: Add ReDoc parameters support test: add test for redoc parameters test: split tests into separate files chore: add suggested changes to docstring --- fastapi/applications.py | 14 +++++++++++++- fastapi/openapi/docs.py | 17 ++++++++++++++++- tests/test_local_docs.py | 14 ++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index 05c7bd2be..eb802ad84 100644 --- a/fastapi/applications.py +++ b/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) diff --git a/fastapi/openapi/docs.py b/fastapi/openapi/docs.py index f181b43c1..73eb477d9 100644 --- a/fastapi/openapi/docs.py +++ b/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""" @@ -245,7 +260,7 @@ def get_redoc_html( - + diff --git a/tests/test_local_docs.py b/tests/test_local_docs.py index 5f102edf1..fde018d89 100644 --- a/tests/test_local_docs.py +++ b/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 From 1e9741df66c95792c33a7078866cb822672de006 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 16 Jul 2025 16:40:50 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/openapi/docs.py | 2 +- tests/test_local_docs.py | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/fastapi/openapi/docs.py b/fastapi/openapi/docs.py index 73eb477d9..af2e10f30 100644 --- a/fastapi/openapi/docs.py +++ b/fastapi/openapi/docs.py @@ -229,7 +229,7 @@ def get_redoc_html( config_string = "" if redoc_ui_parameters: for key, value in redoc_ui_parameters.items(): - config_string += f" {key}=\"{value}\"" + config_string += f' {key}="{value}"' html = f""" diff --git a/tests/test_local_docs.py b/tests/test_local_docs.py index fde018d89..cddd1dee8 100644 --- a/tests/test_local_docs.py +++ b/tests/test_local_docs.py @@ -69,13 +69,16 @@ def test_google_fonts_in_generated_redoc(): 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"} + 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 + 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 + assert 'disable-search="true"' not in body_without_parameters From 1c816ab5e085429d5d50d34fca7223a07e338869 Mon Sep 17 00:00:00 2001 From: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com> Date: Thu, 24 Jul 2025 12:31:07 +0200 Subject: [PATCH 3/3] Fix typo (`/redocs` instead of `/redoc`) --- fastapi/applications.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index eb802ad84..d58ae79ba 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -448,7 +448,7 @@ class FastAPI(Starlette): Optional[Dict[str, Any]], Doc( """ - Parameters to configure ReDoc documentation (by default at `/redocs`). + Parameters to configure ReDoc documentation (by default at `/redoc`). Use kebab-case for [parameters](https://redocly.com/docs/redoc/config). """ ),