【问题标题】:How to describe nested object in Swagger API?如何在 Swagger API 中描述嵌套对象?
【发布时间】:2019-07-19 06:41:45
【问题描述】:

我正在尝试使用 Swagger API 描述 API 端点响应的以下数据结构。(Swagger 描述如下所示)
但是,我收到以下错误。
- smallpower 行应该是对象。
我可以知道是什么问题吗?

实际 API 返回

{
    "smallpower": [
        {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    ],
    "oa": [
        {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    ],
    "light": [
        {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    {
            "A": "A",
            "B": 11.9,
            "C": true,
            "D": "D"
        },
    ]
}  

Swagger API 规范

  responses:
    '200':
      description: successful operation
      content:
        application/json:
          schema:
            type: object
            properties:
              - smallpower:
                type: object
                schema:
                  $ref: '#/components/schemas/SmallPower'
              - oa:
                type: object
                schema:
                  $ref: '#/components/schemas/OA'
              - light:
                type: object
                schema:
                  $ref: '#/components/schemas/light'

根据海伦的评论更新

【问题讨论】:

    标签: yaml swagger


    【解决方案1】:

    这是properties 的正确语法,其中每个属性都是$ref'erenced 架构的数组:

                properties:
                  smallpower:
                    type: array
                    items:
                      $ref: '#/components/schemas/SmallPower'
                  oa:
                    type: array
                    items:
                      $ref: '#/components/schemas/OA'
                  light:
                    type: array
                    items:
                      $ref: '#/components/schemas/light'
    

    【讨论】:

    • 感谢您的回答。它几乎是固定的。但我希望值是对象数组,而不仅仅是对象。你能帮我解决这个问题吗?我在上面添加了当前情况图片
    猜你喜欢
    • 1970-01-01
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-19
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多