2015-06-19 83 views
4

我試圖記錄與靜態招搖文件的API,可以返回一些JSON包含數組,看起來是這樣的:如何在Swagger規範中定義不同示例的數組?

[ 
    { 
    "type": "type A", 
    "field A": "this field is specific to type A" 
    }, 
    { 
    "type": "type B", 
    "field B": "this field is specific to type B" 
    } 
] 

我試着用我的定義規範的幾個不同的方法多態或者明確定義多個例子。這些例子往往不是最終看上去像:

[ 
    { 
    "type": "type A", 
    "field A": "this field is specific to type A", 
    "field B": "this field is specific to type B" 
    } 
] 

或者只是:

[ 
    { 
    "type": "type A", 
    "field A": "this field is specific to type A" 
    } 
] 

有沒有辦法在我昂首闊步規範定義一個例子,這樣的招搖-UI中所示的例子有效載荷,將包含一個數組,其中包含類型A的示例和類型B的示例,就像我寫的第一個JSON一樣?

+0

[如何創建一個招搖的模式,包括不同類型的數組]的可能的複製(https://stackoverflow.com/questions/36318849/how-do-you-create-a -swagger-架構 - 即-包括-AN-陣列的變類型) – Helen

回答

2

你不能。在HTML頁面和鏈接

{ 
    "description": "A response", 
    "schema": { 
    "type": "string" 
    } 
    }, 
    "examples": { 
    "application/json": { 
     "name": "Dog" 
    }, 
    "application/xml": { 
     "name": "Cat" 
    } 
    } 
} 

如果你想添加完整的情況下,我建議你寫(或產生)一個完整的場景例如:

只能定義每個MIME類型一個例子每響應它與externalDocs。您可以在根,操作,標記和模式中定義externalDocs。

3

其實,你可以。在響應對象中,將一個帶有數組的示例對象作爲mime類型的值。像這樣:

400: 
     description: Bad Request 
     examples: 
     application/json: 
      [ 
      { 
       code:10000, 
       message:"Missing Input Parameters", 
       fieldA: "AAAAA" 
      },{ 
       code:42, 
       message:"Ask the question", 
       fieldB: "BBBBBB" 
      } 
      ] 
    default: 
     description: Unexpected error 
     schema: 
     $ref: '#/definitions/Error'