From 111de8995783ca3a8b8bbdbabae6d15bb2009ffa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20S=C3=A1nchez?= Date: Thu, 30 Oct 2025 10:50:23 +0100 Subject: [PATCH 1/7] Add check for ciruclar imports + test --- fastapi/routing.py | 1 + tests/test_router_circular_import.py | 11 +++++++++++ 2 files changed, 12 insertions(+) create mode 100644 tests/test_router_circular_import.py diff --git a/fastapi/routing.py b/fastapi/routing.py index 0b59d250a..eb3d2c620 100644 --- a/fastapi/routing.py +++ b/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("/"), ( diff --git a/tests/test_router_circular_import.py b/tests/test_router_circular_import.py new file mode 100644 index 000000000..dfec8d24c --- /dev/null +++ b/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) \ No newline at end of file From ddfaa1e44172b1cfcf1cadc9e7d6e27b82ec5e01 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 30 Oct 2025 09:51:09 +0000 Subject: [PATCH 2/7] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_router_circular_import.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_router_circular_import.py b/tests/test_router_circular_import.py index dfec8d24c..d2ac71b29 100644 --- a/tests/test_router_circular_import.py +++ b/tests/test_router_circular_import.py @@ -8,4 +8,4 @@ def test_router_circular_import(): app.include_router(router) with pytest.raises(AssertionError, match="Router cannot be the same as parent"): - router.include_router(router) \ No newline at end of file + router.include_router(router) From c0fee03fdfd1a3b025642df8d38a36411d9d0423 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20S=C3=A1nchez?= Date: Thu, 30 Oct 2025 10:54:18 +0100 Subject: [PATCH 3/7] correct error msg --- tests/test_router_circular_import.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_router_circular_import.py b/tests/test_router_circular_import.py index d2ac71b29..f74a49109 100644 --- a/tests/test_router_circular_import.py +++ b/tests/test_router_circular_import.py @@ -7,5 +7,5 @@ def test_router_circular_import(): router = APIRouter() app.include_router(router) - with pytest.raises(AssertionError, match="Router cannot be the same as parent"): + with pytest.raises(AssertionError, match="Cannot include router into itself"): router.include_router(router) From 28fa21fd1a1d08c2e6ee715feb3d979bf2662cf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20S=C3=A1nchez=20Castro?= <72013291+JavierSanchezCastro@users.noreply.github.com> Date: Thu, 30 Oct 2025 13:05:57 +0100 Subject: [PATCH 4/7] Update tests/test_router_circular_import.py Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com> --- tests/test_router_circular_import.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/test_router_circular_import.py b/tests/test_router_circular_import.py index f74a49109..a00205b39 100644 --- a/tests/test_router_circular_import.py +++ b/tests/test_router_circular_import.py @@ -1,11 +1,9 @@ import pytest -from fastapi import APIRouter, FastAPI +from fastapi import APIRouter def test_router_circular_import(): - app = FastAPI() router = APIRouter() - app.include_router(router) with pytest.raises(AssertionError, match="Cannot include router into itself"): router.include_router(router) From 4c9cf7439f8e769af600c87190a44367583526cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20S=C3=A1nchez=20Castro?= <72013291+JavierSanchezCastro@users.noreply.github.com> Date: Thu, 30 Oct 2025 13:06:06 +0100 Subject: [PATCH 5/7] Update fastapi/routing.py Co-authored-by: Motov Yurii <109919500+YuriiMotov@users.noreply.github.com> --- fastapi/routing.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/fastapi/routing.py b/fastapi/routing.py index eb3d2c620..a6ed2f328 100644 --- a/fastapi/routing.py +++ b/fastapi/routing.py @@ -1333,7 +1333,10 @@ class APIRouter(routing.Router): app.include_router(internal_router) ``` """ - assert self is not router, "Cannot include router into itself" + 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("/"), ( From 6762879a3a238c882f56b36a60b0f80c1798944c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Javier=20S=C3=A1nchez?= Date: Thu, 30 Oct 2025 13:10:11 +0100 Subject: [PATCH 6/7] new msg error --- tests/test_router_circular_import.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_router_circular_import.py b/tests/test_router_circular_import.py index a00205b39..e4d614d56 100644 --- a/tests/test_router_circular_import.py +++ b/tests/test_router_circular_import.py @@ -5,5 +5,5 @@ from fastapi import APIRouter def test_router_circular_import(): router = APIRouter() - with pytest.raises(AssertionError, match="Cannot include router into itself"): + 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) From 3d4c85c1baeb7b54cae00a667950ac8ed34b09db Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 30 Oct 2025 12:10:25 +0000 Subject: [PATCH 7/7] =?UTF-8?q?=F0=9F=8E=A8=20[pre-commit.ci]=20Auto=20for?= =?UTF-8?q?mat=20from=20pre-commit.com=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/test_router_circular_import.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/test_router_circular_import.py b/tests/test_router_circular_import.py index e4d614d56..492a26d00 100644 --- a/tests/test_router_circular_import.py +++ b/tests/test_router_circular_import.py @@ -5,5 +5,8 @@ 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?"): + 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)