2017-09-03 51 views
1

这里是新手。我已经通过了swagger primer,据我所知,下面的示例应该可以工作。Swagger语法:如何从可重复使用的响应中引用模型定义

我的响应类型只是不同结构的数组(这些结构在全局定义部分中定义,以减少膨胀,因为它们可能是嵌套的,因此可以重用)。

这里是我的定义:

consumes: 
    - application/json 
produces: 
    - application/json 
schemes: 
    - http 
swagger: '2.0' 
[...Additional details excluded...] 

paths: 
    /first: 
    get: 
     responses: 
     '200': 
      $ref: '#/responses/response1' 
    /second: 
    get: 
     responses: 
     '200': 
      $ref: '#/responses/response2' 


definitions: 
    ObjectA: 
    type: object 
    properties: 
     listOfObjBs: 
     type: array 
     items: 
      $ref: '#/definitions/ObjectB' 
    ObjectB: 
    type: object 
    properties: 
     listOfObjCs: 
     type: array 
     items: 
      $ref: '#/definitions/ObjectC' 
    ObjectC: 
    description: A build 
    type: object 
    properties: 
     someNumericData: 
     type: integer 
     format: int64 

responses: 
    response1: 
    description: There are 2 types of responses, this is the first kind. 
    schema: 
     type: object 
    headers: 
     data: 
     type: array 
     items: 
      $ref: '#/definitions/ObjectA' 
    response2: 
    description: This is the second kind. 
    schema: 
     type: object 
    headers: 
     data: 
     type: array 
     items: 
      $ref: '#/definitions/ObjectC' 

不过,我跑入招摇网页编辑器验证问题。在回应[ '响应1']

架构错误标题[ '数据']项目应 没有额外propertiesadditionalProperty:。$裁判

语义错误的responses.response1.headers.data.items。 $ REF项 $参无法匹配任何以下的: “#/定义”, “#/参数” 在响应[ '响应2']

模式错误报头[ '数据']。物品应该是 没有附加属性additionalProperty:$裁判

在responses.response2.headers.data.items语义错误$ REF项 $裁判不能匹配任何以下的: “#/定义”, “#/参数

它看起来像我使用json引用不正确,但我不知道为什么。我也试着把response1和response2放在定义部分并直接引用它们(例如直接在'#/ definitions/response1'而不是'#/ responses/response1'下直接指向$ ref下的路径)。但是我从编辑中得到一个错误,说我不能直接引用定义。

什么是构造这个定义的正确方法?

回答

1

对身体的反应有schema。要引用一个模型定义,使用$ref基准为schema值:

responses: 
    response1: # <--- This node is on the same level as the status codes '200'/'404' etc. 
    description: There are 2 types of responses, this is the first kind. 
    schema: 
     $ref: '#/definitions/ObjectA' 

     # Or if the response is an array: 
     # type: array 
     # items: 
     # $ref: '#/definitions/ObjectA' 


    response2: 
    description: This is the second kind. 
    schema: 
     $ref: '#/definitions/ObjectC' 

在您的示例中的错误是把headers下引用。 headers部分定义响应的HTTP标头,例如X-RateLimitSet-Cookie,而不是实际的主体有效负载。

response1: 
    description: There are 2 types of responses, this is the first kind. 
    schema: 
     type: object 

    # Errors were caused by this 
    headers: 
     data: 
     type: array 
     items: 
      $ref: '#/definitions/ObjectA' 
相关问题