swagger - 打开 api 3 发布一组文件

标签 swagger openapi

我正在使用 swagger hub 到 create this API ;但它不支持 UI 中的多个文件,所以我不确定我这样做是否正确

我的目标是拥有以下内容

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

这必须是多部分的,因为它是文件;但不确定我是否正确设置了结构。

Swagger/开放 API 代码

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

最佳答案

根据 File Upload OpenAPI 3 规范中的部分:

文件上传

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

多文件上传

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

您当前的定义完全符合规范:

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
    ...

关于swagger - 打开 api 3 发布一组文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49433984/

相关文章:

spring - 无法推断基本 URL。当使用动态 servlet 注册或 API 位于 API 网关后面时,这种情况很常见

amazon-web-services - AWS CDK 如何根据 OpenApi 规范创建由 Lambda 支持的 API 网关?

servicestack - 如何隐藏不受 ServiceStack 的 SwaggerFeature 控制的路由?

swagger - 在 swagger 文档中使用对象类型查询参数

php - 在Swagger openAPI注释中发出 'Authorization: Bearer <token>'

express - 如何在 NestJS 中安装 Express 中间件(express-openapi-validator)?

Swagger OpenAPI 使用带有模式的对象而不是数组

swagger - 是否可以在 OpenAPI 3.0 的根级别指定默认请求/响应格式?

c# - API 版本控制 - 如果模型发生变化我该怎么办

swagger - 如何使用 swagger 在路径中定义可选参数