按照swagger规范,如何将嵌套对象的json定义为yaml?

Pay*_*pal 5 json swagger swagger-ui swagger-2.0 swagger-editor

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

{
    "CountryCombo": {
        "options": {
            "option": [{
                "id": "GB",
                "value": "GB Great Britain"
            }, {
                "id": "US",
                "value": "US United States"
            }, {
                "id": "AD",
                "value": "AD Andorra, Principality of"
            }]
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我将这个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
Run Code Online (Sandbox Code Playgroud)

任何人都可以建议我如何按照swagger规范在yaml中定义这个json?

Moh*_*sen 15

在架构中,您不希望拥有值,只需要值的描述.

CountryCombo:
    type: object
    properties:
        options:
            type: object
            properties:
                option:
                    type: array
                    items:
                        type: object
                        properties:
                          id:
                            type: string
                          value:
                            type: string
Run Code Online (Sandbox Code Playgroud)


Pay*_*pal 11

上面的答案也是正确的,但我已经在我的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
Run Code Online (Sandbox Code Playgroud)