Skip to content

Commit 5c07060

Browse files
authored
Use str as default path converter (encode#9066)
1 parent 9e05aa5 commit 5c07060

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

rest_framework/routers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ def __init__(self, trailing_slash=True, use_regex_path=True):
144144
self._url_conf = re_path
145145
else:
146146
self._base_pattern = '<{lookup_value}:{lookup_prefix}{lookup_url_kwarg}>'
147-
self._default_value_pattern = 'path'
147+
self._default_value_pattern = 'str'
148148
self._url_conf = path
149149
# remove regex characters from routes
150150
_routes = []

tests/test_routers.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,13 @@ def url_path_detail(self, request, *args, **kwargs):
9999
kwarg = self.kwargs.get('kwarg', '')
100100
return Response({'pk': pk, 'kwarg': kwarg})
101101

102+
@action(detail=True, url_path='detail/<int:kwarg>/detail/<int:param>')
103+
def url_path_detail_multiple_params(self, request, *args, **kwargs):
104+
pk = self.kwargs.get('pk', '')
105+
kwarg = self.kwargs.get('kwarg', '')
106+
param = self.kwargs.get('param', '')
107+
return Response({'pk': pk, 'kwarg': kwarg, 'param': param})
108+
102109

103110
notes_router = SimpleRouter()
104111
notes_router.register(r'notes', NoteViewSet)
@@ -561,6 +568,18 @@ def test_detail_extra_action(self):
561568
assert response.status_code == 200
562569
assert json.loads(response.content.decode()) == {'pk': pk, 'kwarg': kwarg}
563570

571+
def test_detail_extra_other_action(self):
572+
# this to assure that ambiguous patterns are interpreted correctly
573+
# using the `path` converters this URL is recognized to match the pattern
574+
# of `UrlPathViewSet.url_path_detail` when it should match
575+
# `UrlPathViewSet.url_path_detail_multiple_params`
576+
pk = '1'
577+
kwarg = 1234
578+
param = 2
579+
response = self.client.get('/path/1/detail/1234/detail/2/')
580+
assert response.status_code == 200
581+
assert json.loads(response.content.decode()) == {'pk': pk, 'kwarg': kwarg, 'param': param}
582+
564583
def test_defaultrouter_root(self):
565584
response = self.client.get('/default/')
566585
assert response.status_code == 200

0 commit comments

Comments
 (0)