Browse Source

Merge 83df9332c3 into 313723494b

pull/13695/merge
Valentin 2 days ago
committed by GitHub
parent
commit
0e8e04eea1
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 2
      docs/de/docs/how-to/configure-swagger-ui.md
  2. 2
      docs/en/docs/how-to/configure-swagger-ui.md
  3. 2
      docs/es/docs/how-to/configure-swagger-ui.md
  4. 2
      docs/ko/docs/how-to/configure-swagger-ui.md
  5. 2
      docs/pt/docs/how-to/configure-swagger-ui.md
  6. 2
      docs/zh/docs/how-to/configure-swagger-ui.md
  7. 36
      fastapi/openapi/docs.py
  8. 2
      tests/test_tutorial/test_configure_swagger_ui/test_tutorial001.py
  9. 2
      tests/test_tutorial/test_configure_swagger_ui/test_tutorial002.py
  10. 2
      tests/test_tutorial/test_configure_swagger_ui/test_tutorial003.py

2
docs/de/docs/how-to/configure-swagger-ui.md

@ -61,7 +61,7 @@ FastAPI umfasst auch diese Nur-JavaScript-`presets`-Einstellungen:
```JavaScript
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
SwaggerUIStandalonePreset
]
```

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

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

2
docs/es/docs/how-to/configure-swagger-ui.md

@ -61,7 +61,7 @@ FastAPI también incluye estas configuraciones `presets` solo de JavaScript:
```JavaScript
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
SwaggerUIStandalonePreset
]
```

2
docs/ko/docs/how-to/configure-swagger-ui.md

@ -61,7 +61,7 @@ FastAPI는 이러한 JavaScript 전용 `presets` 설정을 포함하고 있습
```JavaScript
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
SwaggerUIStandalonePreset
]
```

2
docs/pt/docs/how-to/configure-swagger-ui.md

@ -61,7 +61,7 @@ O FastAPI também inclui estas configurações de `predefinições` somente para
```JavaScript
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
SwaggerUIStandalonePreset
]
```

2
docs/zh/docs/how-to/configure-swagger-ui.md

@ -61,7 +61,7 @@ FastAPI 包含这些 JavaScript-only 的 `presets` 设置:
```JavaScript
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
SwaggerUIStandalonePreset
]
```

36
fastapi/openapi/docs.py

@ -1,5 +1,5 @@
import json
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, Set
from fastapi.encoders import jsonable_encoder
from starlette.responses import HTMLResponse
@ -23,6 +23,18 @@ swagger_ui_default_parameters: Annotated[
}
swagger_ui_default_scripts: Annotated[
Set[str],
Doc(
"""
Default JS scripts for Swagger UI.
You can use it as a template to add any other configurations needed.
"""
),
] = {"https://cdn.jsdelivr.net/npm/swagger-ui-dist@5/swagger-ui-standalone-preset.js"}
def get_swagger_ui_html(
*,
openapi_url: Annotated[
@ -98,6 +110,16 @@ def get_swagger_ui_html(
"""
),
] = None,
swagger_ui_scripts: Annotated[
Optional[Set[str]],
Doc(
"""
Configuration scripts for Swagger UI.
It defaults to [swagger_ui_default_scripts][fastapi.openapi.docs.swagger_ui_default_scripts].
"""
),
] = None,
) -> HTMLResponse:
"""
Generate and return the HTML that loads Swagger UI for the interactive
@ -114,6 +136,10 @@ def get_swagger_ui_html(
if swagger_ui_parameters:
current_swagger_ui_parameters.update(swagger_ui_parameters)
current_swagger_ui_scripts = swagger_ui_default_scripts.copy()
if swagger_ui_scripts:
current_swagger_ui_scripts.update(swagger_ui_scripts)
html = f"""
<!DOCTYPE html>
<html>
@ -126,6 +152,12 @@ def get_swagger_ui_html(
<div id="swagger-ui">
</div>
<script src="{swagger_js_url}"></script>
"""
for elem in current_swagger_ui_scripts:
html += f"<script src={json.dumps(elem)}></script>\n"
html += f"""
<!-- `SwaggerUIBundle` is now available on the page -->
<script>
const ui = SwaggerUIBundle({{
@ -141,7 +173,7 @@ def get_swagger_ui_html(
html += """
presets: [
SwaggerUIBundle.presets.apis,
SwaggerUIBundle.SwaggerUIStandalonePreset
SwaggerUIStandalonePreset
],
})"""

2
tests/test_tutorial/test_configure_swagger_ui/test_tutorial001.py

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

2
tests/test_tutorial/test_configure_swagger_ui/test_tutorial002.py

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

2
tests/test_tutorial/test_configure_swagger_ui/test_tutorial003.py

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

Loading…
Cancel
Save