Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specify an array of strings as body parameter in swagger API

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.

like image 894
Achim Avatar asked Sep 01 '16 22:09

Achim


People also ask

How do you pass multiple parameters in Swagger?

If you are trying to send a body with multiple parameters, add an object model in the definitions section and refer it in your body parameter, see below (works with editor.swagger.io):

What is a body parameter in Swagger?

In Swagger terms, the request body is called a body parameter. There can be only one body parameter, although the operation may have other parameters (path, query, header). Note: The payload of the application/x-www-form-urlencoded and multipart/form-data requests is described by using form parameters , not body parameters.

How to add an array of strings in Swagger?

Let's see how to add one. 3. YAML Firstly, we start by specifying the array of strings in Swagger using YAML notation. In the schema section, we include type: array with items String. To better document the API and instruct the user, we can use the example label of how to insert values:

What is Swagger in REST API?

1. Overview Swagger is a set of specifications to document and describe REST APIs. It also provides example values for the endpoint parameters. In this tutorial, we'll show how to produce a default example value for String arrays, as this behavior is not enabled by default. 2.

How to modify a swagger form with JSON body?

In such case, your swagger document need to be modified as follows: When request body is json, a parameter with in: body is used instead of multiple parameters of in: formData. If in is body, a schema object is required. Defined the json properties under schema.


3 Answers

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/

like image 105
Arnaud Lauret Avatar answered Sep 23 '22 19:09

Arnaud Lauret


I have created a swagger issue as the help provided by Arnaud, although is valid yaml, will give you NPE exceptions when trying to generate. You will need to provide an object like the following:

  myDataItem:
    type: object
    description: A list of values
    required:
      - values
    properties:
      values:
        type: array
        items:
            type: string

And then refer to it (in your post item etc):

  schema:
    $ref: "#/definitions/myDataItem"

For reference the github issue:

https://github.com/swagger-api/swagger-codegen/issues/6745

Note, the issue has been fixed in version 2.3.0 and higher, ideally you should upgrade to that version.

like image 45
PeterS Avatar answered Sep 22 '22 19:09

PeterS


None of the answers worked for me. As it is stated in the following Baeldung article:

To better document the API and instruct the user, we can use the example label of how to insert values

So the full working example would be something like that:

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
            example: ["str1", "str2", "str3"]
      responses:
        default:
          description: OK

You can check how the Example Value is now better informed in the Swagger editor.

like image 34
Carlos Cavero Avatar answered Sep 22 '22 19:09

Carlos Cavero