Parameter validation bug with nested arrays
I think I may have found a bug. I'm trying to debug a reasonably complex set of params looks to be an issued with nested arrays causing the below stack trace. I've setup a barebones grape project to reproduce the following error.
https://github.com/danieldraper/ruby-grape-params
Should be able to get up and running by cloning the repo then running:
bundle install
bundle exec puma
There is a curl command at the top of api.rb which causes this error.
If anyone has any insights into this it would be greatly appreciated.
Puma caught this error: no implicit conversion of Symbol into Integer (TypeError)
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/dsl/parameters.rb:195:in `[]'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/dsl/parameters.rb:195:in `block in params'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/dsl/parameters.rb:195:in `map'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/dsl/parameters.rb:195:in `params'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/validations/attributes_iterator.rb:11:in `initialize'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/validations/validators/base.rb:37:in `new'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/validations/validators/base.rb:37:in `validate!'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/validations/validators/base.rb:27:in `validate'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/endpoint.rb:338:in `block in run_validators'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/endpoint.rb:336:in `each'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/endpoint.rb:336:in `run_validators'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/endpoint.rb:256:in `block in run'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/activesupport-5.0.0.1/lib/active_support/notifications.rb:166:in `instrument'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/endpoint.rb:238:in `run'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/endpoint.rb:305:in `block in build_stack'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/middleware/base.rb:30:in `call!'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/middleware/base.rb:23:in `call'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/middleware/base.rb:30:in `call!'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/middleware/base.rb:23:in `call'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/middleware/error.rb:34:in `block in call!'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/middleware/error.rb:33:in `catch'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/middleware/error.rb:33:in `call!'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/middleware/base.rb:23:in `call'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/rack-2.0.1/lib/rack/head.rb:12:in `call'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/endpoint.rb:222:in `call!'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/endpoint.rb:216:in `call'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/router/route.rb:68:in `exec'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/router.rb:71:in `block in identity'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/router.rb:92:in `transaction'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/router.rb:67:in `identity'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/router.rb:52:in `block in call'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/router.rb:115:in `with_optimization'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/router.rb:51:in `call'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/api.rb:119:in `call'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/api.rb:45:in `call!'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/bundler/gems/grape-0fef1bc2c3af/lib/grape/api.rb:40:in `call'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/puma-3.6.0/lib/puma/configuration.rb:225:in `call'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/puma-3.6.0/lib/puma/server.rb:578:in `handle_request'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/puma-3.6.0/lib/puma/server.rb:415:in `process_client'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/puma-3.6.0/lib/puma/server.rb:275:in `block in run'
/usr/local/var/rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/puma-3.6.0/lib/puma/thread_pool.rb:116:in `block in spawn_thread'
So just an update, still not sure what is going on however adding this begin/rescue (hackz) and running again I still get a number of no implicit conversion of Symbol into Integer however everything seems to work as expected. By works as expected I mean the nested arrays are parsed and persisted to the database and then correctly returned in the response.
Will keep digging, any advise please feel free to comment.
# @param params [Hash] initial hash of parameters
# @return hash of parameters relevant for the current scope
# @api private
def params(params)
params = @parent.params(params) if @parent
if @element
params = if params.is_a?(Array)
# used for calculating parent array indices for error messages
begin
params.map { |el| el[@element] || {} }
rescue => e
end
elsif params.is_a?(Hash)
params[@element] || {}
else
{}
end
end
params
end
I would try Grape HEAD, then try to build a simple repro as a spec.
@dblock not sure I follow sorry? I've tried using gem "grape", github: "ruby-grape/grape" is this what you're referring to and you can also see the issue in the provided sample repo above https://github.com/danieldraper/ruby-grape-params no specs at the moment however.
Yes.
Someone will take a look, but you can get there a lot faster by narrowing down the problem and maybe even fixing it. I would write a spec, make sure it reproduces the issue, then start taking parameters out of the long list to find out which one causes this.
Thanks @dblock will see how we go. I have a very evil solution in my fork that allows things to work and progress at the moment. From what I can see the following is occurring:
The def params(params) method has a line if params.is_a?(Array) which works fine until you have an array inside an array. The params goes from an array of objects to an array of arrays of objects. In my example the issues start with :options
[{}, {}] - params.map { |el| el[@element] } is effectively array[:symbol] this doesn't work when your params are an array.
[{}, [{}], [{}]] still working through the code and trying to figure out what is actually going on and why its doing it.
Once I have a better grip I should hopefully be able to provide a better solution. (Current hack is begin/rescue) and just ignore the exception and everything still works as expected... which is confusing :)
Hopefully this gives some more content and hopefully, I'll have a spec and solution soon.
hows the research going?
desc 'Hunting bug'
params do
requires :arr1, type: Array do
requires :hash1, type: Hash do
requires :arr2, type: Array do
requires :hash2, type: Hash do
requires :something, type: String
end
end
end
end
end
post :bughunt do
{ message: 'ok' }
end
curl -H "Content-type: application/json" -X POST -d '{"arr1":[{"arr2":[{"something":"value"}]}]}' http://localhost:3000/api/v1/bughunt
gives:
TypeError at /api/v1/bughunt
============================
> no implicit conversion of Symbol into Integer
seems like array-hash-array-hash combination is causing problems.
Someone please write a spec for this, @krbs sounds like you're close!
khm, got finally time for proper testproject with tests - https://github.com/krbs/grape_test
Ok, but what I want is a pull request into Grape that has failing tests, so we (you) can fix the bug easily.
pull request added with fix & test https://github.com/ruby-grape/grape/pull/1512
Closed via https://github.com/ruby-grape/grape/pull/1512.