|
| 1 | +.. _drf_yasg_integration |
| 2 | +
|
| 3 | +``drf-yasg`` Integration |
| 4 | +------------------------ |
| 5 | + |
| 6 | +`drf-yasg`_ is a library that automatically generates an OpenAPI schema by |
| 7 | +inspecting DRF ``Serializer`` definitions. Because |
| 8 | +``django-rest-framework-simplejwt`` serializers are not symmetric, if you |
| 9 | +want to generate correct OpenAPI schemas for your JWT token endpoints, use the |
| 10 | +following code to decorate your JWT ``View`` definitions. |
| 11 | + |
| 12 | +.. code-block:: python |
| 13 | +
|
| 14 | + from drf_yasg.utils import swagger_auto_schema |
| 15 | + from rest_framework import serializers, status |
| 16 | + from rest_framework_simplejwt.views import ( |
| 17 | + TokenObtainPairView, TokenRefreshView, TokenVerifyView) |
| 18 | +
|
| 19 | +
|
| 20 | + class TokenObtainPairResponseSerializer(serializers.Serializer): |
| 21 | + access = serializers.CharField() |
| 22 | + refresh = serializers.CharField() |
| 23 | +
|
| 24 | + def create(self, validated_data): |
| 25 | + raise NotImplementedError() |
| 26 | +
|
| 27 | + def update(self, instance, validated_data): |
| 28 | + raise NotImplementedError() |
| 29 | +
|
| 30 | +
|
| 31 | + class DecoratedTokenObtainPairView(TokenObtainPairView): |
| 32 | + @swagger_auto_schema( |
| 33 | + responses={ |
| 34 | + status.HTTP_200_OK: TokenObtainPairResponseSerializer}) |
| 35 | + def post(self, request, *args, **kwargs): |
| 36 | + return super().post(request, *args, **kwargs) |
| 37 | +
|
| 38 | +
|
| 39 | + class TokenRefreshResponseSerializer(serializers.Serializer): |
| 40 | + access = serializers.CharField() |
| 41 | +
|
| 42 | + def create(self, validated_data): |
| 43 | + raise NotImplementedError() |
| 44 | +
|
| 45 | + def update(self, instance, validated_data): |
| 46 | + raise NotImplementedError() |
| 47 | +
|
| 48 | +
|
| 49 | + class DecoratedTokenRefreshView(TokenRefreshView): |
| 50 | + @swagger_auto_schema( |
| 51 | + responses={ |
| 52 | + status.HTTP_200_OK: TokenRefreshResponseSerializer}) |
| 53 | + def post(self, request, *args, **kwargs): |
| 54 | + return super().post(request, *args, **kwargs) |
| 55 | +
|
| 56 | +
|
| 57 | + class TokenVerifyResponseSerializer(serializers.Serializer): |
| 58 | + def create(self, validated_data): |
| 59 | + raise NotImplementedError() |
| 60 | +
|
| 61 | + def update(self, instance, validated_data): |
| 62 | + raise NotImplementedError() |
| 63 | +
|
| 64 | +
|
| 65 | + class DecoratedTokenVerifyView(TokenVerifyView): |
| 66 | + @swagger_auto_schema( |
| 67 | + responses={ |
| 68 | + status.HTTP_200_OK: TokenVerifyResponseSerializer}) |
| 69 | + def post(self, request, *args, **kwargs): |
| 70 | + return super().post(request, *args, **kwargs) |
| 71 | +
|
| 72 | +.. _drf-yasg: https://github.com/axnsan12/drf-yasg |
0 commit comments