How can I pass an array with grape swagger ui?

I have an api endpoint definition below:

params do requires :ids, type: Array, desc: 'Array of group ids' end 

I cannot pass an array from the user interface created by Swagger. If I enter [1, 2, 3, 4] or ids%5b%5d=1&ids%5b%5d=2&ids%5b%5d=3 , then both become invalid. If I call api from spec with an array, it will work. My client would like to try the entire Swagger api, so I would like to get a solution that works with the Swagger user interface.

+6
source share
3 answers

To clarify what Boti said, I think he meant that he changed the type of the array to a string and sent the string of the JSON array - the value "[1,2,3]" . Obviously this is not ideal, but it will work if you parse it on the API side. So the parameters will be -

 params do requires :ids, type: String, desc: 'Array of group ids' end 

To be clear, this is a problem with the Swagger screech user interface, especially because Grape Swagger supports Swagger 1.2. Grape Swagger has a problem sending the correct format for the Swagger array (see this Github question ). Grape does an excellent job of array type, you just cannot test it in Swagger-UI.

For our project, we simply did not use Swagger for this parameter. I would prefer the test platform (Swagger-UI) to work incorrectly than to edit otherwise the correct code in the actual product.

0
source

Here's how I did it with String parameters

 params do requires :array, type: String end post 'test_array' do params[:array] = params[:array].split(',') end 

then in the Swagger user interface I can pass a string to simulate an array.

item1. item2. item3 item1. item2. item3 instead of [item1. item2. item3] [item1. item2. item3]

this may not be a good approach, but may be an alternative.

0
source

Finally, I passed the JSON array as a String.

-3
source

Source: https://habr.com/ru/post/969998/


All Articles