Skip to content

Commit df95011

Browse files
JavierSanchezCastroJavier Sánchezpre-commit-ci[bot]YuriiMotov
authored
✨ Show a clear error on attempt to include router into itself (fastapi#14258)
Co-authored-by: Javier Sánchez <javier.sanchez.castro@bookline.ai> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com>
1 parent 363aced commit df95011

2 files changed

Lines changed: 16 additions & 0 deletions

File tree

fastapi/routing.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,6 +1393,10 @@ def read_users():
13931393
app.include_router(internal_router)
13941394
```
13951395
"""
1396+
assert self is not router, (
1397+
"Cannot include the same APIRouter instance into itself. "
1398+
"Did you mean to include a different router?"
1399+
)
13961400
if prefix:
13971401
assert prefix.startswith("/"), "A path prefix must start with '/'"
13981402
assert not prefix.endswith("/"), (
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import pytest
2+
from fastapi import APIRouter
3+
4+
5+
def test_router_circular_import():
6+
router = APIRouter()
7+
8+
with pytest.raises(
9+
AssertionError,
10+
match="Cannot include the same APIRouter instance into itself. Did you mean to include a different router?",
11+
):
12+
router.include_router(router)

0 commit comments

Comments
 (0)