OpenAPI 3-如何在架构中使用允许的键值属性描述数组?

时间:2020-07-09 19:14:02

标签: api swagger openapi

我正在尝试创建一个包含键-值数组的架构,但是属性本身是单独定义的。例如,假设我要描述一个具有以下属性的文件:

  • id
  • 文件名
  • fileType

其中,fileType是一个具有ID的对象,并带有一组特定的可接受键/值组合,例如

  • 0,单词
  • 1,Excel
  • 2,PDF
  • ...等等

我对如何用OpenApi 3描述这一点感到有些困惑,因为从文档中我似乎找不到找到描述具有特定键值组合的数组的方法。

现在我正在使用下面的枚举

components:
  schemas:
   fileType:
     type: string
     description: file type
     enum:
      - Word
      - Excel
      - PDF

但这是不够的。 谢谢

1 个答案:

答案 0 :(得分:1)

您可以使用类似这样的内容:

您的fileType模式

components:
  schemas:
    fileType:
      type: object
      description: file type
      properties:
        1:
          type: string
          example: "Word"
        2:
          type: string
          example: "Excel"
        3:
          type: string
          example: "PDF"

这是您的requestBudy

requestBody:
  description: File
  content:
    '*/*':
      schema:
        properties:
          id:
            type: string
          fileName:
            type: string
          fileType:
            $ref: '#/components/schemas/fileType'

看起来像

enter image description here

希望获得帮助。