Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I pass an array with grape swagger ui?

I have definition of api endpoint below:

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

I can't pass an array form the UI generated 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 the api from spec with an array it works. My client would like to try the whole api from Swagger, so I would like a solution which works with Swagger UI.

like image 701
Boti Avatar asked May 28 '14 19:05

Boti


1 Answers

my solution for all cases:

params do
  requires :ids, type: Array[Integer], desc: 'Array of group ids', 
           coerce_with: -> { |val| (val.is_a?(Array) ? val : val.to_s.split(',').map(&:strip)).map(&:to_i) }
end
like image 103
Viktor Ivliiev Avatar answered Nov 09 '22 00:11

Viktor Ivliiev