4

我正在使用 swagger python-flask 框架来创建一个 OpenAPI 3 API,它需要执行以下操作:

1. Upload a video file

2. Upload an array of images

3. Upload extra string data.

我可以使用以下方法成功上传单个文件:

MyFileUploadAPI:
 post:
  operationId: my_file_upload
  requestBody:
    content:
      multipart/form-data:
        schema:
          $ref: '#/components/schemas/videofile'

components:
  schemas:    
    videofile:
      type: object
      properties:
        upload:
         type: string
         format: binary


# This works fine
def my_file_upload(videofile):  
    file_name = videofile.filename

我可以使用以下方法上传一组图像和其他数据:

MyMultipleImagesUploadAPI:
 post:
  operationId: multiple_files
  requestBody:
    content:
      application/json:
        schema:
          $ref: '#/components/schemas/multiplefiles'
components:
  schemas:    
    multiplefiles:
      type: object
      properties:
        Images:
          items:
            $ref: '#/components/schemas/MyImage'
        TotalCount:
          type: integer

# This works fine
def multiple_files(body):    

    if connexion.request.is_json:
        body = multiplefiles.from_dict(connexion.request.get_json())                
        images = body._images
        count = body._totalcount

现在我希望将两者结合在一起(视频上传+多张图片+额外数据)所以我做了以下事情:

/MyEverythingAPI:
post:
  operationId: video_multiple_images
  requestBody:
    content:
      multipart/form-data:
        schema:
          $ref: '#/components/schemas/everything'

components:
  schemas:  
  everything:
    type: object
    properties:
      Video:
        type: string
        format: binary
      Images:
        type: array
        items:
          $ref: '#/components/schemas/MyImage'
      TotalCount:
        type: integer

# This has issues
def video_multiple_images(body, video_clip, **kwargs): 
   #body = {'images': ['[object File]'], 'totalcount': 2}
   uploaded_file_name = video_clip.filename

现在我发现有 2 个问题是不正确的:

问题 1. video_clip 没有传递给函数video_multiple_images
我调试并跟踪它,发现它没有被添加,因为没有 **kwargs 添加到我的函数签名中。当我添加它时,视频文件开始通过。这篇文章帮助我解决了这部分问题:https ://github.com/zalando/connexion/pull/753/files 我可以忍受这个问题,但想知道为什么会这样。

问题 2. 图像数组以 [object File]''[Object File]'' 等形式通过。这篇文章表明 OpenAPI 3 不支持此功能:在 Swagger openapi v3.0.0 中,面临多个文件上传的问题

那么如何传递视频文件+图像数组?

4

0 回答 0