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

FastAPI integration fails if docs are disabled #398

Open
MattiasOlla opened this issue Jan 6, 2022 · 1 comment · May be fixed by #454
Open

FastAPI integration fails if docs are disabled #398

MattiasOlla opened this issue Jan 6, 2022 · 1 comment · May be fixed by #454
Assignees

Comments

@MattiasOlla
Copy link

I have a FastAPI application where the docs are disabled:

from fastapi import FastAPI
import rollbar
from rollbar.contrib.fastapi import add_to as rollbar_add_to

rollbar.init(
    ...,
    environment='test',
    handler="async",
    include_request_body=True,
)

app = FastAPI(docs_url=None, redoc_url=None)

rollbar_add_to(app)

Which gives me the error ERROR:rollbar.contrib.fastapi.routing:RollbarLoggingRoute must to be added to a bare router (before adding routes). See docs for more details.

I dug a bit, and the issue seems to be in this section

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)
):
return False
return True
where the number of expected routes for a bare app is hardwired to 4 (meaning it expects /openapi.json, /docs, /docs/oauth2-redirect, and /redoc), but my app only has one of those (/openapi.json).

Depending on the FastAPI setup, there might be anywhere between 0 and 4 available routes for a "bare" app. I might create a PR for this later, but my suggestion would be checking that there are no available routes other than the ones in the fields openapi_url, docs_url, redoc_url, and swagger_ui_oauth2_redirect_url on the FastAPI object https://github.com/tiangolo/fastapi/blob/3efb4f7edff99fdc12802a85ae9d140ec4772497/fastapi/applications.py#L107-L121

tsasao added a commit to tsasao/pyrollbar that referenced this issue Apr 25, 2023
@antonio-cinnamon
Copy link

This is also happening to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants