25

I'm having a hard time trying to figure out how I can nest models in OpenAPI 2.0.

Currently I have:

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

OtherModel:
  properties:
    otherProp:
      type: string   

I have tried many other ways:

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

None of the above seem to work.

However, with arrays works just fine:

prop3:
  type: array
  items:
    $ref: OtherModel
Promise Preston
  • 24,334
  • 12
  • 145
  • 143
Henrique Barcelos
  • 7,670
  • 1
  • 41
  • 66

2 Answers2

50

The correct way to model it in OpenAPI 2.0 would be:

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 use 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 schemas because the type is not inferred from other keywords.

Helen
  • 87,344
  • 17
  • 243
  • 314
Ron
  • 14,160
  • 3
  • 52
  • 39
5

Here's another trick that works. This solution applies to OpenAPI 3 – the latest version of the OpenAPI Specification as the point of answering this question.

Here's how:

Say, I have a User model with a State enum. I defined the State enum in a different schema and then referenced it in the User schema.

components:
  schemas:
    User:
      type: object
      properties:
        id:
          type: integer
          format: int64
        first_name:
          type: string
        last_name:
          type: string
        password:
          type: string
          format: password
        state:
          $ref: '#/components/schemas/State'
    State:
      type: string
      description: List of States
      enum:
        - State 1
        - State 2
        - State 3
        - State 4
        - State 5

Note that the enums were represented as arrays here, if you would love to represent them as hashes, check out this solution on Representing enum property in hash.

That's all.

I hope this helps

Promise Preston
  • 24,334
  • 12
  • 145
  • 143
  • What if I had another ref for those properties ` id: type: integer format: int64 first_name: type: string last_name: type: string password: type: string format: password` How would I combine them with the ref to the sate property ? – Mwibutsa Floribert Jul 27 '22 at 09:37
  • It is very interesting I cannot include `state.description` below `state.$ref`. – Nikolas Charalambidis Nov 10 '22 at 08:17