Swagger - 如何编写常见的响应字段?

时间:2016-11-08 06:17:32

标签: swagger swagger-2.0

我有几个API,所有这些API都返回一个名为success的布尔字段的JSON。

API 1 {"success": true, "data": "some data"}

API 2 {"success": false, "error": "error message"}

我可以用模板之类的东西编写swagger 2.0文档,所以我不需要像这样在每个API中复制和粘贴成功字段部分吗?

  responses:
    200:
      schema:
        properties:
          success:
            type: boolean
             description: true if the request is successful.
          data:
             ...

  responses:
    200:
      schema:
        properties:
          success:
            type: boolean
             description: true if the request is successful.
          error:
             ...

谢谢!

1 个答案:

答案 0 :(得分:5)

是的,使用allOf作为公共字段:

responses:
  200:
    schema:
      allOf:
        - $ref: '#/definitions/common'
        - properties:
            data:
            # your details here

definitions:
  Common:
    type: object
    properties:
      success:
        type: boolean
         description: true if the request is successful.

此外:

      schema:
        allOf:
          - $ref: '#/definitions/Common'
          - properties:
              data:
                $ref: '#/definitions/Another'