Katsuya Obara
Katsuya Obara

Reputation: 963

How to describe nested object in Swagger API?

I am trying to describe below data structure for response of API endpoint using Swagger API.(Swagger description is shown below)
But, I get following error.
line - smallpower should be object.
May I know what is wrong?

actual API return

{
    "smallpower": [
        {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    ],
    "oa": [
        {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    ],
    "light": [
        {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    ]
}  

Swagger API specification

  responses:
    '200':
      description: successful operation
      content:
        application/json:
          schema:
            type: object
            properties:
              - smallpower:
                type: object
                schema:
                  $ref: '#/components/schemas/SmallPower'
              - oa:
                type: object
                schema:
                  $ref: '#/components/schemas/OA'
              - light:
                type: object
                schema:
                  $ref: '#/components/schemas/light'

Updated according to Helen's comment
enter image description here

Upvotes: 2

Views: 12814

Answers (1)

Helen
Helen

Reputation: 97540

Here is the correct syntax for properties where each property is an array of $ref'erenced schemas:

            properties:
              smallpower:
                type: array
                items:
                  $ref: '#/components/schemas/SmallPower'
              oa:
                type: array
                items:
                  $ref: '#/components/schemas/OA'
              light:
                type: array
                items:
                  $ref: '#/components/schemas/light'

Upvotes: 3

Related Questions