10

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

谢谢!

4

2 回答 2

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)。如果您只想要整数,请考虑使用“整数”。

于 2014-10-16T18:14:05.703 回答
15

在 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"
于 2015-06-16T12:51:31.197 回答