open api 3 posting an array of files

2019-07-22 18:54发布

问题:

I am using swagger hub to create this API; but it doesn't support multi files in the UI so I am unsure if I am doing this right

My goal is to have the following

item:{Json describe the item}
images[] = images for item posted as an array
titles[] = Parallel array to images that has the title for image
alt_texts[] = Parallel array to images that has the alt text for image

This has to be multipart since it is files; but am unsure if I setup the structure correctly.

Swagger/Open API Code

post:
  summary: Add a new item to the store
  description: ''
  operationId: addItem
  requestBody:
    content:
      multipart/form-data:
        schema:
          $ref: '#/components/schemas/NewItemWithImage'
    description: Item object that needs to be added to the store
    required: true


NewItemWithImage:
  type: object
  properties:
    item:
      $ref: '#/components/schemas/NewItem'
    images[]:
      type: array
      items:
        type: string
        format: binary
    titles[]:
      type: array
      items:
        type: string
    alt_texts[]:
      type: array
      items:
        type: string
    variation_ids[]:
      type: array
      items:
        type: string
  required:
    - item

回答1:

According to File Upload section in OpenAPI 3 specification:

File Upload

Files use a type: string schema with format: binary or format: base64, depending on how the file contents will be encoded.

Multiple File Upload

Use the multipart media type to define uploading an arbitrary number of files (an array of files):

      requestBody:
        content:
          multipart/form-data:
            schema:
              type: object
              properties:
                filename:
                  type: array
                  items:
                    type: string
                    format: binary

You current definition corresponds to the specification precisely:

requestBody:
  content:
    application/json:
      schema:
        $ref: '#/components/schemas/NewItemWithImageUrl'
    multipart/form-data:
      schema:
        $ref: '#/components/schemas/NewItemWithImage'

NewItemWithImage:
  type: object
  properties:
    item:
      $ref: '#/components/schemas/NewItem'
    images[]:
      type: array
      items:
        type: string
        format: binary
    titles[]:
      type: array
      items:
        type: string
    ...


回答2:

The code for swagger-ui is failing as of today in curlify.js

        if (v instanceof win.File) {
          curlified.push( `"${k}=@${v.name}${v.type ? `;type=${v.type}` : ""}"` )
        } else {
          curlified.push( `"${k}=${v}"` )
        }

curlify.js is not taking the array into account and is sending:

curl -X POST "http://localhost:9122/upload-all" -H "accept: */*" -H "Content-Type: multipart/form-data" -F "my-attachment=[object File],[object File]"

and not something like:

curl -X POST "https://foo/v1/api/upload/" -H "accept: application/json" -H "Content-Type: multipart/form-data" -F "myfile=@bla;type=application/zip" -F "myfile=@foo;type=application/zip"