Search code examples
swaggerswagger-uiswagger-2.0swagger-editorswagger-codegen

Swagger YAML Query (type object) Parameter Definition Error


I'm getting the following error:

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

Here is my definition:

  /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]

I understand you can't use a schema definition as per this question: Swagger: Reusing an enum definition as query parameter

What do I need to modify to make the YAML compile without errors?


Solution

  • Objects in query parameters are not supported in OpenAPI/Swagger 2.0, but are supported in OpenAPI 3.0.

    If you stick with OpenAPI 2.0, you'll need to split the object into separate parameters, like so:

          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]
    

    In OpenAPI 3.0, you can define the parameter as object and use the style and explode keywords to specify how this object should be serialized.

          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]