7

我正在尝试使用静态 swagger 文件记录一个 API,该文件可以返回一些 JSON,其中包含一个看起来像这样的数组:

[
  {
    "type": "type A",
    "field A": "this field is specific to type A"
  },
  {
    "type": "type B",
    "field B": "this field is specific to type B"
  }
]

我尝试了几种不同的方法来定义我的规范,使用多态性或显式定义多个示例。这些示例总是最终看起来像:

[
  {
    "type": "type A",
    "field A": "this field is specific to type A",
    "field B": "this field is specific to type B"
  }
]

要不就:

[
  {
    "type": "type A",
    "field A": "this field is specific to type A"
  }
]

有没有办法在我的 swagger 规范中定义一个示例,以便 swagger-ui 显示的示例有效负载将包含一个数组,其中包含一个类型 A 的示例和一个类型 B 的示例,就像我写的第一个 JSON 一样?

4

2 回答 2

7

事实上,你可以。在响应对象中,放置一个带有数组的示例对象作为 mime 类型的值。像这样:

    400:
      description: Bad Request
      examples:
        application/json:
          [
            {
              code:10000,
              message:"Missing Input Parameters",
              fieldA: "AAAAA"
            },{
              code:42,
              message:"Ask the question",
              fieldB: "BBBBBB"
            }
          ]
    default:
      description: Unexpected error
      schema:
        $ref: '#/definitions/Error'
于 2017-02-22T18:12:53.170 回答
1

你不能。

每个响应只能为每个 mime 类型定义一个示例:

{
  "description": "A response",
  "schema": {
    "type": "string"
    }
  },
  "examples": {
    "application/json": {
      "name": "Dog"
    },
    "application/xml": {
      "name": "Cat"
    }
  }
}

如果您想添加完整的场景,我建议您在 HTML 页面中编写(或生成)一个完整的场景示例并将其与externalDocs链接。您可以在根、操作、标签和模式中定义 externalDocs。

于 2015-06-27T20:37:09.153 回答