Skip to content

Commit

Permalink
Revert "Fix NamespaceVersioning ignoring DEFAULT_VERSION on non-None …
Browse files Browse the repository at this point in the history
…namespac…" (#9335)

This reverts commit 71f87a5.
  • Loading branch information
auvipy authored Mar 22, 2024
1 parent 4ef3aaf commit 400b4c5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 102 deletions.
19 changes: 9 additions & 10 deletions rest_framework/versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,15 @@ class NamespaceVersioning(BaseVersioning):

def determine_version(self, request, *args, **kwargs):
resolver_match = getattr(request, 'resolver_match', None)
if resolver_match is not None and resolver_match.namespace:
# Allow for possibly nested namespaces.
possible_versions = resolver_match.namespace.split(':')
for version in possible_versions:
if self.is_allowed_version(version):
return version

if not self.is_allowed_version(self.default_version):
raise exceptions.NotFound(self.invalid_version_message)
return self.default_version
if resolver_match is None or not resolver_match.namespace:
return self.default_version

# Allow for possibly nested namespaces.
possible_versions = resolver_match.namespace.split(':')
for version in possible_versions:
if self.is_allowed_version(version):
return version
raise exceptions.NotFound(self.invalid_version_message)

def reverse(self, viewname, args=None, kwargs=None, request=None, format=None, **extra):
if request.version is not None:
Expand Down
93 changes: 1 addition & 92 deletions tests/test_versioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ class FakeResolverMatch(ResolverMatch):
assert response.status_code == status.HTTP_404_NOT_FOUND


class TestAcceptHeaderAllowedAndDefaultVersion:
class TestAllowedAndDefaultVersion:
def test_missing_without_default(self):
scheme = versioning.AcceptHeaderVersioning
view = AllowedVersionsView.as_view(versioning_class=scheme)
Expand Down Expand Up @@ -318,97 +318,6 @@ def test_missing_with_default_and_none_allowed(self):
assert response.data == {'version': 'v2'}


class TestNamespaceAllowedAndDefaultVersion:
def test_no_namespace_without_default(self):
class FakeResolverMatch:
namespace = None

scheme = versioning.NamespaceVersioning
view = AllowedVersionsView.as_view(versioning_class=scheme)

request = factory.get('/endpoint/')
request.resolver_match = FakeResolverMatch
response = view(request)
assert response.status_code == status.HTTP_404_NOT_FOUND

def test_no_namespace_with_default(self):
class FakeResolverMatch:
namespace = None

scheme = versioning.NamespaceVersioning
view = AllowedAndDefaultVersionsView.as_view(versioning_class=scheme)

request = factory.get('/endpoint/')
request.resolver_match = FakeResolverMatch
response = view(request)
assert response.status_code == status.HTTP_200_OK
assert response.data == {'version': 'v2'}

def test_no_match_without_default(self):
class FakeResolverMatch:
namespace = 'no_match'

scheme = versioning.NamespaceVersioning
view = AllowedVersionsView.as_view(versioning_class=scheme)

request = factory.get('/endpoint/')
request.resolver_match = FakeResolverMatch
response = view(request)
assert response.status_code == status.HTTP_404_NOT_FOUND

def test_no_match_with_default(self):
class FakeResolverMatch:
namespace = 'no_match'

scheme = versioning.NamespaceVersioning
view = AllowedAndDefaultVersionsView.as_view(versioning_class=scheme)

request = factory.get('/endpoint/')
request.resolver_match = FakeResolverMatch
response = view(request)
assert response.status_code == status.HTTP_200_OK
assert response.data == {'version': 'v2'}

def test_with_default(self):
class FakeResolverMatch:
namespace = 'v1'

scheme = versioning.NamespaceVersioning
view = AllowedAndDefaultVersionsView.as_view(versioning_class=scheme)

request = factory.get('/endpoint/')
request.resolver_match = FakeResolverMatch
response = view(request)
assert response.status_code == status.HTTP_200_OK
assert response.data == {'version': 'v1'}

def test_no_match_without_default_but_none_allowed(self):
class FakeResolverMatch:
namespace = 'no_match'

scheme = versioning.NamespaceVersioning
view = AllowedWithNoneVersionsView.as_view(versioning_class=scheme)

request = factory.get('/endpoint/')
request.resolver_match = FakeResolverMatch
response = view(request)
assert response.status_code == status.HTTP_200_OK
assert response.data == {'version': None}

def test_no_match_with_default_and_none_allowed(self):
class FakeResolverMatch:
namespace = 'no_match'

scheme = versioning.NamespaceVersioning
view = AllowedWithNoneAndDefaultVersionsView.as_view(versioning_class=scheme)

request = factory.get('/endpoint/')
request.resolver_match = FakeResolverMatch
response = view(request)
assert response.status_code == status.HTTP_200_OK
assert response.data == {'version': 'v2'}


class TestHyperlinkedRelatedField(URLPatternsTestCase, APITestCase):
included = [
path('namespaced/<int:pk>/', dummy_pk_view, name='namespaced'),
Expand Down

0 comments on commit 400b4c5

Please sign in to comment.