Skip to content
This repository has been archived by the owner on Jan 21, 2024. It is now read-only.

Query parameter declared as array cannot contain only one value #31

Open
leflamm opened this issue May 10, 2017 · 1 comment
Open

Query parameter declared as array cannot contain only one value #31

leflamm opened this issue May 10, 2017 · 1 comment

Comments

@leflamm
Copy link

leflamm commented May 10, 2017

Hi,

a query parameter that is declared as type: array isn't allowed to contain only one value.

#%RAML 1.0
title: foo
version: v0.0.1

/bar:
  get:
    queryParameters:
      q:
        type: array
        items: string
    responses:
      200:
        body:
          application/json:
            example: |
              OK

Providing one query parameter causes an error...

$ curl "localhost:3333/bar?q=narf"
{
   "errors":[
      {
         "type":"query",
         "dataPath":"q",
         "keyword":"type",
         "schema":"array",
         "data":"narf",
         "message":"Invalid type, expected array"
      }
   ],
   "stack":"BadRequestError: Request failed to validate against RAML definition\n    at createValidationError (/home/cf/.nvm/versions/node/v6.9.4/lib/node_modules/osprey-mock-service/node_modules/osprey-method-handler/osprey-method-handler.js:746:14)\n    at ospreyQuery (/home/cf/.nvm/versions/node/v6.9.4/lib/node_modules/osprey-mock-service/node_modules/osprey-method-handler/osprey-method-handler.js:210:19)\n    at handle (/home/cf/.nvm/versions/node/v6.9.4/lib/node_modules/osprey-mock-service/node_modules/compose-middleware/lib/index.js:56:16)\n    at dispatch (/home/cf/.nvm/versions/node/v6.9.4/lib/node_modules/osprey-mock-service/node_modules/compose-middleware/lib/index.js:39:20)\n    at next (/home/cf/.nvm/versions/node/v6.9.4/lib/node_modules/osprey-mock-service/node_modules/compose-middleware/lib/index.js:37:24)\n    at ospreyAccepts (/home/cf/.nvm/versions/node/v6.9.4/lib/node_modules/osprey-mock-service/node_modules/osprey-method-handler/osprey-method-handler.js:187:12)\n    at handle (/home/cf/.nvm/versions/node/v6.9.4/lib/node_modules/osprey-mock-service/node_modules/compose-middleware/lib/index.js:56:16)\n    at dispatch (/home/cf/.nvm/versions/node/v6.9.4/lib/node_modules/osprey-mock-service/node_modules/compose-middleware/lib/index.js:39:20)\n    at IncomingMessage.next (/home/cf/.nvm/versions/node/v6.9.4/lib/node_modules/osprey-mock-service/node_modules/compose-middleware/lib/index.js:37:24)\n    at emitNone (events.js:86:13)"
}

... providing two or more works fine

$ curl "localhost:3333/bar?q=narf&q=poit"
OK
$ curl "localhost:3333/bar?q=narf&q=poit&q=zort"
OK
@jstoiko
Copy link
Contributor

jstoiko commented Aug 7, 2017

The issue is that a single q is passed as a string to the validator, and hence it does not validate against type: array. For reference, the RAML spec has a section about this scenario:

RAML does not define validation when a query parameter declaration specifies any of the following types for the value of the query parameter: an object type, a union of non-scalar types, or an array type if the underlying type of the array is an object type or union of non-scalar types. Processors MAY default to treating the format of the query parameter value as JSON in applying the type to instances of that query parameter, or they MAY allow other treatments based on annotations.

Have you tried declaring q as type: array | string? wouldn't it be a more accurate definition in your case?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants