diff --git a/fastapi_restful/cbv.py b/fastapi_restful/cbv.py index 8508425c..386826bf 100644 --- a/fastapi_restful/cbv.py +++ b/fastapi_restful/cbv.py @@ -85,6 +85,15 @@ def new_init(self: Any, *args: Any, **kwargs: Any) -> None: setattr(cls, CBV_CLASS_KEY, True) +def _remove_router_tags(route: Route, router: APIRouter) -> None: + """ + Removes tags previously assigned to the route by the router, as they will + be added again later when re-added to the router. + """ + if hasattr(route, 'tags'): + route.tags = list(set(route.tags) - set(router.tags)) + + def _register_endpoints(router: APIRouter, cls: Type[Any], *urls: str) -> None: cbv_router = APIRouter() function_members = inspect.getmembers(cls, inspect.isfunction) @@ -111,6 +120,7 @@ def _register_endpoints(router: APIRouter, cls: Type[Any], *urls: str) -> None: prefix_length = len(router.prefix) # Until 'black' would fix an issue which causes PEP8: E203 for route in cbv_routes: router.routes.remove(route) + _remove_router_tags(route, router) route.path = route.path[prefix_length:] _update_cbv_route_endpoint_signature(cls, route) cbv_router.routes.append(route) diff --git a/tests/test_cbv.py b/tests/test_cbv.py index a6ba1af1..aba14091 100644 --- a/tests/test_cbv.py +++ b/tests/test_cbv.py @@ -130,3 +130,14 @@ def root(self) -> str: response = client.get("/api/item") assert response.status_code == 200 assert response.json() == "hello" + + def test_duplicate_tag_removal(self) -> None: + router = APIRouter(prefix="/api", tags=["test"]) + + @cbv(router) + class CBV: + @router.get("/item") + def root(self) -> str: + return "hello" + + assert router.routes[0].tags == ["test"]