Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass array as query parameter in Swagger OpenApi 3.0

Ive currently given the below code to get an array of values as query paramters (in Node-express, nodejs v14.17 and OpenAPI version 3.0.1),

       - name: abcd
          description:  abcd
          in: query
          required: false
          schema:
            type: array
            items:
              type: string

but it send the req as an array of values(type is object) only if there are atleast two of them. For a single value, the type is string.How to always get req as array itself?

enter image description here

For example, if i give a single value say
"Hello", console.log(typeof(req.query["abcd"]),req.query["abcd"])
O/P: string Hello
but if i give "Hello" and "World",
O/P: object ['Hello','World']

like image 547
fractal397 Avatar asked Feb 03 '26 20:02

fractal397


1 Answers

As this answer explains, when a query parameter gets passed a single value, e.g. ?abcd=hello, Express parses it as a regular value (i.e. string) rather than an array.

To pass a one-value array, you need to append [] at the end of the query parameter name, i.e. ?abcd[]=hello. This means you need to change the parameter name in your OpenAPI file:

       - name: abcd[]    # <------------
         description:  abcd
         in: query
         required: false
         schema:
           type: array
           items:
             type: string
like image 63
Helen Avatar answered Feb 06 '26 10:02

Helen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!