[Fixed]-Django-rest-swagger nested serializers with readonly fields not rendered properly

1๐Ÿ‘

Try to use drf_yasg instead, Swagger will generate the documentation for APIs, but itโ€™s not absolutely right!
If you want to correct Swagger documentation, you can do it. You will need to use swagger_auto_schema decorator. Below is the sample code:

from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema

class ProductSuspendView(CreateAPIView):

    @swagger_auto_schema(
        tags=['dashboard'],
        request_body=openapi.Schema(
            type=openapi.TYPE_OBJECT,
            properties={
                'id': openapi.Schema(
                    type=openapi.TYPE_INTEGER,
                    description='Id',
                ),
                'suspend_kinds': openapi.Schema(
                    type=openapi.TYPE_ARRAY,
                    items=openapi.Items(type=openapi.TYPE_INTEGER),
                    description='Array suspend (Inappropriate image: 1, Insufficient information: 2,  Bad language: 3) (suspend_kinds=[1,2])'
                ),
            }
        ),
        responses={
            status.HTTP_200_OK: SuccessResponseSerializer,
            status.HTTP_400_BAD_REQUEST: ErrorResponseSerializer
        }
    )
    def post(self, request, *args, **kwargs):
        """
        Suspend a product.
        """
        ...
        if success:
            return Response({'success': True}, status.HTTP_200_OK)

        return Response({'success': False}, status.HTTP_400_BAD_REQUEST)
๐Ÿ‘คVu Phan

1๐Ÿ‘

Using https://drf-spectacular.readthedocs.io/en/latest/ would be the right choice for this use case now. this is well maintained and support OpenAPI 3.0+. also it is now suggested by django-rest-framework itself.

๐Ÿ‘คauvipy

0๐Ÿ‘

1. of everything please use drf-yasg for documentation .

2. you can find its implementation in one of my repository Kirpi and learn how to use that.

3. if you in 3. ; have question,let me know.

๐Ÿ‘คHamed Rostami

Leave a comment