how to pass multi value query params in swagger

Viewed 20510

I have following service in swagger.yml. The service is written so that page_id can be passed multiple times. e.g /pages?page_id[]=123&page_id[]=542

I checked this link https://swagger.io/specification/ but couldnt understand how could i update yml so i could pass id multiple times.

I see that i have to set collectionFormat but dont know how.

I tried updating it like below but no luck https://github.com/OAI/OpenAPI-Specification/blob/master/versions/2.0.md.

it generates url like 'http://localhost:0000/pages?page_id=123%2C%20542`

 '/pages':
    get:
      tags:
        - 
      summary: get the list of pages
      operationId: getPages
      produces:
        - application/json
      parameters:
        - name: page_id
          in: query
          description: some description
          required: false
          type: string
          collectionFormat: multi
        - name: page_detail
          in: query
          description: some description
          required: false
          type: string
      responses:
        '200':
          description: OK
        '401':
          description: Authentication Failed
        '404':
          description: Not Found
        '503':
          description: Service Not Available
3 Answers

You are almost there. Name the parameter page_id[], make it type: array and use collectionFormat: multi:

      parameters:
        - name: page_id[]
          in: query
          description: some description
          required: false
          type: array
          items:
            type: string   # or type: integer or whatever the type is 
          collectionFormat: multi

Note that the requests will be sent with the [ and ] characters percent-encoded as %5B and %5D, because they are reserved characters according to RFC 3986.

http://example.com/pages?page_id%5B%5D=123&page_id%5B%5D=456

From the docs:

parameters:
- name: id
  in: path
  description: ID of pet to use
  required: true
  schema:
    type: array
    style: simple
    items:
      type: string

You have to define the parameter as array.

How to add default values to the query parameter of type array:

    parameters:
      - name: 'liabilityType[]'
        in: query
        description: liabilityType filters the servicers list according to liability types.
        required: false
        schema:
          type: array
          items:
            type: string
        collectionFormat: multi
        value:
          - CAR
          - HOUSE

I have attached a picture of how this code would look like in Swagger UI [1]: https://i.stack.imgur.com/MSSaJ.png

Related