Specify an array of strings as body parameter in swagger API

Achim picture Achim · Sep 2, 2016 · Viewed 48.7k times · Source

I would like to post an array of strings like

[
  "id1",
  "id2"
]

to a Swagger based API. In my swagger file, I have those lines:

paths:
  /some_url:
    post:
      parameters:
        - name: ids
          in: body
          required: true

What is the correct way to specify the type of ids as an array of strings?

Update:

According to the specification, the following should work in my option:

  parameters:
    - in: body
      description: xxx
      required: true
      schema:
        type: array
        items:
          type: string

https://github.com/Yelp/swagger_spec_validator does not accept it and returns a long list of convoluted errors, which look like the code expects some $ref.

Answer

Arnaud Lauret picture Arnaud Lauret · Sep 2, 2016

Your description of an array of string is correct, but the parameter definition misses the name property to be valid.

Here's a full working example:

swagger: "2.0"

info:
  title: A dummy title
  version: 1.0.0

paths:
  /path:
    post:
      parameters:
        - in: body
          description: xxx
          required: true
          name: a name
          schema:
            type: array
            items:
              type: string
      responses:
        default:
          description: OK

Try the online editor to check your OpenAPI (fka. Swagger) specs: http://editor.swagger.io/