21

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.
Helen
  • 87,344
  • 17
  • 243
  • 314
Irgendw Pointer
  • 1,770
  • 3
  • 30
  • 67
  • Related (or duplicate): [Return an array of object in SwaggerHub](https://stackoverflow.com/q/46167981/113116) – Helen Sep 04 '20 at 11:08

2 Answers2

41

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
Helen
  • 87,344
  • 17
  • 243
  • 314
5

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

your sir
  • 51
  • 1
  • 3