无法生成包含多部分/表单数据内容类型的swagger文件
描述
我有一个上传文件的请求,在那里我发送多部分/表单数据的文件。我试图将表单数据描述为
这就是我在邮递员中的要求。
当我试图生成一个swagger文件时。它给出了以下错误drf_yasg.errors.SwaggerGenerationError:当请求有请求体时不能添加表单参数;您忘记在视图上设置适当的解析器类了吗?
最小再生产
@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')
})
)
}
)
发布于 2022-03-29 06:53:41
在视图类中,需要设置MultiPartParser类,定义所使用的媒体类型:
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
https://stackoverflow.com/questions/70847708
复制相似问题