From 008ac949829e740194086e0fc68e272c3b1820c3 Mon Sep 17 00:00:00 2001 From: sevdog Date: Thu, 10 Aug 2023 17:51:52 +0200 Subject: [PATCH] Use str as default path converter --- rest_framework/routers.py | 2 +- tests/test_routers.py | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/rest_framework/routers.py b/rest_framework/routers.py index fa5d16922b..2b9478e905 100644 --- a/rest_framework/routers.py +++ b/rest_framework/routers.py @@ -144,7 +144,7 @@ def __init__(self, trailing_slash=True, use_regex_path=True): self._url_conf = re_path else: self._base_pattern = '<{lookup_value}:{lookup_prefix}{lookup_url_kwarg}>' - self._default_value_pattern = 'path' + self._default_value_pattern = 'str' self._url_conf = path # remove regex characters from routes _routes = [] diff --git a/tests/test_routers.py b/tests/test_routers.py index b4bde5c312..887f601d54 100644 --- a/tests/test_routers.py +++ b/tests/test_routers.py @@ -99,6 +99,13 @@ def url_path_detail(self, request, *args, **kwargs): kwarg = self.kwargs.get('kwarg', '') return Response({'pk': pk, 'kwarg': kwarg}) + @action(detail=True, url_path='detail//detail/') + def url_path_detail_multiple_params(self, request, *args, **kwargs): + pk = self.kwargs.get('pk', '') + kwarg = self.kwargs.get('kwarg', '') + param = self.kwargs.get('param', '') + return Response({'pk': pk, 'kwarg': kwarg, 'param': param}) + notes_router = SimpleRouter() notes_router.register(r'notes', NoteViewSet) @@ -561,6 +568,18 @@ def test_detail_extra_action(self): assert response.status_code == 200 assert json.loads(response.content.decode()) == {'pk': pk, 'kwarg': kwarg} + def test_detail_extra_other_action(self): + # this to assure that ambiguous patterns are interpreted correctly + # using the `path` converters this URL is recognized to match the pattern + # of `UrlPathViewSet.url_path_detail` when it should match + # `UrlPathViewSet.url_path_detail_multiple_params` + pk = '1' + kwarg = 1234 + param = 2 + response = self.client.get('/path/1/detail/1234/detail/2/') + assert response.status_code == 200 + assert json.loads(response.content.decode()) == {'pk': pk, 'kwarg': kwarg, 'param': param} + def test_defaultrouter_root(self): response = self.client.get('/default/') assert response.status_code == 200