Skip to content

Integration with drf-rw-serializers #448

@luzfcb

Description

@luzfcb

Describe the bug
Hello, first of all, thanks for your work. This library is being very useful for one of the projects I'm working on.

One of the projects I work on, we make extensive use of drf-rw-serializers.

This means that serializer_class is not defined in the Views, and instead, write_serializer_class and read_serializer_class are defined.

All view classes provided by drf-rw-serializers inherit from drf-rw-serializers GenericAPIView class https://github.com/vintasoftware/drf-rw-serializers/blob/master/drf_rw_serializers/generics.py#L8-L71

I'm trying to get the documentation generated correctly, but I haven't been able to succeed yet.

The generated schema does not include any information provided by serializers:

  /api/mymodel/:
    post:
      operationId: mymodel_create
      description: ''
      tags:
      - core
      security:
      - cookieAuth: []
      - basicAuth: []
      responses:
        '201':
          description: No response body
  /api/mymodel/{id}/:
    get:
      operationId: mymodel_retrieve
      description: ''
      parameters:
      - in: path
        name: id
        schema:
          type: integer
        required: true
      tags:
      - core
      security:
      - cookieAuth: []
      - basicAuth: []
      responses:
        '200':
          description: No response body

Below is the minimum code to reproduce the problem.

To Reproduce

from rest_framework.permissions import IsAdminUser
from rest_framework import parsers
from drf_rw_serializers.viewsets import ModelViewSet as ModelRWViewSet
from drf_spectacular.utils import extend_schema, extend_schema_view

from .models import MyModel
from .serializers import MyModelUploadSerializer, MyModelSerializer

@extend_schema_view(
    post=extend_schema(
        description="POST method description here",
        request=MyModelUploadSerializer,
        responses={
            201: MyModelSerializer
        }
    ),
    get=extend_schema(
        description="GET method description here",
        responses={
            200: MyModelSerializer
        }
    )
)
class MyAPIView(ModelRWViewSet):
    queryset = MyModel.objects.order_by('-created').all()
    write_serializer_class = MyModelUploadSerializer
    read_serializer_class = MyModelSerializer
    permission_classes = [IsAdminUser]
    parser_classes = [parsers.MultiPartParser]

urls.py

from django.urls import path
from .api_views import MyAPIView

urlpatterns = [
    path('api/mymodel/', view=MyAPIView.as_view({'post': 'create'}),
         name='mymodel-upload'
         ),
    path('api/mymodel/<int:pk>/', view=MyAPIView.as_view({'get': 'retrieve'}),
         name='mymodel-detail'
         ),
]

Expected behavior

I would hope to get some hint on how to implement a generic way to make drf-spectacular understand the structure of a view that inherits from drf-rw-serializers GenericAPIView class and from there, be able to use write_serializer_class and read_serializer_class instead of just serializer_class to generate the OpenAPI schema.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions