Search code examples
modelsswaggerdefinitions

Swagger 2.0: How to declare a definition property of type model?


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!


Solution

  • 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".