djangodrf-yasg

Add field in swagger ui using drf-yasg


My code is hosted here. It uses drf-yasg, jwt and the django rest framework. I am trying to set up a patch method to have the user edit his task. The code in a nutshell is as follows

def get_payload(request):
    token = request.COOKIES.get('jwt')
    if not token:
        raise AuthenticationFailed('Unauthenticated!')

    try:
        payload = jwt.decode(token, 'secret', algorithms=['HS256'])
    except jwt.ExpiredSignatureError:
        raise AuthenticationFailed('Unauthenticated!')
    return payload


class TaskView(APIView):
    pk = openapi.Parameter('pk', openapi.IN_QUERY,
                             description="field you want to order by to",
                             type=openapi.TYPE_INTEGER)
    @swagger_auto_schema(
        request_body=openapi.Schema(
            manual_parameters=[pk],
            type=openapi.TYPE_OBJECT,
            properties={
                'taskname': openapi.Schema(type=openapi.TYPE_STRING, description='Add taskname'),
                'completion': openapi.Schema(type=openapi.TYPE_BOOLEAN, description='completion'),
            }
        )
    )
    def patch(self, request, pk):
        payload = get_payload(request=request)
        task = Tasks.objects.filter(id=pk, username=payload['username']).first()
        serializer = TaskSerializer(task, data=request.data)
        if serializer.is_valid():
            serializer.save()
            return Response(serializer.data)
        return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)  

However, the swagger ui does not display the field for pk. I can see the sample body but the field for pk is missing. enter image description here

How do I add the field for pk here?


Solution

  • You need to specify pk parameter in urls config as well:

    urlpatterns = [
        path('register/', RegisterView.as_view()),
        path('login/', LoginView.as_view()),
        path('user/', UserView.as_view()),
        path('logout/', LogoutView.as_view()),
        path('task/<int:pk>/', TaskView.as_view()),
    ]