Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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]

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...