Browse Source

Show a clear error on attempt to include router into itself (#14258)

Co-authored-by: Javier Sánchez <[email protected]>
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Motov Yurii <[email protected]>
pull/14884/head
Javier Sánchez Castro 4 months ago
committed by GitHub
parent
commit
df950111fe
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 4
      fastapi/routing.py
  2. 12
      tests/test_router_circular_import.py

4
fastapi/routing.py

@ -1393,6 +1393,10 @@ class APIRouter(routing.Router):
app.include_router(internal_router)
```
"""
assert self is not router, (
"Cannot include the same APIRouter instance into itself. "
"Did you mean to include a different router?"
)
if prefix:
assert prefix.startswith("/"), "A path prefix must start with '/'"
assert not prefix.endswith("/"), (

12
tests/test_router_circular_import.py

@ -0,0 +1,12 @@
import pytest
from fastapi import APIRouter
def test_router_circular_import():
router = APIRouter()
with pytest.raises(
AssertionError,
match="Cannot include the same APIRouter instance into itself. Did you mean to include a different router?",
):
router.include_router(router)
Loading…
Cancel
Save