From caa368ad967717ae69d5cb0dc962c9011b9bb0a1 Mon Sep 17 00:00:00 2001 From: Daniel Morell Date: Fri, 12 Jul 2024 11:49:41 -0500 Subject: [PATCH] Fixed #398 FastAPI integration fails if docs are disabled. --- rollbar/contrib/fastapi/utils.py | 25 ++++++++++++++----------- 1 file changed, 14 insertions(+), 11 deletions(-) diff --git a/rollbar/contrib/fastapi/utils.py b/rollbar/contrib/fastapi/utils.py index db4902ec..daff1b2d 100644 --- a/rollbar/contrib/fastapi/utils.py +++ b/rollbar/contrib/fastapi/utils.py @@ -96,17 +96,20 @@ def get_installed_middlewares(app): return middlewares -def has_bare_routing(app_or_router): - expected_app_routes = 4 - expected_router_routes = 0 - - if ( - isinstance(app_or_router, FastAPI) - and expected_app_routes != len(app_or_router.routes) - ) or ( - isinstance(app_or_router, APIRouter) - and expected_router_routes != len(app_or_router.routes) - ): +def has_bare_routing(app_or_router: FastAPI | APIRouter): + if not isinstance(app_or_router, (FastAPI, APIRouter)): + return False + + urls = [ + getattr(app_or_router, 'openapi_url', None), + getattr(app_or_router, 'docs_url', None), + getattr(app_or_router, 'redoc_url', None), + getattr(app_or_router, 'swagger_ui_oauth2_redirect_url', None), + ] + + for route in app_or_router.routes: + if route is None or route.path in urls: + continue return False return True