diff --git a/fastapi/applications.py b/fastapi/applications.py index 3f78238d60f2f..7315a27af535d 100644 --- a/fastapi/applications.py +++ b/fastapi/applications.py @@ -46,6 +46,7 @@ def __init__( redoc_url: Optional[str] = "/redoc", swagger_ui_oauth2_redirect_url: Optional[str] = "/docs/oauth2-redirect", swagger_ui_init_oauth: Optional[Dict[str, Any]] = None, + swagger_ui_expansion: str = "list", middleware: Optional[Sequence[Middleware]] = None, exception_handlers: Optional[ Dict[ @@ -114,6 +115,13 @@ def __init__( self.redoc_url = redoc_url self.swagger_ui_oauth2_redirect_url = swagger_ui_oauth2_redirect_url self.swagger_ui_init_oauth = swagger_ui_init_oauth + assert swagger_ui_expansion in ( + "none", + "list", + "full", + ), f"'swagger_ui_expansion' option must be in: none, list or full, not {swagger_ui_expansion}" + self.swagger_ui_expansion = swagger_ui_expansion + self.extra = extra self.dependency_overrides: Dict[Callable[..., Any], Callable[..., Any]] = {} @@ -165,6 +173,7 @@ async def swagger_ui_html(req: Request) -> HTMLResponse: title=self.title + " - Swagger UI", oauth2_redirect_url=oauth2_redirect_url, init_oauth=self.swagger_ui_init_oauth, + doc_expansion=self.swagger_ui_expansion, ) self.add_route(self.docs_url, swagger_ui_html, include_in_schema=False) diff --git a/fastapi/openapi/docs.py b/fastapi/openapi/docs.py index fd22e4e8c167d..9267ad7c29c66 100644 --- a/fastapi/openapi/docs.py +++ b/fastapi/openapi/docs.py @@ -14,6 +14,7 @@ def get_swagger_ui_html( swagger_favicon_url: str = "https://fastapi.tiangolo.com/img/favicon.png", oauth2_redirect_url: Optional[str] = None, init_oauth: Optional[Dict[str, Any]] = None, + doc_expansion: str = "list", ) -> HTMLResponse: html = f""" @@ -37,7 +38,7 @@ def get_swagger_ui_html( if oauth2_redirect_url: html += f"oauth2RedirectUrl: window.location.origin + '{oauth2_redirect_url}'," - html += """ + html += f""" dom_id: '#swagger-ui', presets: [ SwaggerUIBundle.presets.apis, @@ -46,8 +47,9 @@ def get_swagger_ui_html( layout: "BaseLayout", deepLinking: true, showExtensions: true, - showCommonExtensions: true - })""" + showCommonExtensions: true, + docExpansion: '{doc_expansion}' + }})""" if init_oauth: html += f"""