Swagger:将枚举定义重用为查询参数

rdr*_*rey 13 swagger swagger-2.0

我想definitions在查询字符串中使用作为参数定义的一部分定义的枚举.

我在definitionsSwagger 2.0规范文件中定义了Swagger Enum .

OperationType:
  type: string
  enum:
  - registration
  - renewal
Run Code Online (Sandbox Code Playgroud)

我可以在其他定义中创建对它的引用:

Operation:
  type: object
  properties:
    name:
      type: string
    type:
      $ref: '#/definitions/OperationType'
Run Code Online (Sandbox Code Playgroud)

我可以使用schema标签在参数为时引用它in: body,但不是在它的时候in: query

    - name: operation
      in: body
      description: description
      schema:
        $ref: '#/definitions/OperationType'
Run Code Online (Sandbox Code Playgroud)

我试着放弃schema:并提供参考enum:,但无法让它工作.

Ron*_*Ron 14

对于Swagger 2.0,我们限制了将模型定义用于除body参数之外的任何内容的能力.该definitions部分用于定义模式,该模式也可用于定义非对象.但是,只能在使用schema关键字的地方访问这些定义.如最初所述,schema非身体参数不可访问,因此,查询或路径参数不能使用,因此限制了重用这些定义的能力.

有一个开放的功能请求,要求在未来版本的规范中处理它.

  • 不,但看起来在3.0中它是可能的. (2认同)

Hel*_*len 6

这在 OpenAPI 3.0 中是可能的。所有参数现在都使用 a schema,并且通过扩展,可以$ref使用模式。

openapi: 3.0.0
...
paths:
  /something:
    get:
      parameters:
        - in: query
          name: action
          schema:
            $ref: '#/components/schemas/OperationType'
      ...

components:
  schemas:
    OperationType:
      type: string
      enum:
        - registration
        - renewal
Run Code Online (Sandbox Code Playgroud)