drf_yasg: How to define multipart/form-data in request body

Not able to generate a swagger file with multipart/form-data content-type in request

Description

I have a POST request to upload a document where I send the document sent in multipart/form-data. I have tried to describe the form data as such

image

This is how my request looks in postman image

When I try to generate a swagger file. It gives me the following error drf_yasg.errors.SwaggerGenerationError: cannot add form parameters when the request has a request body; did you forget to set an appropriate parser class on the view?

Minimal Reproduction

@swagger_auto_schema(
        operation_id='Create a document',
        operation_description='Create a document by providing file and s3_key',
        manual_parameters=[
            openapi.Parameter('file', openapi.IN_FORM, type=openapi.TYPE_FILE, description='Document to be uploaded'),
            openapi.Parameter('s3_key', openapi.IN_FORM, type=openapi.TYPE_STRING, description='S3 Key of the Document '
                                                                                               '(folders along with name)')
        ],
        responses={
            status.HTTP_200_OK: openapi.Response(
                'Success', schema=openapi.Schema(type=openapi.TYPE_OBJECT, properties={
                    'doc_id': openapi.Schema(type=openapi.TYPE_STRING, description='Document ID'),
                    'mime_type': openapi.Schema(type=openapi.TYPE_STRING, description='Mime Type of the Document'),
                    'version_id': openapi.Schema(type=openapi.TYPE_STRING, description='S3 version ID of the document')
                })
            )
        }
    )

Upvotes: 1

Views: 4374

Answers (1)

Atod2
Atod2

Reputation: 31

In your View class, you need to set the MultiPartParser class, defining what type of media you are using:

from rest_framework.views import APIView
from rest_framework.parsers import MultiPartParser

class MyAPIView(APIView):
    parser_classes = [MultiPartParser]

    @swagger_auto_schema(
            operation_id='Create a document',
            operation_description='Create a document by providing file and s3_key',
            manual_parameters=[
                openapi.Parameter('file', openapi.IN_FORM, type=openapi.TYPE_FILE, description='Document to be uploaded'),
                openapi.Parameter('s3_key', openapi.IN_FORM, type=openapi.TYPE_STRING, description='S3 Key of the Document '
                                                                                                   '(folders along with name)')
            ],
            responses={
                status.HTTP_200_OK: openapi.Response(
                    'Success', schema=openapi.Schema(type=openapi.TYPE_OBJECT, properties={
                        'doc_id': openapi.Schema(type=openapi.TYPE_STRING, description='Document ID'),
                        'mime_type': openapi.Schema(type=openapi.TYPE_STRING, description='Mime Type of the Document'),
                        'version_id': openapi.Schema(type=openapi.TYPE_STRING, description='S3 version ID of the document')
                    })
                )
            }
        )
    def post(self, request, *args, **kwargs):
        # Content of the post method

Upvotes: 3

Related Questions