Skip to content

Commit

Permalink
Maintain compatibility with serializer_class overrides (#530)
Browse files Browse the repository at this point in the history
  • Loading branch information
vainu-arto committed Feb 8, 2022
1 parent 5338fd6 commit 1f3e73d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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)

0 comments on commit 1f3e73d

Please sign in to comment.