2014-10-09 93 views
5

我很難想出如何在swagger 2.0中嵌套模型。在swagger 2.0上的模型的屬性參考(嵌套)

目前我有:

SomeModel: 
properties: 
    prop1: 
    type: string 
    prop2: 
    type: integer 
    prop3: 
    type: 
     $ref: OtherModel 

OtherModel: 
    properties: 
    otherProp: 
     type: string 

我已經嘗試了很多其他方式:

prop3: 
    $ref: OtherModel 
# or 
prop3: 
    schema: 
    $ref: OtherModel 
# or 
prop3: 
    type: 
    schema: 
     $ref: OtherModel 

以上都不似乎工作。

然而,隨着陣列工作得很好:

prop3: 
    type: array 
    items: 
    $ref: OtherModel 

回答

16

正確的方法來模擬這將是:

SomeModel: 
properties: 
    prop1: 
    type: string 
    prop2: 
    type: integer 
    prop3: 
    $ref: OtherModel 

OtherModel: 
    properties: 
    otherProp: 
     type: string 
+0

你能看看http://stackoverflow.com/questions/35127186/swagger-2-x-reference-model-from-other-file是否有使用JSON而不是一個問題yaml? – Gobliins 2016-02-01 09:43:17

1

相信羅恩的例子應該閱讀:

definitions: 

    OtherModel: 
     properties: 
      otherProp: 
      type: string 

    SomeModel: 
     properties: 
      prop1: 
      type: string 
      prop2: 
      type: integer 
      prop3: 
      type: object 
      $ref: #/definitions/OtherModel 
4

我相信Herc的例子應該是

定義:

OtherModel: 
    properties: 
     otherProp: 
     type: string 

SomeModel: 
    properties: 
     prop1: 
     type: string 
     prop2: 
     type: integer 
     prop3: 
     type: object 
     $ref: '#/definitions/OtherModel' 
+0

必須改變他們的規範,因爲您的答案在我工作的同時接受的答案不起作用。 'prop3'我不需要'type:object' – chrisan 2017-03-21 19:33:39