27

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.
1

2 Answers 2

53

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
Sign up to request clarification or add additional context in comments.

Comments

7

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

2 Comments

actually, it's a much more accurate answer than most voted one
@OzzyCzech only if you need xml, which is not the case of this question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.