Skip to content

Commit

Permalink
Merge pull request #459 from rollbar/fixed/issue-398-fastapi-fails-if…
Browse files Browse the repository at this point in the history
…-docs-disabled

Fixed #398 FastAPI integration fails if docs are disabled.
  • Loading branch information
danielmorell authored Sep 20, 2024
2 parents b0d5a23 + 66f8d89 commit 94cbfb8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 10 deletions.
24 changes: 14 additions & 10 deletions rollbar/contrib/fastapi/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import functools
import logging
from typing import Union

import fastapi
from fastapi import APIRouter, FastAPI
Expand Down Expand Up @@ -96,17 +97,20 @@ def get_installed_middlewares(app):
return middlewares


def has_bare_routing(app_or_router):
expected_app_routes = 4
expected_router_routes = 0
def has_bare_routing(app_or_router: Union[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),
]

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)
):
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 94cbfb8

Please sign in to comment.