Skip to content

MultipleChoiceField not exploding choices in Swagger UI #965

@sparktx-adam-gleason

Description

@sparktx-adam-gleason

Describe the bug
drf-spectacular version 0.26.1

serializers.MultipleChoiceField in a request Serializer results in invalid choice 400 error when selecting more than one option using the Swagger UI view.

To Reproduce

# serializers.py
choices = [
    "option1",
    "option2",
    "option3",
]

class MyCreateSerializer(serializers.Serializer):
    """Create serializer for saving my choices""""
    my_choices = serializers.MultipleChoiceField(choices=choices)

# views.py
@extend_schema(
    methods=["post"],
    request=serializers.MyCreateSerializer,
)
def save_choices(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        if serializer.is_valid(raise_exception=True):
            my_choices = serializer.validated_data.get("my_choices", [])
            services.save_choices(choices=my_choices)
            return Response({"success": True }, status=status.HTTP_200_OK)

In the Swagger UI, I see the multiple select widget with my choice options. Selecting one choice works but selecting more than one produces this error

status code: 400 Bad Request

{
  "my_choices": [
    "\"option1,option2,option3\" is not a valid choice."
  ]
}

Here is a cURL example

curl -X 'POST' \
  '<MY ENDPOINT URL>' \
  -H 'accept: application/json' \
  -H 'Authorization: Basic REDACTED' \
  -H 'Content-Type: multipart/form-data' \
  -H 'X-CSRFTOKEN: REDACTED' \
  -F 'my_choices=option1,option2,option3'

Expected behavior
Selecting more than one option for MultipleChoiceField would explode the choices by default

curl -X 'POST' \
  '<MY ENDPOINT URL>' \
  -H 'accept: application/json' \
  -H 'Authorization: Basic REDACTED' \
  -H 'Content-Type: multipart/form-data' \
  -H 'X-CSRFTOKEN: REDACTED' \
  -F 'my_choices=option1' \
  -F 'my_choices=option2' \
  -F 'my_choices=option3'

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfix confirmation pendingissue has been fixed and confirmation from issue reporter is pending

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions