Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add multiple example values in swagger properties?

I am using Swagger OpenAPI Specification tool, I have a string array property in one of the definitions as follows :

cities:
        type: array
        items:
          type: string
          example: "Pune"

My API produce JSON result so for above object following result appears in response :

{
  "cities": [
    "Pune"
  ]
}

Tried comma separated strings like below :

cities:
            type: array
            items:
              type: string
              example: "Pune", "Mumbai", "Bangaluru"

Expecting result as :

{
      "cities": [
        "Pune",
        "Mumbai",
        "Bangaluru"
      ]
    }

But the editor shows error. "Bad indentation"

I want to give multiple values to the example tag is there any way ?

Update

User Helen below has given correct answer I had indentaion problem hence there were nested arrays ( 2d arrays )

Correct way :

cities:
        type: array
        items:
          type: string
        example: 
        - Pune
        - Mumbai

My way ( Which was wrong )

cities:
        type: array
        items:
          type: string
          example: 
          - Pune
          - Mumbai

Look for indentation of example tag in above two cases which makes the difference, Its YAML indentation matters.

like image 234
Prashant Avatar asked Oct 05 '17 04:10

Prashant


People also ask

How do you add multiple hosts on Swagger?

Multiple hosts are supported in OpenAPI 3.0. 2.0 supports only one host per API specification (or two if you count HTTP and HTTPS as different hosts). A possible way to target multiple hosts is to omit the host and schemes from your specification and serve it from each host.


1 Answers

To display an array example with multiple items, add the example on the array level instead of item level:

cities:
  type: array
  items:
    type: string
  example:
    - Pune
    - Mumbai
    - Bangaluru

  # or
  # example: [Pune, Mumbai, Bangaluru]

In case of array of objects, the example would look like this:

type: array
items:
  type: object
  properties:
    id:
      type: integer
    name:
      type: string
example:
  - id: 1
    name: Prashant
  - id: 2
    name: Helen
like image 161
Helen Avatar answered Sep 19 '22 17:09

Helen