Feature Request: make customizing the swagger-ui version easier #10524
Unanswered
mvanderlee
asked this question in
Questions
Replies: 2 comments
-
|
Workaround from fastapi import FastAPI, Request
from fastapi.openapi.docs import get_swagger_ui_html, get_swagger_ui_oauth2_redirect_html
from fastapi.responses import HTMLResponse
def init_openapi(app: FastAPI):
# https://github.com/tiangolo/fastapi/discussions/10524
# Copied from FastAPI, and customized the version
async def swagger_ui_html(req: Request) -> HTMLResponse:
root_path = req.scope.get("root_path", "").rstrip("/")
openapi_url = root_path + app.openapi_url
oauth2_redirect_url = app.swagger_ui_oauth2_redirect_url
if oauth2_redirect_url:
oauth2_redirect_url = root_path + oauth2_redirect_url
return get_swagger_ui_html(
openapi_url=openapi_url,
title=app.title + " - Swagger UI",
oauth2_redirect_url=oauth2_redirect_url,
init_oauth=app.swagger_ui_init_oauth,
swagger_ui_parameters=app.swagger_ui_parameters,
swagger_js_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui-bundle.js",
swagger_css_url="https://cdn.jsdelivr.net/npm/swagger-ui-dist@5.9.0/swagger-ui.css",
)
app.add_route('/docs', swagger_ui_html, include_in_schema=False)
if app.swagger_ui_oauth2_redirect_url:
async def swagger_ui_redirect(req: Request) -> HTMLResponse:
return get_swagger_ui_oauth2_redirect_html()
app.add_route(
app.swagger_ui_oauth2_redirect_url,
swagger_ui_redirect,
include_in_schema=False,
)
app = FastAPI(
docs_url=None, # https://github.com/tiangolo/fastapi/discussions/10524
)
init_openapi(app) |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
https://github.com/swagger-api/swagger-ui/releases/tag/v5.9.2 was released with this fix, can a new FastAPI be published with this update? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
First Check
Commit to Help
Example Code
Description
Yesterday Swagger-UI released a 5.9.1 which broke swagger docs for us.
FastAPI should make it easier for me to specify which version it uses. Adding
swagger_ui_version: str = '5'to the FastAPI object would be easiest, that way I can simply specifyswagger_ui_version=5.9.0'to revert to an older version.Right now there is a workaround, but it's cumbersome.
Operating System
Windows
Operating System Details
NA
FastAPI Version
0.103.1
Pydantic Version
2.3.0
Python Version
3.11.3
Additional Context
swagger-api/swagger-ui#9337
Beta Was this translation helpful? Give feedback.
All reactions