Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
PetrKaleta opened this issue Nov 16, 2020 · 1 comment
Closed

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

PetrKaleta opened this issue Nov 16, 2020 · 1 comment

Comments

@PetrKaleta
Copy link

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...

@jeremyevans
Copy link
Owner

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants