From e023af3ee2c334e0dba6d7eed3d08d3496e13084 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Foucault?= Date: Tue, 30 Nov 2021 15:51:05 +0100 Subject: [PATCH 1/2] fix: Allow empty path when prefix is set --- fastapi_restful/cbv.py | 8 ++++++-- tests/test_cbv.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/fastapi_restful/cbv.py b/fastapi_restful/cbv.py index b2cd38f1..77881548 100644 --- a/fastapi_restful/cbv.py +++ b/fastapi_restful/cbv.py @@ -86,7 +86,7 @@ def new_init(self: Any, *args: Any, **kwargs: Any) -> None: def _register_endpoints(router: APIRouter, cls: Type[Any], *urls: str) -> None: - cbv_router = APIRouter() + cbv_router = APIRouter(prefix=router.prefix) function_members = inspect.getmembers(cls, inspect.isfunction) for url in urls: _allocate_routes_by_method_name(router, url, function_members) @@ -112,7 +112,11 @@ def _register_endpoints(router: APIRouter, cls: Type[Any], *urls: str) -> None: route.path = route.path[prefix_length:] _update_cbv_route_endpoint_signature(cls, route) cbv_router.routes.append(route) - router.include_router(cbv_router) + # Tempory remove prefix to by pass the prefix check and allow + # allow empty root method with a prefix + router.prefix = "" + router.include_router(cbv_router, prefix=cbv_router.prefix) + router.prefix = cbv_router.prefix def _allocate_routes_by_method_name(router: APIRouter, url: str, function_members: List[Tuple[str, Any]]) -> None: diff --git a/tests/test_cbv.py b/tests/test_cbv.py index d7981e90..b8e5af05 100644 --- a/tests/test_cbv.py +++ b/tests/test_cbv.py @@ -1,5 +1,6 @@ from typing import Any, ClassVar +import pytest from fastapi import APIRouter, Depends, FastAPI from starlette.testclient import TestClient @@ -97,3 +98,33 @@ def root(self) -> str: response = client.get("/api/item") assert response.status_code == 200 assert response.json() == "hello" + + +def test_prefix_and_empty_route() -> None: + router = APIRouter(prefix="/api") + + @cbv(router) + class RootHandler: + @router.get("") + def root(self) -> str: + return "hello" + + client = TestClient(router) + response = client.get("/api") + assert response.status_code == 200 + assert response.json() == "hello" + + +def test_no_prefix_and_empty_route_raises() -> None: + router = APIRouter() + + with pytest.raises(Exception): + + @cbv(router) + class RootHandler: + @router.get("") + def root(self) -> str: + return "hello" + + client = TestClient(router) + client.get("/api") From 133e25ade4a93aa95927f320341297781ade62bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Foucault?= Date: Tue, 30 Nov 2021 16:21:02 +0100 Subject: [PATCH 2/2] fix: Switch to poetry core for build --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index da310f30..1c31d904 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -125,4 +125,4 @@ source = "init" [build-system] requires = ["poetry_core>=1.0.0"] -build-backend = "poetry.masonry.api" +build-backend = "poetry.core.masonry.api"