Browse Source

Add check for ciruclar imports + test

pull/14258/head
Javier Sánchez 7 months ago
parent
commit
111de89957
  1. 1
      fastapi/routing.py
  2. 11
      tests/test_router_circular_import.py

1
fastapi/routing.py

@ -1333,6 +1333,7 @@ class APIRouter(routing.Router):
app.include_router(internal_router)
```
"""
assert self is not router, "Cannot include router into itself"
if prefix:
assert prefix.startswith("/"), "A path prefix must start with '/'"
assert not prefix.endswith("/"), (

11
tests/test_router_circular_import.py

@ -0,0 +1,11 @@
import pytest
from fastapi import APIRouter, FastAPI
def test_router_circular_import():
app = FastAPI()
router = APIRouter()
app.include_router(router)
with pytest.raises(AssertionError, match="Router cannot be the same as parent"):
router.include_router(router)
Loading…
Cancel
Save