From bdc64d4e7370575a70a167dc2ae5d159610ce184 Mon Sep 17 00:00:00 2001 From: Yannick PEROUX Date: Wed, 25 Feb 2015 11:54:11 +0100 Subject: [PATCH 1/2] Fix removal of url_path on @detail_route and @list_route. Fix # #2583 SimpleRouter.get_routes was popping out the url_path kwarg from list_route and detail_route decorators. This was causing troubles when the route was re-used, for example if the viewset was inherited. --- rest_framework/routers.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rest_framework/routers.py b/rest_framework/routers.py index 6a4184e20..081654b8c 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -171,9 +171,9 @@ class SimpleRouter(BaseRouter): # Dynamic detail routes (@detail_route decorator) for httpmethods, methodname in detail_routes: method_kwargs = getattr(viewset, methodname).kwargs - url_path = method_kwargs.pop("url_path", None) or methodname initkwargs = route.initkwargs.copy() initkwargs.update(method_kwargs) + url_path = initkwargs.pop("url_path", None) or methodname ret.append(Route( url=replace_methodname(route.url, url_path), mapping=dict((httpmethod, methodname) for httpmethod in httpmethods), @@ -184,9 +184,9 @@ class SimpleRouter(BaseRouter): # Dynamic list routes (@list_route decorator) for httpmethods, methodname in list_routes: method_kwargs = getattr(viewset, methodname).kwargs - url_path = method_kwargs.pop("url_path", None) or methodname initkwargs = route.initkwargs.copy() initkwargs.update(method_kwargs) + url_path = initkwargs.pop("url_path", None) or methodname ret.append(Route( url=replace_methodname(route.url, url_path), mapping=dict((httpmethod, methodname) for httpmethod in httpmethods), From 9cafdd1854ccd5215b7a188c5896fb498a59d725 Mon Sep 17 00:00:00 2001 From: Yannick PEROUX Date: Tue, 24 Feb 2015 17:14:53 +0100 Subject: [PATCH 2/2] Add a test for #2583 fix --- tests/test_routers.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/tests/test_routers.py b/tests/test_routers.py index 948c69bbf..08c58ec70 100644 --- a/tests/test_routers.py +++ b/tests/test_routers.py @@ -302,12 +302,16 @@ class DynamicListAndDetailViewSet(viewsets.ViewSet): return Response({'method': 'link2'}) +class SubDynamicListAndDetailViewSet(DynamicListAndDetailViewSet): + pass + + class TestDynamicListAndDetailRouter(TestCase): def setUp(self): self.router = SimpleRouter() - def test_list_and_detail_route_decorators(self): - routes = self.router.get_routes(DynamicListAndDetailViewSet) + def _test_list_and_detail_route_decorators(self, viewset): + routes = self.router.get_routes(viewset) decorator_routes = [r for r in routes if not (r.name.endswith('-list') or r.name.endswith('-detail'))] MethodNamesMap = namedtuple('MethodNamesMap', 'method_name url_path') @@ -336,3 +340,9 @@ class TestDynamicListAndDetailRouter(TestCase): else: method_map = 'get' self.assertEqual(route.mapping[method_map], method_name) + + def test_list_and_detail_route_decorators(self): + self._test_list_and_detail_route_decorators(DynamicListAndDetailViewSet) + + def test_inherited_list_and_detail_route_decorators(self): + self._test_list_and_detail_route_decorators(SubDynamicListAndDetailViewSet)