Thanks a lot for making this library. I find it very useful.
I encountered an issue after updating to 4.1.1, using the following code in a custom view action.
# …
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
# …
token_serializer = TokenObtainPairSerializer()
tokens = token_serializer.validate(
{"email": user.email, "password": (data["password"])}
)
# …
This results in the following KeyError.
Traceback (most recent call last):
File "/home/scrutinizer/build/users/test_reset_password.py", line 14, in test_user_exists
{"password": new_password, "token": token, "user": user_id},
File "/home/scrutinizer/build/lib/python3.7/site-packages/rest_framework/test.py", line 300, in post
path, data=data, format=format, content_type=content_type, **extra)
File "/home/scrutinizer/build/lib/python3.7/site-packages/rest_framework/test.py", line 213, in post
return self.generic('POST', path, data, content_type, **extra)
File "/home/scrutinizer/build/lib/python3.7/site-packages/rest_framework/test.py", line 238, in generic
method, path, data, content_type, secure, **extra)
File "/home/scrutinizer/build/lib/python3.7/site-packages/django/test/client.py", line 414, in generic
return self.request(**r)
File "/home/scrutinizer/build/lib/python3.7/site-packages/rest_framework/test.py", line 289, in request
return super(APIClient, self).request(**kwargs)
File "/home/scrutinizer/build/lib/python3.7/site-packages/rest_framework/test.py", line 241, in request
request = super(APIRequestFactory, self).request(**kwargs)
File "/home/scrutinizer/build/lib/python3.7/site-packages/django/test/client.py", line 495, in request
raise exc_value
File "/home/scrutinizer/build/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/scrutinizer/build/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/scrutinizer/build/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/scrutinizer/build/lib/python3.7/site-packages/django/views/decorators/csrf.py", line 54, in wrapped_view
return view_func(*args, **kwargs)
File "/home/scrutinizer/build/lib/python3.7/site-packages/rest_framework/viewsets.py", line 116, in view
return self.dispatch(request, *args, **kwargs)
File "/home/scrutinizer/build/lib/python3.7/site-packages/rest_framework/views.py", line 495, in dispatch
response = self.handle_exception(exc)
File "/home/scrutinizer/build/lib/python3.7/site-packages/rest_framework/views.py", line 455, in handle_exception
self.raise_uncaught_exception(exc)
File "/home/scrutinizer/build/lib/python3.7/site-packages/rest_framework/views.py", line 492, in dispatch
response = handler(request, *args, **kwargs)
File "/home/scrutinizer/build/users/views.py", line 95, in reset_password
{"email": user.email, "password": (data["password"])}
File "/home/scrutinizer/build/lib/python3.7/site-packages/rest_framework_simplejwt/serializers.py", line 63, in validate
data = super().validate(attrs)
File "/home/scrutinizer/build/lib/python3.7/site-packages/rest_framework_simplejwt/serializers.py", line 35, in validate
'request': self.context['request'],
KeyError: 'request'
I fixed the issue by adding token_serializer.context["request"] = request:
# …
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
# …
token_serializer = TokenObtainPairSerializer()
token_serializer.context["request"] = request
tokens = token_serializer.validate(
{"email": user.email, "password": (data["password"])}
)
# …
As far as I can understand, #40 introduced the passing on of request, resulting in the KeyError. I haven't found any documentation for validate(). It may not be intended be part of the public API. If so, maybe it should be prepended with an underscore?
If there's anything I can do to help, please let me know.
Just cut a fix for this: https://github.com/davesque/django-rest-framework-simplejwt/commit/e188845febc29a5ad059377437df58c8070a7acb . Re-open if this is still happening.
Yeah, this solves the problem. Thanks a lot for the quick response!
Most helpful comment
Just cut a fix for this: https://github.com/davesque/django-rest-framework-simplejwt/commit/e188845febc29a5ad059377437df58c8070a7acb . Re-open if this is still happening.