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

JAM*_*JAM 10 models definitions swagger

我想在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"
Run Code Online (Sandbox Code Playgroud)

谢谢!

Ron*_*Ron 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"
Run Code Online (Sandbox Code Playgroud)

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


小智 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"
Run Code Online (Sandbox Code Playgroud)