tfranzel / drf-spectacular

Sane and flexible OpenAPI 3 schema generation for Django REST framework.
https://drf-spectacular.readthedocs.io
BSD 3-Clause "New" or "Revised" License
2.37k stars 263 forks source link

problem with many=True and multipart/form-data #1308

Open hihello1123 opened 1 week ago

hihello1123 commented 1 week ago

views.py

import json
from rest_framework.response import Response
from rest_framework.views import APIView
from drf_spectacular.utils import extend_schema
from test_app.serializer import *
class test_answer(APIView):
    @extend_schema(
        request={
            # "application/json": req_Serializer,
            "multipart/form-data": req_Serializer,
        },
        responses={"200": OK_response(), "400": ERROR_response()},
    )
    def post(self, req):
        data_list = req.data.get("list")
        post_list = req.POST.get("list")
        print("data ", data_list)
        print("post ", post_list)
        return Response({"message": "hi"})

serializer.py

from rest_framework import serializers

class item(serializers.Serializer):
    txt = serializers.CharField()

class req_Serializer(serializers.Serializer):
    list = item(many=True)

class OK_response(serializers.Serializer):
    status = serializers.IntegerField(default=200)

class ERROR_response(serializers.Serializer):
    status = serializers.IntegerField(default=400)

with these code i expected 2 item() objects in a list like below:

list = [
    { "txt":"asdf"},{ "txt","qwer"}
]

But swagger-ui makes below:

'list=[{"txt":"asdf"},"{\n  \"txt\": \"qwer\"\n}"]'

image

how can i make it right?

johnthagen commented 4 days ago

FYI, if you wrap you code blocks with

```py

They will syntax highlight and make it easier for the maintainer to read.

hihello1123 commented 3 days ago

@johnthagen thx for letting me know. I'm sorry that i'm not familiar with making a issue, Because this was my first Issue. Thank you again to let me know