Skip to content

Commit

Permalink
fix: Fixed oauth2 redirect bug for versioned routes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexschimpf authored Mar 5, 2024
1 parent c1ec66a commit 17f0172
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions fastapi_versionizer/versionizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from enum import Enum
from fastapi import FastAPI, APIRouter
from fastapi.openapi.docs import get_redoc_html
from fastapi.openapi.docs import get_swagger_ui_html
from fastapi.openapi.docs import get_swagger_ui_html, get_swagger_ui_oauth2_redirect_html
import fastapi.openapi.utils
from fastapi.responses import HTMLResponse, JSONResponse
from fastapi.routing import APIRoute, APIWebSocketRoute
Expand Down Expand Up @@ -258,17 +258,25 @@ async def get_openapi() -> Any:
return fastapi.openapi.utils.get_openapi(**openapi_params)

if self._include_version_docs and self._app.docs_url is not None and self._app.openapi_url is not None:
openapi_url = self._build_api_url(version_prefix, self._app.openapi_url)
oauth2_redirect_url = self._build_api_url(
version_prefix, cast(str, self._app.swagger_ui_oauth2_redirect_url))

@router.get(self._app.docs_url, include_in_schema=False)
async def get_docs() -> HTMLResponse:
openapi_url = self._build_api_url(version_prefix, cast(str, self._app.openapi_url))
return get_swagger_ui_html(
openapi_url=openapi_url,
title=title,
swagger_ui_parameters=self._app.swagger_ui_parameters,
init_oauth=self._app.swagger_ui_init_oauth,
oauth2_redirect_url=self._app.swagger_ui_oauth2_redirect_url
oauth2_redirect_url=oauth2_redirect_url
)

if self._app.swagger_ui_oauth2_redirect_url:
@router.get(self._app.swagger_ui_oauth2_redirect_url, include_in_schema=False)
async def get_oauth2_redirect() -> HTMLResponse:
return get_swagger_ui_oauth2_redirect_html()

if self._include_version_docs and self._app.redoc_url is not None and self._app.openapi_url is not None:
@router.get(self._app.redoc_url, include_in_schema=False)
async def get_redoc() -> HTMLResponse:
Expand Down Expand Up @@ -343,7 +351,11 @@ def _add_route_to_router(
def _strip_routes(self) -> None:
paths_to_keep = []
if self._include_main_docs:
paths_to_keep.extend([self._app.docs_url, self._app.redoc_url])
paths_to_keep.extend([
self._app.docs_url,
self._app.redoc_url,
self._app.swagger_ui_oauth2_redirect_url
])
if self._include_main_openapi_route:
paths_to_keep.append(self._app.openapi_url)

Expand Down

0 comments on commit 17f0172

Please sign in to comment.