2

I want to create a path which accepts any number of IDs separated with commas. It should accept all the following:

GET /pets/1,2  # returns info about pets with ids 1, 2
GET /pets/4,10,12,124,2  # same, for pets with ids 4, 10, 12, 124, 2

The book "building apis you won't hate" gave me this idea. How do I do that in swagger?

CrabMan
  • 1,578
  • 18
  • 37

1 Answers1

6

Swagger 2.0 supports the collectionFormat parameter. From the documentation:

Determines the format of the array if type array is used. Possible values are:

  • csv: comma separated values: foo,bar
  • ssv: space separated values: foo bar
  • tsv: tab separated values: foo\tbar
  • pipes: pipe separated values: foo|bar
  • multi: corresponds to multiple parameter instances instead of multiple values for a single instance foo=bar&foo=baz. This is valid only for parameters in "query" or "formData".

Default value is csv.

An example of use can be seen on the documentation:

{
  "get": {
    "description": "Returns pets based on ID",
    "summary": "Find pets by ID",
    "operationId": "getPetsById",
    "produces": [
      "application/json",
      "text/html"
    ],
    "responses": { ... }
  },
  "parameters": [
    {
      "name": "id",
      "in": "path",
      "description": "ID of pet to use",
      "required": true,
      "type": "array",
      "items": {
        "type": "string"
      },
      "collectionFormat": "csv"
    }
  ]
}
cassiomolin
  • 124,154
  • 35
  • 280
  • 359