Skip to content

Commit

Permalink
Add integration instructions for drf-yasg (#145)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
johnthagen and Andrew-Chen-Wang committed Jul 7, 2021
1 parent 8864dc8 commit c941b99
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
72 changes: 72 additions & 0 deletions docs/drf_yasg_integration.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
.. _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 (
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)
.. _drf-yasg: https://github.com/axnsan12/drf-yasg
1 change: 1 addition & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Contents
blacklist_app
experimental_features
development_and_contributing
drf_yasg_integration
rest_framework_simplejwt


Expand Down

0 comments on commit c941b99

Please sign in to comment.