From 1caa0ecc39cee8fb5da74aa6c6753a2ea6707108 Mon Sep 17 00:00:00 2001 From: Robert Stein Date: Tue, 19 Nov 2024 19:51:32 +0100 Subject: [PATCH] :sparkles: Add ReDoc parameters support --- fastapi/applications.py | 17 ++++++++++++++++- fastapi/openapi/docs.py | 17 ++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/fastapi/applications.py b/fastapi/applications.py index 6d427cdc2..78203a357 100644 --- a/fastapi/applications.py +++ b/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) diff --git a/fastapi/openapi/docs.py b/fastapi/openapi/docs.py index c2ec358d2..10f52709f 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( - +