Browse Source

Merge 841c5c3189 into 460f8d2cc8

pull/14762/merge
Yurii Motov 12 hours ago
committed by GitHub
parent
commit
27bc286598
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 3
      docs/en/docs/how-to/configure-swagger-ui.md
  2. 39
      fastapi/openapi/docs.py
  3. 3
      tests/test_tutorial/test_configure_swagger_ui/test_tutorial001.py
  4. 3
      tests/test_tutorial/test_configure_swagger_ui/test_tutorial002.py
  5. 3
      tests/test_tutorial/test_configure_swagger_ui/test_tutorial003.py

3
docs/en/docs/how-to/configure-swagger-ui.md

@ -60,8 +60,7 @@ FastAPI also includes these JavaScript-only `presets` settings:
```JavaScript
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
SwaggerUIBundle.presets.apis
]
```

39
fastapi/openapi/docs.py

@ -77,6 +77,14 @@ def get_swagger_ui_html(
"""
),
] = "https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-bundle.js",
swagger_extra_js_urls: Annotated[
Optional[list[str]],
Doc(
"""
The URLs of additional JavaScript files to include.
"""
),
] = None,
swagger_css_url: Annotated[
str,
Doc(
@ -133,6 +141,14 @@ def get_swagger_ui_html(
"""
),
] = None,
swagger_extra_presets: Annotated[
Optional[list[str]],
Doc(
"""
Extra presets to add to Swagger UI.
"""
),
] = None,
) -> HTMLResponse:
"""
Generate and return the HTML that loads Swagger UI for the interactive
@ -149,6 +165,13 @@ def get_swagger_ui_html(
if swagger_ui_parameters:
current_swagger_ui_parameters.update(swagger_ui_parameters)
js_urls = [swagger_js_url]
if swagger_extra_js_urls:
js_urls.extend(swagger_extra_js_urls)
scripts_str = "\n ".join(
f'<script src="{js_url}"></script>' for js_url in js_urls
)
html = f"""
<!DOCTYPE html>
<html>
@ -161,7 +184,7 @@ def get_swagger_ui_html(
<body>
<div id="swagger-ui">
</div>
<script src="{swagger_js_url}"></script>
{scripts_str}
<!-- `SwaggerUIBundle` is now available on the page -->
<script>
const ui = SwaggerUIBundle({{
@ -174,12 +197,18 @@ def get_swagger_ui_html(
if oauth2_redirect_url:
html += f"oauth2RedirectUrl: window.location.origin + '{oauth2_redirect_url}',"
html += """
presets = ["SwaggerUIBundle.presets.apis"]
if swagger_extra_presets:
presets.extend(swagger_extra_presets)
presets_str = ",\n ".join(presets)
html += f"""
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
{presets_str},
],
})"""
"""
html += " })"
if init_oauth:
html += f"""

3
tests/test_tutorial/test_configure_swagger_ui/test_tutorial001.py

@ -18,9 +18,6 @@ def test_swagger_ui():
assert "SwaggerUIBundle.presets.apis," in response.text, (
"default configs should be preserved"
)
assert "SwaggerUIBundle.SwaggerUIStandalonePreset" in response.text, (
"default configs should be preserved"
)
assert '"layout": "BaseLayout",' in response.text, (
"default configs should be preserved"
)

3
tests/test_tutorial/test_configure_swagger_ui/test_tutorial002.py

@ -21,9 +21,6 @@ def test_swagger_ui():
assert "SwaggerUIBundle.presets.apis," in response.text, (
"default configs should be preserved"
)
assert "SwaggerUIBundle.SwaggerUIStandalonePreset" in response.text, (
"default configs should be preserved"
)
assert '"layout": "BaseLayout",' in response.text, (
"default configs should be preserved"
)

3
tests/test_tutorial/test_configure_swagger_ui/test_tutorial003.py

@ -24,9 +24,6 @@ def test_swagger_ui():
assert "SwaggerUIBundle.presets.apis," in response.text, (
"default configs should be preserved"
)
assert "SwaggerUIBundle.SwaggerUIStandalonePreset" in response.text, (
"default configs should be preserved"
)
assert '"layout": "BaseLayout",' in response.text, (
"default configs should be preserved"
)

Loading…
Cancel
Save