Swagger 2.0:如何声明类型模型的定义属性?

时间:2014-09-18 15:32:06

标签: models swagger definitions

我想在Swagger 2.0

中声明类型的定义属性

这个定义是否正确? (特别是类型:StatusObject部分)

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"

谢谢!

2 个答案:

答案 0 :(得分:26)

参考其他模型/定义是使用" $ ref"。

完成的

以上代码段应如下所示:

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"

另一条评论 - 您正在使用"数字"作为id和代码的类型。 "数"还可以有一个小数点,我不确定你想要的(尤其是id)。如果您只想要整数,请考虑使用"整数"。

答案 1 :(得分:14)

在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"