2014-10-09 95 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