Skip to content

Use str as default path converter #9066

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 16, 2023
Merged
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
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any tests to verify this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here they are: I have added the corner case in which there may be some patterns which may be ambigouus.

Thus using the <path:var> could cause problems, since it is too greedy, and having the latest registered pattern to never match if there is any sub-match.

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