Link to the model on swagger 2.0 (nesting)

I find it hard to find how I can embed models in swagger 2.0.

I currently have:

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

I tried many other ways:

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

None of the above work.

However, working with arrays works just fine:

 prop3: type: array items: $ref: OtherModel 
+5
source share
3 answers

The correct way to model it in OpenAPI 2.0 is:

 swagger: '2.0' ... definitions: SomeModel: type: object properties: prop1: type: string prop2: type: integer prop3: $ref: '#/definitions/OtherModel' # <----- OtherModel: type: object properties: otherProp: type: string 

If you are using OpenAPI 3.0, models live in components/schemas instead of definitions :

 openapi: 3.0.1 ... components: schemas: SomeModel: type: object properties: prop1: type: string prop2: type: integer prop3: $ref: '#/components/schemas/OtherModel' # <----- OtherModel: type: object properties: otherProp: type: string 

Remember to add type: object to your object diagrams, because the type is not inferred from other keywords.

+18
source

I believe the Herc example should be

definitions:

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

I believe that Ron's example should look like this:

 definitions: OtherModel: properties: otherProp: type: string SomeModel: properties: prop1: type: string prop2: type: integer prop3: type: object $ref: #/definitions/OtherModel 
+1
source

Source: https://habr.com/ru/post/1204370/


All Articles