grape icon indicating copy to clipboard operation
grape copied to clipboard

declared(params) - "no implicit conversion of Symbol into Integer" when receiving blank array

Open jacek213 opened this issue 4 years ago • 3 comments

  1. I have an optional array parameter. In some cases I want it to be blank, but if the items are passed, they must match the schema inside.
params do
  optional :outline, type: Array do
    requires :x
    requires :y
  end
end
  1. I do a PATCH request and send {"outline": []} payload.
  2. Received params =>{ "outline"=>[""] } - here is the first question - why an empty string inside an array? I have a feeling that I've struggled with that a few years back, but don't remember what was the cause.
  3. declared(params) raises no implicit conversion of Symbol into Integer

jacek213 avatar Dec 17 '20 21:12 jacek213

What content-type are you using? This should work with content-type: JSON, but if you supply things in multipart or query params Rack can't figure it out.

dblock avatar Dec 17 '20 21:12 dblock

@dblock thanks for a quick response.

Content-type is JSON.

module API
  class Base < Grape::API
    prefix :api
    content_type :json, 'application/json; charset=utf-8'
    format :json
    formatter :json, Grape::Formatter::ActiveModelSerializers

    ...
end

jacek213 avatar Dec 17 '20 22:12 jacek213

That defines the response, not the request. How do you PATCH/POST data?

dblock avatar Dec 18 '20 03:12 dblock