Skip to content

Commit

Permalink
Use str as default path converter (#9066)
Browse files Browse the repository at this point in the history
  • Loading branch information
sevdog authored Aug 16, 2023
1 parent 9e05aa5 commit 5c07060
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
2 changes: 1 addition & 1 deletion rest_framework/routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down
19 changes: 19 additions & 0 deletions tests/test_routers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:kwarg>/detail/<int:param>')
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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5c07060

Please sign in to comment.