Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define an array of objects in OpenAPI 3.0?

Tags:

openapi

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.
like image 943
Irgendw Pointer Avatar asked Dec 23 '22 16:12

Irgendw Pointer


1 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
like image 114
Helen Avatar answered Feb 09 '23 00:02

Helen