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

Add integration instructions for drf-yasg #145

Merged
merged 10 commits into from
Jul 7, 2021
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ build-docs:
rest_framework_simplejwt/compat.py \
rest_framework_simplejwt/exceptions.py \
rest_framework_simplejwt/settings.py \
rest_framework_simplejwt/state.py
rest_framework_simplejwt/state.py \
rest_framework_simplejwt/views.py
Andrew-Chen-Wang marked this conversation as resolved.
Show resolved Hide resolved
$(MAKE) -C docs clean
$(MAKE) -C docs html
$(MAKE) -C docs doctest
Expand Down
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def django_configure():
from django.conf import settings

settings.configure(
SECRET_KEY="exposed secret key",
INSTALLED_APPS=(
'django.contrib.admin',
'django.contrib.auth',
Expand Down
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 @@ -45,6 +45,7 @@ Contents
blacklist_app
experimental_features
development_and_contributing
drf_yasg_integration
rest_framework_simplejwt


Expand Down