File: drf_yasg_integration.rst

package info (click to toggle)
python-djangorestframework-simplejwt 5.5.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 956 kB
  • sloc: python: 3,783; makefile: 213; javascript: 40; sh: 6
file content (100 lines) | stat: -rw-r--r-- 3,118 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
.. _drf_yasg_integration:

``drf-yasg`` Integration
------------------------

`drf-yasg`_ is a library that automatically generates an OpenAPI schema by
inspecting DRF ``Serializer`` definitions. Because
``django-rest-framework-simplejwt`` serializers are not symmetric, if you
want to generate correct OpenAPI schemas for your JWT token endpoints, use the
following code to decorate your JWT ``View`` definitions.

.. code-block:: python

    from drf_yasg.utils import swagger_auto_schema
    from rest_framework import serializers, status
    from rest_framework_simplejwt.views import (
        TokenBlacklistView,
        TokenObtainPairView,
        TokenRefreshView,
        TokenVerifyView,
    )


    class TokenObtainPairResponseSerializer(serializers.Serializer):
        access = serializers.CharField()
        refresh = serializers.CharField()

        def create(self, validated_data):
            raise NotImplementedError()

        def update(self, instance, validated_data):
            raise NotImplementedError()


    class DecoratedTokenObtainPairView(TokenObtainPairView):
        @swagger_auto_schema(
            responses={
                status.HTTP_200_OK: TokenObtainPairResponseSerializer,
            }
        )
        def post(self, request, *args, **kwargs):
            return super().post(request, *args, **kwargs)


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

        def create(self, validated_data):
            raise NotImplementedError()

        def update(self, instance, validated_data):
            raise NotImplementedError()


    class DecoratedTokenRefreshView(TokenRefreshView):
        @swagger_auto_schema(
            responses={
                status.HTTP_200_OK: TokenRefreshResponseSerializer,
            }
        )
        def post(self, request, *args, **kwargs):
            return super().post(request, *args, **kwargs)


    class TokenVerifyResponseSerializer(serializers.Serializer):
        def create(self, validated_data):
            raise NotImplementedError()

        def update(self, instance, validated_data):
            raise NotImplementedError()


    class DecoratedTokenVerifyView(TokenVerifyView):
        @swagger_auto_schema(
            responses={
                status.HTTP_200_OK: TokenVerifyResponseSerializer,
            }
        )
        def post(self, request, *args, **kwargs):
            return super().post(request, *args, **kwargs)


    class TokenBlacklistResponseSerializer(serializers.Serializer):
        def create(self, validated_data):
            raise NotImplementedError()

        def update(self, instance, validated_data):
            raise NotImplementedError()


    class DecoratedTokenBlacklistView(TokenBlacklistView):
        @swagger_auto_schema(
            responses={
                status.HTTP_200_OK: TokenBlacklistResponseSerializer,
            }
        )   
        def post(self, request, *args, **kwargs):
            return super().post(request, *args, **kwargs)

.. _drf-yasg: https://github.com/axnsan12/drf-yasg