Swagger YAML 查询(类型对象)参数定义错误

Swagger YAML Query (type object) Parameter Definition Error

我收到以下错误:

Schema error at paths./cards/count.get.parameters[0] is not exactly one from <#/definitions/parameter>,<#/definitions/jsonReference>

这是我的定义:

  /cards/count:
    get:
      tags:
      - "cards"
      summary: "Number of Cards available"
      description: "Excludes cards which the user has answered correctly in the past."
      operationId: "countCards"
      produces:
      - "application/json"
      parameters:
      - name: "tagFilter"
        in: "query"
        description: "Input is optional - left blank will return all tags and a count"
        type: "object"
        properties:
          tags_any:
            type: "array"
            items:
              type: "integer"
              format: "int64"
              enum: [1,2,3]
          tags_all:
            type: "array"
            items:
              type: "integer"
              format: "int64"
              enum: [4,5,6]
          tags_not:
            type: "array"
            items:
              type: "integer"
              format: "int64"
              enum: [4,5,6]

我知道您不能根据这个问题使用模式定义:

我需要修改什么才能使 YAML 编译无误?

查询参数中的对象在 OpenAPI/Swagger 2.0 中不受支持,但在 OpenAPI 3.0 中受支持。

如果您坚持使用 OpenAPI 2.0,则需要将对象拆分为单独的参数,如下所示:

      parameters:
        - in: query
          name: tags_any
          type: array
          items:
            type: integer
            format: int64
            enum: [1,2,3]
        - in: query
          name: tags_all
          type: array
          items:
            type: integer
            format: int64
            enum: [4,5,6]
        - in: query
          name: tags_not
          type: array
          items:
            type: integer
            format: int64
            enum: [4,5,6]

在 OpenAPI 3.0 中,您可以将参数定义为对象,并使用 style and explode 关键字指定应如何序列化此对象。

      parameters:
        - name: tagFilter
          in: query
          description: Input is optional - left blank will return all tags and a count

          # Send the object as ?prop1=value1&prop2=value2
          # This is the default serialization method for objects in query parameters
          style: form
          explode: true

          schema:
            type: object
            properties:
              tags_any:
                type: array
                items:
                  type: integer
                  format: int64
                  enum: [1,2,3]
              tags_all:
                type: array
                items:
                  type: integer
                  format: int64
                  enum: [4,5,6]
              tags_not:
                type: array
                items:
                  type: integer
                  format: int64
                  enum: [4,5,6]