How to define an array of objects in OpenAPI 3.0?

Viewed 14057

I'm trying to add an object in an array, but this seems not be possible. I've tried the following, but I get always the error:

Property Name is not allowed.

This is shown for all items defined in the devices array. How can I define items in an array in 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.
2 Answers

You need to add two extra lines inside items to specify that the item type is an object:

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

This is the array of objects with examples:

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

Related