Django rest framework drf-yasg swagger multiple file upload error for ListField serializer

Question:

I am trying to make upload file input from swagger (with drf-yasg), but when I use MultiPartParser class it gives me the below error:

drf_yasg.errors.SwaggerGenerationError: FileField is supported only in a formData Parameter or response Schema

My view:

class AddExperience(generics.CreateAPIView):
    parser_classes = [MultiPartParser]

    permission_classes = [IsAuthenticated]
    serializer_class = DoctorExperienceSerializer

My serializer:

class DoctorExperienceSerializer(serializers.Serializer):
    diploma = serializers.ListField(
        child=serializers.FileField(allow_empty_file=False)
    )
    education = serializers.CharField(max_length=1000)
    work_experience = serializers.CharField(max_length=1000)

I also tried FormParser but it still gives me the same error. Also: FileUploadParser parser but it works like JsonParser:

Asked By: I.Jokhadze

||

Answers:

The OpenAPISchema (OAS) 2 doesn’t support the multiple file upload (see issue #254); but OAS 3 supports it (you can use this YML spec on a live swagger editer (see this result)).

Comes to the real issue, there is a section in the drf-yasg’s doc,

If you are looking to add Swagger/OpenAPI support to a new project you might want to take a look at drf-spectacular, which is an actively maintained new library that shares most of the goals of this project, while working with OpenAPI 3.0 schemas.

OpenAPI 3.0 provides a lot more flexibility than 2.0 in the types of API that can be described. drf-yasg is unlikely to soon, if ever, get support for OpenAPI 3.0.

That means the package drf-yasg doesn’t have support for OAS3 and thus, it won’t support the "multiple file upload" feature.

You can consider migrating from drf-yasg to drf-spectacular. But, also note that, drf-spectacular is also dealing the FileUpload in a different way.

Answered By: JPG