axnsan12 / drf-yasg

Automated generation of real Swagger/OpenAPI 2.0 schemas from Django REST Framework code.
https://drf-yasg.readthedocs.io/en/stable/
Other
3.41k stars 438 forks source link

Failed to create schema for ListField(chield=FileField) #747

Open Panter7777 opened 3 years ago

Panter7777 commented 3 years ago

Bug Report

Description

Hi, I'm trying to upload files via swagger + drf but got an error. Can you please help?

serializers.py

class PictureInputSerializer(serializers.Serializer):
    is_title = serializers.BooleanField(required=False)
    file = serializers.FileField(
        max_length=1000,
        allow_empty_file=False,
        use_url=False,
        validators=[validate_image_size]
    )

class UpdatePhotoSerializer(serializers.Serializer):
    obj_id = serializers.SlugRelatedField(queryset=SomeModel.objects.all(), slug_field='id')
    pictures = serializers.ListField(child=PictureInputSerializer())

view.py

    @action(
        detail=False,
        methods=["POST"],
        serializer_class=UpdatePhotoSerializer,
        parser_classes=(MultiPartParser, FormParser),
        pagination_class=None
    )
    def upload_photo(self, request):
        with transaction.atomic():
            serializer = self.get_serializer(data=request.data)
            serializer.is_valid(raise_exception=True)
            serializer.custom_create(serializer.validated_data)
        return Response(status=status.HTTP_201_CREATED)

Stack trace / Error message

 File "../venv/lib/python3.8/site-packages/drf_yasg/generators.py", line 404, in get_paths
    operation = self.get_operation(view, path, prefix, method, components, request)
  File "../venv/lib/python3.8/site-packages/drf_yasg/generators.py", line 446, in get_operation
    operation = view_inspector.get_operation(operation_keys)
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/view.py", line 32, in get_operation
    body = self.get_request_body_parameters(consumes)
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/view.py", line 82, in get_request_body_parameters
    return self.get_request_form_parameters(serializer)
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/view.py", line 134, in get_request_form_parameters
    return self.serializer_to_parameters(serializer, in_=openapi.IN_FORM)
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/base.py", line 448, in serializer_to_parameters
    return self.probe_inspectors(
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/base.py", line 110, in probe_inspectors
    result = method(obj, **kwargs)
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/field.py", line 48, in get_request_parameters
    parameters = [
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/field.py", line 49, in <listcomp>
    self.probe_field_inspectors(
  File "..venv/lib/python3.8/site-packages/drf_yasg/inspectors/base.py", line 228, in probe_field_inspectors
    return self.probe_inspectors(
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/base.py", line 110, in probe_inspectors
    result = method(obj, **kwargs)
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/field.py", line 77, in field_to_swagger_object
    child_schema = self.probe_field_inspectors(field.child, ChildSwaggerType, use_references)
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/base.py", line 228, in probe_field_inspectors
    return self.probe_inspectors(
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/base.py", line 110, in probe_inspectors
    result = method(obj, **kwargs)
  File "../venv/lib/python3.8/site-packages/drf_yasg/inspectors/field.py", line 86, in field_to_swagger_object
    raise SwaggerGenerationError("cannot instantiate nested serializer as " + swagger_object_type.__name__)
drf_yasg.errors.SwaggerGenerationError: cannot instantiate nested serializer as Items
[05/Oct/2021 15:49:51] "GET /swagger/?format=openapi HTTP/1.1" 500 215395

Your Environment

Django==3.2.7
django-rest-framework==0.1.0
djangorestframework==3.12.4
drf-yasg==1.20.0
sminnee commented 2 years ago

I've had this issue too - it seems that read/write nested serializers aren't supported; only read-only

If you simply edit the offending line to allow items, it mostly works:

            if swagger_object_type != openapi.Schema and swagger_object_type != openapi.Items:
                raise SwaggerGenerationError("cannot instantiate nested serializer " + field.__class__.__name__ + " as " + swagger_object_type.__name__ )

However, the type of the value isn't inferred:

image