Skip to content
Open
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
2 changes: 2 additions & 0 deletions debug_toolbar/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from fastapi.responses import StreamingResponse
from fastapi.staticfiles import StaticFiles
from starlette.middleware.base import BaseHTTPMiddleware, RequestResponseEndpoint
from starlette.responses import JSONResponse
from starlette.routing import NoMatchFound
from starlette.types import ASGIApp

Expand Down Expand Up @@ -44,6 +45,7 @@ def init_toolbar(self) -> None:
self.settings.API_URL,
name="debug_toolbar.render_panel",
include_in_schema=False,
response_class=JSONResponse,
)(self.require_show_toolbar(render_panel))

self.router.mount(
Expand Down
13 changes: 9 additions & 4 deletions debug_toolbar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,15 @@ def matched_route(request: Request) -> APIRoute | None:
for route in request.app.routes:
match, _ = route.matches(request.scope)

if match == Match.FULL:
if hasattr(route, "endpoint"):
return route
break
if match != Match.FULL:
continue
if hasattr(route, "endpoint"):
return route
if contexts := getattr(route, "effective_route_contexts", None):
for context in contexts():
match, _ = context.matches(request.scope)
if match == Match.FULL and hasattr(context.original_route, "endpoint"):
return context.original_route
return None


Expand Down
15 changes: 14 additions & 1 deletion tests/test_middleware.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from fastapi import FastAPI, Request, status
from fastapi import APIRouter, FastAPI, Request, status
from fastapi.responses import HTMLResponse

from debug_toolbar.middleware import show_toolbar
from debug_toolbar.settings import DebugToolbarSettings
Expand All @@ -18,6 +19,18 @@ def test_json(client: TestClient) -> None:
assert client.get_store_id("/openapi.json")


def test_included_router(app: FastAPI) -> None:
router = APIRouter()

@router.get("/included", response_class=HTMLResponse)
async def included() -> str:
return "<html><body>Included</body></html>"

app.include_router(router)

assert TestClient(app).get_store_id("/included")


def test_404(client: TestClient) -> None:
response = client.get("/404")
assert response.status_code == status.HTTP_404_NOT_FOUND
Expand Down