Swagger-php: How to use arrays in swagger via formData

Created on 3 Apr 2017  路  9Comments  路  Source: zircote/swagger-php

I want to receive in my api via $_POST (formData) an array like this:

answers[458]=hi&answers[90]=hello
.

array(1) {
  ["answer"]=>
  array(2) {
    [458]=>
    string(2) "hi"
    [90]=>
    string(5) "hello"
  }
}

When i use postman i can do this so for my code there is no problem, but now i want to use swagger and thats where i cant get it to work.

i've tried

/**
 * @SWG\Post(
 *   path="/sec/answer",
 *   summary="location",
 *   tags={"question"},
 *   @SWG\Parameter(
 *         name="answer",
 *         in="formData",
 *         description="",
 *         type="array",
 *         @SWG\Items(
 *             type="string"
 *         )
 *   ),
 *   @SWG\Response(
 *     response=200,
 *     description="array"
 *   )
 * )
 */

But the problem with this is that i get a field with multiple lines in swagger and when i fill this with: hi hello

this results in the following request:

'answer=hi&answer=hello'
with this result:

array(1) {
  ["answer"]=>
  string(5) "hello"
}

i know that with this option i cant receive an array with a variable key(because i cant fill in the key), but as far as i can see this isn't even an array swagger sends a wrong request with overruling parameters.

What to do? I hope someone can help me

Ps. i have also tried an array within an array but this has the same result

Most helpful comment

```

  • @SWG\Parameter(
  • name="answers[]",
  • in="query",
  • description="Your description",
  • required=true,
  • type="array",
  • collectionFormat="multi",
  • @SWG\Items(type="string")
  • )
    ```

All 9 comments

@bfanger Perhaps you know this?

I believe name="answer[]" works for php backends.

That result in 'answers%5B%5D=q'

You say it like thats a bad thing.

url: test.php?answers%5B%5D=q

test.php

<?php
var_dump($_GET['answers']);

Output:

array(1) {
  [0]=>
  string(1) "q"
}

@bfanger if I post multiple items for answer from swagger ui , am able to receive all in single element of array.

Example :
array(1) {
[0]=>
string(1) "1,2,3"
}

Which ideally should be
array(3){
[0]=>1,
[1]=>2,
[2]=>3,
}

right? What should be done ?

@ricardovanlaarhoven where you able to solve the problem? Am facing similar issue

```

  • @SWG\Parameter(
  • name="answers[]",
  • in="query",
  • description="Your description",
  • required=true,
  • type="array",
  • collectionFormat="multi",
  • @SWG\Items(type="string")
  • )
    ```
*   @SWG\Parameter(
*       name="answers[]",
*       in="query",
*       description="Your description",
*       required=true,
*       type="array",
*       collectionFormat="multi",
*       @SWG\Items(type="string")
*   )

cool !!!

Stumbled across while trying to figure this out for swagger-php 3. In case you found here as well, it should be

     * @OA\Parameter(
     *     name="answers[]",
     *     in="query",
     *     @OA\Schema(type="array", @OA\Items(type="string"))
     * )

The paramater serialization page might also be relevant for you https://swagger.io/docs/specification/serialization/

Was this page helpful?
0 / 5 - 0 ratings

Related issues

huksley picture huksley  路  4Comments

DerManoMann picture DerManoMann  路  3Comments

mostafaabdelbaset picture mostafaabdelbaset  路  5Comments

hpatoio picture hpatoio  路  3Comments

birendragurung picture birendragurung  路  3Comments