]> git.ipfire.org Git - thirdparty/fastapi/fastapi.git/commitdiff
:sparkles: Allow empty routed path (issue #414) (#415)
authorVitaliy Kucheryaviy <ppr.vitaly@gmail.com>
Sun, 25 Aug 2019 00:39:48 +0000 (03:39 +0300)
committerSebastián Ramírez <tiangolo@gmail.com>
Sun, 25 Aug 2019 00:39:48 +0000 (19:39 -0500)
fastapi/routing.py
tests/test_empty_router.py [new file with mode: 0644]

index 304b2a11616fb62128d5c0d89c9247b6ea2506d5..33a7d49d10155235d32ca464a6e3a2b93feb0c03 100644 (file)
@@ -201,7 +201,6 @@ class APIRoute(routing.Route):
         response_class: Type[Response] = JSONResponse,
         dependency_overrides_provider: Any = None,
     ) -> None:
-        assert path.startswith("/"), "Routed paths must always start with '/'"
         self.path = path
         self.endpoint = endpoint
         self.name = get_name(endpoint) if name is None else name
@@ -448,6 +447,12 @@ class APIRouter(routing.Router):
             assert not prefix.endswith(
                 "/"
             ), "A path prefix must not end with '/', as the routes will start with '/'"
+        else:
+            for r in router.routes:
+                if not r.path:
+                    raise Exception(
+                        f"Prefix and path cannot be both empty (operation: {r.name})"
+                    )
         if responses is None:
             responses = {}
         for route in router.routes:
diff --git a/tests/test_empty_router.py b/tests/test_empty_router.py
new file mode 100644 (file)
index 0000000..57dd006
--- /dev/null
@@ -0,0 +1,33 @@
+import pytest
+from fastapi import APIRouter, FastAPI
+from starlette.testclient import TestClient
+
+app = FastAPI()
+
+router = APIRouter()
+
+
+@router.get("")
+def get_empty():
+    return ["OK"]
+
+
+app.include_router(router, prefix="/prefix")
+
+
+client = TestClient(app)
+
+
+def test_use_empty():
+    with client:
+        response = client.get("/prefix")
+        assert response.json() == ["OK"]
+
+        response = client.get("/prefix/")
+        assert response.status_code == 404
+
+
+def test_include_empty():
+    # if both include and router.path are empty - it should raise exception
+    with pytest.raises(Exception):
+        app.include_router(router)