2

我真的很难理解examples回复部分的格式。我为 500 Internal Sever 错误定义了以下响应。

500InternalServerError:
    description: The server encountered an unexpected condition which prevented it from fulfilling the request
    schema:
      allOf:
        - $ref: '#/definitions/Failure'
    headers:
      X-Rate-Limit-Limit:
        description: The number of allowed requests in the current period
        type: integer
      X-Rate-Limit-Remaining:
        description: The number of remaining requests in the current period
        type: integer
      X-Rate-Limit-Reset:
        description: The number of seconds left in the current period
        type: integer
    examples:
      application/json:
        code: -1
        message: The server encountered an unexpected condition which prevented it from fulfilling the request

当我在 swagger-ui 中加载它时,它看起来像这样:

Json 响应格式

如何使响应格式化为多行并看起来像这样?:

{
  "code": "-1",
  "message": "The server encountered an unexpected condition which prevented it from fulfilling the request"
}
4

1 回答 1

1

响应级示例中缺少漂亮的打印似乎是 Swagger UI 3.0.x 中的一个错误或缺少的功能。随意在 GitHub 上提交问题。

解决方法是改用模式级示例:

definitions:
  Failure:
    type: object
    ...
    example:
      code: "-1"  # Quotes force the number to be treated as a string
      message: The server encountered an unexpected condition which prevented it from fulfilling the request


顺便说一句,单独allOf使用时不需要$ref(不与其他项目结合使用):

schema:
  $ref: '#/definitions/Failure'
于 2017-04-18T20:32:13.277 回答