diff --git a/docs/de/docs/how-to/configure-swagger-ui.md b/docs/de/docs/how-to/configure-swagger-ui.md index 1ee72d205..ee0227ef2 100644 --- a/docs/de/docs/how-to/configure-swagger-ui.md +++ b/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 ] ``` diff --git a/docs/en/docs/how-to/configure-swagger-ui.md b/docs/en/docs/how-to/configure-swagger-ui.md index a8a8de48f..e995dc559 100644 --- a/docs/en/docs/how-to/configure-swagger-ui.md +++ b/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 ] ``` diff --git a/docs/es/docs/how-to/configure-swagger-ui.md b/docs/es/docs/how-to/configure-swagger-ui.md index 4243c191c..a896ca320 100644 --- a/docs/es/docs/how-to/configure-swagger-ui.md +++ b/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 ] ``` diff --git a/docs/ko/docs/how-to/configure-swagger-ui.md b/docs/ko/docs/how-to/configure-swagger-ui.md index 5a57342cf..2a61f4b39 100644 --- a/docs/ko/docs/how-to/configure-swagger-ui.md +++ b/docs/ko/docs/how-to/configure-swagger-ui.md @@ -61,7 +61,7 @@ FastAPI는 이러한 JavaScript 전용 `presets` 설정을 포함하고 있습 ```JavaScript presets: [ SwaggerUIBundle.presets.apis, - SwaggerUIBundle.SwaggerUIStandalonePreset + SwaggerUIStandalonePreset ] ``` diff --git a/docs/pt/docs/how-to/configure-swagger-ui.md b/docs/pt/docs/how-to/configure-swagger-ui.md index 915b2b5c5..a13a2bc8a 100644 --- a/docs/pt/docs/how-to/configure-swagger-ui.md +++ b/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 ] ``` diff --git a/docs/zh/docs/how-to/configure-swagger-ui.md b/docs/zh/docs/how-to/configure-swagger-ui.md index 108e0cb95..74f05bf09 100644 --- a/docs/zh/docs/how-to/configure-swagger-ui.md +++ b/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 ] ``` diff --git a/fastapi/openapi/docs.py b/fastapi/openapi/docs.py index f181b43c1..79904167b 100644 --- a/fastapi/openapi/docs.py +++ b/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""" @@ -126,6 +152,12 @@ def get_swagger_ui_html(
+ """ + + for elem in current_swagger_ui_scripts: + html += f"\n" + + html += f"""