Skip to content

Commit

Permalink
Fixed #398 FastAPI integration fails if docs are disabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
danielmorell committed Sep 20, 2024
1 parent 4956f85 commit e74fcde
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
25 changes: 14 additions & 11 deletions rollbar/contrib/fastapi/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
16 changes: 16 additions & 0 deletions rollbar/test/fastapi_tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,22 @@ async def read_root():

self.assertFalse(has_bare_routing(app))

def test_should_return_true_if_docs_disabled(self):
from fastapi import APIRouter, FastAPI
from rollbar.contrib.fastapi.utils import has_bare_routing

app = FastAPI(docs_url=None, redoc_url=None)
self.assertTrue(has_bare_routing(app))

app = FastAPI(docs_url=None)
self.assertTrue(has_bare_routing(app))

app = FastAPI(redoc_url=None)
self.assertTrue(has_bare_routing(app))

router = APIRouter()
self.assertTrue(has_bare_routing(router))

@unittest.skipUnless(
FASTAPI_INSTALLED and ALLOWED_PYTHON_VERSION, 'FastAPI requires Python3.6+'
)
Expand Down

0 comments on commit e74fcde

Please sign in to comment.