Skip to content
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

Maintain compatibility with serializer_class overrides #530

Merged
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion rest_framework_simplejwt/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,17 @@ class TokenViewBase(generics.GenericAPIView):
authentication_classes = ()

serializer_class = None
_serializer_class = ""

www_authenticate_realm = "api"

def get_serializer_class(self):
# Get the serializer from settings
"""
If serializer_class is set, use it directly. Otherwise get the class from settings.
"""

if self.serializer_class:
return self.serializer_class
try:
return import_string(self._serializer_class)
except ImportError:
Expand Down
14 changes: 14 additions & 0 deletions tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from django.contrib.auth import get_user_model
from django.utils import timezone
from rest_framework.test import APIRequestFactory

from rest_framework_simplejwt import serializers
from rest_framework_simplejwt.settings import api_settings
Expand All @@ -13,6 +14,7 @@
datetime_from_epoch,
datetime_to_epoch,
)
from rest_framework_simplejwt.views import TokenViewBase

from .utils import APIViewTestCase, override_api_settings

Expand Down Expand Up @@ -431,3 +433,15 @@ def test_it_should_return_401_if_token_is_blacklisted(self):
del self.view_name

self.assertEqual(res.status_code, 401)


class TestCustomTokenView(APIViewTestCase):
def test_custom_view_class(self):
class CustomTokenView(TokenViewBase):
serializer_class = serializers.TokenObtainPairSerializer

factory = APIRequestFactory()
view = CustomTokenView.as_view()
request = factory.post("/", {}, format="json")
res = view(request)
self.assertEqual(res.status_code, 400)