Skip to content
This repository was archived by the owner on Nov 10, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions fastapi_restful/cbv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions tests/test_cbv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]