【问题标题】:Following swagger specifications, how can I define json of nested objects to yaml?遵循 swagger 规范,如何将嵌套对象的 json 定义为 yaml?
【发布时间】:2016-03-12 08:05:03
【问题描述】:

我在 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

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

【问题讨论】:

标签: json swagger swagger-ui swagger-2.0 swagger-editor


【解决方案1】:

在架构中,您不希望有值,只希望有值的描述。

CountryCombo:
    type: object
    properties:
        options:
            type: object
            properties:
                option:
                    type: array
                    items:
                        type: object
                        properties:
                          id:
                            type: string
                          value:
                            type: string

【讨论】:

    【解决方案2】:

    上面的答案也是正确的,但我已经在我的 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
    

    【讨论】:

    • 我喜欢这个版本,因为我会在多个地方重复使用 Country_row 定义。
    • 这绝对是最好的解决方案
    【解决方案3】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-16
      • 2021-05-19
      • 2017-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 2019-10-02
      相关资源
      最近更新 更多