diff --git a/fastapi_restful/cbv.py b/fastapi_restful/cbv.py index fbcaf35e..c354ea77 100644 --- a/fastapi_restful/cbv.py +++ b/fastapi_restful/cbv.py @@ -106,8 +106,10 @@ def _register_endpoints(router: APIRouter, cls: Type[Any], *urls: str) -> None: for route in router.routes if isinstance(route, (Route, WebSocketRoute)) and route.endpoint in functions_set ] + prefix_length = len(router.prefix) # Until 'black' would fix an issue which causes PEP8: E203 for route in cbv_routes: router.routes.remove(route) + route.path = route.path[prefix_length:] _update_cbv_route_endpoint_signature(cls, route) cbv_router.routes.append(route) router.include_router(cbv_router) diff --git a/tests/test_cbv.py b/tests/test_cbv.py index 0619c3bb..dafd1a5b 100644 --- a/tests/test_cbv.py +++ b/tests/test_cbv.py @@ -82,3 +82,18 @@ def root(self, item_path: str = None, item_query: str = None) -> Any: assert client.get("/items").json() == [] assert client.get("/items/1").json() == {"item_path": "1"} assert client.get("/database/abc").json() == {"item_path": "abc"} + + +def test_prefix() -> None: + router = APIRouter(prefix="/api") + + @cbv(router) + class RootHandler: + @router.get("/item") + def root(self) -> str: + return "hello" + + client = TestClient(router) + response = client.get("/api/item") + assert response.status_code == 200 + assert response.json() == "hello"