jeremyevans/roda

typecast_params wrong behaviour with array!(:any, 'arr')

Closed this issue · 1 comments

Lets say we have a very simple roda 3.37.0 app

class App < Roda
  plugin :typecast_params

  route do |r|
    r.post 'test' do
      arr = typecast_params.array!(:any, 'arr')
    end
  end
end

So arr must be present in params and must be an array of any objects. So this request should pass:

curl --location --request POST 'http://0.0.0.0:9292/test' \
--header 'Content-Type: application/json' \
--data-raw '{
    "arr": [[1], [1, 2], null]
}'

But I am receiving Roda::RodaPlugins::TypecastParams::Error: invalid value in array parameter foo Yes, it's because I am sending a null value as one object in arr. Which should not be a problem, because I am using any converter.

If I use typecast_params.array(:any, 'arr') everything works good, but in that case arr may not be present in params at all...

array! doesn't look at the type convertor used (and shouldn't because you can override the default type convertors). It is documented to raise an TypecastParams::Error if the returned value is nil or any value in the returned array is nil, so it is operating as expected, and this isn't considered a bug.

In your case, you should probably use array and manually check the resulting value is not nil.