10

I want to declare a definition property of type model in Swagger 2.0

Is this definition correct? (specially the type: StatusObject part)

definitions:
  MyObject:
    type: "object"
    properties:
      id:
        type: "number"
      country:
        type: "string"
      status:
        type: StatusObject
  StatusObject:
    type: "object"
    properties:
      code:
        type: "number"
      shortMessage:
        type: "string"
      message:
        type: "string"

Thanks!

JAM
  • 323
  • 2
  • 4
  • 14

2 Answers2

26

Referring to other models/definitions is done using "$ref".

The above snippet should look like this:

definitions:
  MyObject:
    type: "object"
    properties:
      id:
        type: "number"
      country:
        type: "string"
      status:
        $ref: StatusObject
  StatusObject:
    type: "object"
    properties:
      code:
        type: "number"
      shortMessage:
        type: "string"
      message:
        type: "string"

Another comment - you're using "number" as the type for id and code. "number" can also have a decimal point which I'm not sure you want (especially for id). If you want whole numbers only, consider using "integer".

Ron
  • 14,160
  • 3
  • 52
  • 39
15

In Swagger 2.0:

definitions:
  MyObject:
    properties:
     id:
      type: "number"
     country:
      type: "string"
     status:
      $ref: "#/definitions/StatusObject"
  StatusObject:
   properties:
    code:
     type: "number"
    shortMessage:
     type: "string"
    message:
     type: "string"
Etienne
  • 729
  • 7
  • 11