在 Swagger 中创建我自己的类型

Create my own type in Swagger

我有这个 swagger 的 YAML 代码,我需要创建自己的类型(名为 MyOwnType)。

如果我使用 "MyOwnType" 会出现编译错误。

paths:
  /in/total:
    get:
      summary: My summary.
      description: My description.

      parameters:
        - name: total
          in: query
          description: Total.
          required: true
          type: MyOwnType # -- THIS LINE OCCURS ERROR --
          format: MyOwnType
      responses:
        201:
          description: Respose
          schema:
            $ref: '#/definitions/MyOwnType'

definitions:
  MyOwnType:
    properties:
      var:
        type: string
        description: data.

我创建了一个定义 "MyOwnType",我可以这样使用:“$ref: '#/definitions/MyOwnType'” 在模式中。

但是如何使用参数类型的 "MyOwnType" 定义?

查询参数不能有 JSON 架构。如果你想为你的参数设置模式,你应该将参数的 in 更改为 bodyformData 并使用 schema key:

swagger: '2.0'
info:
  version: 0.0.0
  title: '<enter your title>'
paths:
  /in/total:
    get:
      summary: My summary.
      description: My description.

      parameters:
        - name: total
          in: body
          description: Total.
          required: true
          schema:
            $ref: '#/definitions/MyOwnType'
      responses:
        201:
          description: Respose
          schema:
            $ref: '#/definitions/MyOwnType'

definitions:
  MyOwnType:
    properties:
      var:
        type: string
        description: data.