Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'swagger_ui_expansion' option in 'FastAPI()' #3471

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions fastapi/applications.py
Expand Up @@ -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[
Expand Down Expand Up @@ -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]] = {}

Expand Down Expand Up @@ -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)
Expand Down
8 changes: 5 additions & 3 deletions fastapi/openapi/docs.py
Expand Up @@ -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"""
Expand All @@ -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,
Expand All @@ -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"""
Expand Down