How to pass array of string in swagger form data field

Viewed 3292

I want to pass an array inside form data like below:

but I am getting the whole array as a string in NodeJS console like this:

{
  targetUniversity: "['613e3ecfefa725074cb17968', '613e3ecfefa725074cb17969']",
  targetBusinessType: "['freelancer','sw dev']",
}

The swagger file looks something like this,

"/announce": {
      "post": {
        "tags": ["Announcement"],
        "description": "Make an announcement",
        "parameters": [
          {
            "name": "targetUniversity",
            "in": "formData",
            "type": "array",
            "description": "University ID in array []- from DD"
          },
          {
            "name": "targetBusinessType",
            "in": "formData",
            "type": "array",
            "description": "Business type (string - name) in array []"
          }
        ],
        "produces": ["application/json"],
        "responses": {
          "201": {
            "description": "announced successfully"
          }
        }
      }
    }

I just want the array itself, not the array in string format.

2 Answers

Array parameters need the items keyword to define the type of array items. Also, the operation must specify what MIME type(s) it consumes:

        "consumes": [
          "application/x-www-form-urlencoded"
        ],
        "parameters": [
          {
            "name": "targetUniversity",
            "in": "formData",
            "type": "array",
            "items": {           // <------
              "type": "string"
            },
            "description": "University ID in array []- from DD"
          },
          {
            "name": "targetBusinessType",
            "in": "formData",
            "type": "array",
            "items": {           // <------
              "type": "string"
            },
            "description": "Business type (string - name) in array []"
          }
        ],

In Swagger UI, enter the array items one per line and without quotes:

Array parameters in Swagger UI

You can specify arrays in OAS3 as follows;

"parameters": [
    {
      "name": "targetUniversity",
      "in": "formData", -->> according to comments this should be replaced by requestBody in OAS3
      "schema": {
        "type": "array",
        "items": {
          "type": "string"
        }
    ...

The result will be like;

enter image description here

Related