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

Simple JWT integration goes wrong. #232

Closed
dem214 opened this issue Dec 14, 2020 · 5 comments
Closed

Simple JWT integration goes wrong. #232

dem214 opened this issue Dec 14, 2020 · 5 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@dem214
Copy link

dem214 commented Dec 14, 2020

Describe the bug
Generated schema shows wrong response schema of rest_fremework_simplejwt.views.TokenObtainPairView()
Screenshot_20201214_172547

To Reproduce
Add this to urls of new django project:

from rest_framework_simplejwt.views import TokenObtainPairView

urlpatterns += [path("api/token/", TokenObtainPairView.as_view(), name='token_obtain_pair'),]

Check the schema.

Expected behavior
api/token/ view must response with schema like {"access":string,"refresh":string}

Versions
django==3.0.10
djangorestframework==3.12.1
djangorestframework-simplejwt==4.4.0
drf-spectacular==0.11.1

See also
Same problem can be with from rest_framework_simplejwt.views.TokenRefreshView

@dem214 dem214 changed the title Simple JWT integrations goes wrong. Simple JWT integration goes wrong. Dec 14, 2020
@tfranzel tfranzel added the bug Something isn't working label Dec 14, 2020
@tfranzel
Copy link
Owner

Hi @dem214, djangorestframework-simplejwt has a weird way of constructing those serializers. this should solve it.

@tfranzel tfranzel added the fix confirmation pending issue has been fixed and confirmation from issue reporter is pending label Dec 14, 2020
@marcinjosinski
Copy link

marcinjosinski commented Dec 14, 2020

Hello,
@tfranzel this fix resolves all problems except the token/refresh view.

Something like that fixes schema for token/refresh view too.

class SimpleJWTTokenRefreshView(OpenApiViewExtension):
    target_class = 'rest_framework_simplejwt.views.TokenRefreshView'

    def view_replacement(self):
        if getattr(settings, 'SIMPLE_JWT', {}).get('ROTATE_REFRESH_TOKENS', False):
            class TokenRefreshResponseSerializer(serializers.Serializer):
                access = serializers.CharField()
                refresh = serializers.CharField()
        else:
            class TokenRefreshResponseSerializer(serializers.Serializer):
                access = serializers.CharField()

        class Fixed(self.target_class):
            @extend_schema(responses=TokenRefreshResponseSerializer)
            def post(self, request, *args, **kwargs):
                pass  # pragma: no cover

        return Fixed

@tfranzel By the way, thanks for maintaining such a great project!

@dem214
Copy link
Author

dem214 commented Dec 15, 2020

Nicely done

@dem214 dem214 closed this as completed Dec 15, 2020
tfranzel added a commit that referenced this issue Dec 15, 2020
@tfranzel
Copy link
Owner

hey @marcinjosinski, i missed that one too. slightly modified your proposal to target TokenRefreshSerializer instead of the view.

            class TokenRefreshResponseSerializer(serializers.Serializer):
                access = serializers.CharField()

i think your proposal has a slight bug. as far as i understand, for ROTATE_REFRESH_TOKENS=False the serializer has refresh (request only) and access (response only). can you confirm this is now correct?

this also fixes dj_rest_auth as it optionally uses simple JWT. yeah!

@marcinjosinski
Copy link

My proposal gives the same results because I override responses here only, but targeting the Serializer is a more elegant solution.

I've just checked it and everything looks good. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

3 participants