kin-openapi icon indicating copy to clipboard operation
kin-openapi copied to clipboard

A query parameter of type array fails a minItems: validation when omitted even though parameter optional

Open vulcanfan opened this issue 2 years ago • 1 comments

Hi,

I'm having some trouble with the following snippet under get: and parameters:

        - name: group-id
          in: query
          description: Group IDs
          schema:
            type: array
            items:
              $ref: '#/components/schemas/GroupId'
            minItems: 1
          style: form
          explode: false

The parameter is optional (no required: true). Yet when the parameter is omitted completely from the query parameter list in a HTTP Request then validation fails with the error 'Min items is 1'. Even though the parameter itself is optional.

Looking at the code, in ValidateParameter() in validate_request.go there is a test 'if value == nil' before checking parameter.Required. However if a query parameter such as above is missing completely then 'value' still gets decoded as the empty slice []interface{}, which is different to being nil in Go. So the 'test == nil' fails, the Required flag is not checked and it moves on to checking MinItems when it shouldn't in this case I don't believe, thus generating the validation error.

Is my interpretation correct here?

Many thanks

vulcanfan avatar Sep 15 '21 16:09 vulcanfan

Is my interpretation correct here?

There's only one way to be sure ;)

fenollp avatar Sep 16 '21 09:09 fenollp