Revert "Fix NamespaceVersioning ignoring DEFAULT_VERSION on non-None namespac…" (#9335)

This reverts commit 71f87a5864.
This commit is contained in:
Asif Saif Uddin 2024-03-22 15:39:30 +06:00 committed by GitHub
parent 4ef3aaf0ad
commit 400b4c5441
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 101 deletions

View File

@ -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 resolver_match is None or not resolver_match.namespace:
return self.default_version
if not self.is_allowed_version(self.default_version):
raise exceptions.NotFound(self.invalid_version_message)
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:

View File

@ -272,7 +272,7 @@ class TestInvalidVersion:
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)
@ -318,97 +318,6 @@ class TestAcceptHeaderAllowedAndDefaultVersion:
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'),