如何在OpenAPI 3.0中定义对象数组?

时间:2020-09-04 09:44:29

标签: openapi

我正在尝试在数组中添加一个对象,但这似乎是不可能的。我尝试了以下方法,但总是收到错误消息:

不允许使用属性名称。

devices数组中定义的所有项目显示。如何在OpenAPI中定义数组中的项目?

  /demo/:
    post:
      summary: Summary
      requestBody:
        description: Description.
        required: true
        content:
          application/json:
            schema:
              type: object
              properties:
                Name:
                  type: string
                Number:
                  type: array
                  items:
                    type: string
                description:
                  type: string
                type:
                  type: string
                devices:
                  type: array
                  items:
                    Name:
                      type: string
                    descripiton:
                      type: string
                    Number:
                      type: integer
                    enabled:
                      type: boolean
              required:
                - Name
                - Number
                - devices
      responses:
        '201': # status code
          description: Created.
        '500':
          description: Error.
        '405':
          description: Invalid body has been presented.

1 个答案:

答案 0 :(得分:3)

您需要在items内添加两行以指定项目类型为对象:

            devices:
              type: array
              items:
                type: object      # <----------
                properties:       # <----------
                  Name:
                    type: string
                  descripiton:
                    type: string
                  Number:
                    type: integer
                  enabled:
                    type: boolean