2016-11-08 4 views
1

У меня есть несколько API, и все они возвращают JSON с булевым полем с именем success.Swagger - Как написать общее поле ответа?

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

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

Могу ли я написать свою развязность 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: 
      ... 

Спасибо!

ответ

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'