6

I am having a problem in defining the array of objects in swagger yaml. Swagger editor is giving an error everytime I try to define the type: array part of the yaml. I defined it, but it is not right as it is giving an error. Following is the json I am trying to define in swagger yaml.

{
    "CountryCombo": {
        "options": {
            "option": [{
                "id": "GB",
                "value": "GB Great Britain"
            }, {
                "id": "US",
                "value": "US United States"
            }, {
                "id": "AD",
                "value": "AD Andorra, Principality of"
            }]
        }
    }
}

I defined this json into swagger yaml like this but it is giving an error:

CountryCombo:
    type: object
    properties:
        options:
            type: object
            properties:
                option:
                    type: array
                    items:
                        - id:
                            type: string
                            description: GB
                          value:
                            type: string
                            description: GB Great Britain
                        - id:
                            type: string
                            description: US
                          value:
                            type: string
                            description: US United States
                        - id:
                            type: string
                            description: AD
                          value:
                            type: string
                            description: AD Andorra, Principality of

Can anyone suggest me how would I define this json in yaml following swagger specifications?

Paypal Paypal
  • 253
  • 1
  • 3
  • 10
  • Related: [Property reference to Model in OpenAPI 2.0 (nesting)](https://stackoverflow.com/q/26287962/113116) – Helen Dec 17 '19 at 13:28

3 Answers3

17

In a schema, you don't want to have the values, only the description of the values.

CountryCombo:
    type: object
    properties:
        options:
            type: object
            properties:
                option:
                    type: array
                    items:
                        type: object
                        properties:
                          id:
                            type: string
                          value:
                            type: string
Mohsen
  • 64,437
  • 34
  • 159
  • 186
16

The above answer is also right but I already implemented this in my yaml. I found that I can also define array by creating another definition.

CountryCombo:
    type: object
    properties:
        options:
            type: object
            properties:
                option:
                    type: array
                    items:
                        $ref: '#/definitions/Country_row'

Country_row:
    type: object
    properties:
      id:
        type: string
      value:
        type: string
Paypal Paypal
  • 253
  • 1
  • 3
  • 10
0

The accepted answer cannot achieve the output desired the question. Swagger documentation on examples explains how to add multiple examples for an array of objects:

definitions:
  ArrayOfCatalogItems:
    type: array
    items:
      $ref: '#/definitions/CatalogItem'
    example:
      - id: 38
        title: T-shirt
      - id: 114
        title: Phone

OP was actually on the right track, but made a syntactic mistake:

CountryCombo:

type: object
properties:
    options:
        type: object
        properties:
            option:
                type: array
                items:
                    - id:
                        type: string
                      value:
                        type: string
                    - id:
                        type: string
                      value:
                        type: string
                    - id:
                        type: string
                      value:
                        type: string
                example:
                    - id: GB
                      value: GB Great Britain
                    - id: US
                      value: US United States
                    - id: AD
                      value: AD Andorra, Principles of
josealvarez97
  • 75
  • 1
  • 8