Closed
Description
Today Grape supports Hash and Array as a group parameter type and raises UnsupportedGroupTypeError for everything else. We would like to be able to do type: JSON
, like so:
resource :reticulated_splines do
params do
requires :splines, type: JSON do
requires :id, type: Integer
requires :reticulated, type: Boolean
end
end
get do
params[:splines].map do |spline|
spline.merge(reticulated: !spline[:reticulated])
end
end
end
Today you have to workaround this, see ruby-grape/grape-on-rack@0f904bf:
resource :reticulated_splines do
before do
params.each_pair do |k, v|
params[k] = JSON.parse(v) rescue v
end
end
params do
requires :splines, type: Array do
requires :id, type: Integer
requires :reticulated, type: Boolean
end
end
get do
params[:splines].map do |spline|
spline.merge(reticulated: !spline[:reticulated])
end
end
end
Note that the JSON itself can be an array or a hash. So maybe we can just generalize:
requires :splines, type: Array, coerce_type: JSON do
requires :splines, type: Array, coerce_method: ->(val) { JSON.parse(val) }
requires :splines, type: Array, coerce_method: JSON.method(:parse)