How to add multiple example values in swagger propertise?

送分小仙女□ 提交于 2019-11-26 17:20:59

问题


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.


回答1:


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]


来源:https://stackoverflow.com/questions/46578110/how-to-add-multiple-example-values-in-swagger-propertise

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!