2017-09-03 52 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' 
相關問題