Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #398 FastAPI integration fails if docs are disabled. #459

Merged
merged 2 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading