6

我在 swagger yaml 中定义对象数组时遇到问题。每次我尝试定义类型时,Swagger 编辑器都会出错:yaml 的数组部分。我定义了它,但它不正确,因为它给出了错误。以下是我试图在 swagger yaml 中定义的 json。

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

我像这样将这个json定义为swagger yaml,但它给出了一个错误:

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

谁能建议我如何按照招摇规范在 yaml 中定义这个 json?

4

3 回答 3

17

在模式中,您不希望有值,只希望有值的描述。

CountryCombo:
    type: object
    properties:
        options:
            type: object
            properties:
                option:
                    type: array
                    items:
                        type: object
                        properties:
                          id:
                            type: string
                          value:
                            type: string
于 2016-03-14T20:48:22.760 回答
16

上面的答案也是正确的,但我已经在我的 yaml 中实现了这一点。我发现我也可以通过创建另一个定义来定义数组。

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
于 2016-03-28T12:28:29.347 回答
0

接受的答案无法达到问题所需的输出。关于示例的 Swagger 文档解释了如何为对象数组添加多个示例:

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

OP实际上是在正确的轨道上,但犯了一个语法错误:

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
于 2021-12-09T19:07:35.947 回答