【问题标题】:How to define an array of objects in OpenAPI 3.0?如何在 OpenAPI 3.0 中定义对象数组?
【发布时间】:2020-09-04 09:44:29
【问题描述】:

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

不允许使用属性名称。

这显示在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.

【问题讨论】:

标签: openapi


【解决方案1】:

你需要在items里面多加两行来指定item类型是一个对象:

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

【讨论】:

    【解决方案2】:

    这是带有示例的对象数组:

    components:        
      schemas:
        abc:
          xml:
            wrapped : true    
            name: abc
          type: array
          items:
            type: object
            xml:
              name: 'item'
            properties: 
              Name:
                type: string
              age:
                type: integer
              enabled: 
                type: boolean
          example:
            - Name: no1
              age: 18
              enabled: true
            - Name: no2
              age: 20
              enabled: false
    

    json

    xml

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多