Skip to content

Commit c941b99

Browse files
Add integration instructions for drf-yasg (#145)
* Add drf-yasg integration page to docs * Add views.py * Add SECRET_KEY to docs/conf.py * Remove Makefile change Co-authored-by: Andrew Chen Wang <60190294+Andrew-Chen-Wang@users.noreply.github.com>
1 parent 8864dc8 commit c941b99

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

docs/drf_yasg_integration.rst

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Contents
5353
blacklist_app
5454
experimental_features
5555
development_and_contributing
56+
drf_yasg_integration
5657
rest_framework_simplejwt
5758

5859

0 commit comments

Comments
 (0)