Use str as default path converter (#9066)

This commit is contained in:
Devid 2023-08-16 15:11:50 +01:00 committed by GitHub
parent 9e05aa5962
commit 5c07060ce0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 1 deletions

View File

@ -144,7 +144,7 @@ class SimpleRouter(BaseRouter):
self._url_conf = re_path self._url_conf = re_path
else: else:
self._base_pattern = '<{lookup_value}:{lookup_prefix}{lookup_url_kwarg}>' self._base_pattern = '<{lookup_value}:{lookup_prefix}{lookup_url_kwarg}>'
self._default_value_pattern = 'path' self._default_value_pattern = 'str'
self._url_conf = path self._url_conf = path
# remove regex characters from routes # remove regex characters from routes
_routes = [] _routes = []

View File

@ -99,6 +99,13 @@ class UrlPathViewSet(viewsets.ViewSet):
kwarg = self.kwargs.get('kwarg', '') kwarg = self.kwargs.get('kwarg', '')
return Response({'pk': pk, 'kwarg': 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 = SimpleRouter()
notes_router.register(r'notes', NoteViewSet) notes_router.register(r'notes', NoteViewSet)
@ -561,6 +568,18 @@ class TestUrlPath(URLPatternsTestCase, TestCase):
assert response.status_code == 200 assert response.status_code == 200
assert json.loads(response.content.decode()) == {'pk': pk, 'kwarg': kwarg} 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): def test_defaultrouter_root(self):
response = self.client.get('/default/') response = self.client.get('/default/')
assert response.status_code == 200 assert response.status_code == 200