grape-entity
grape-entity copied to clipboard
Expose with :if and lambda stopped working on Ruby 2.4.1
I updated to Ruby 2.4.1 and grape-entity started complaining about using a lambda with :if
.
This worked perfectly on Ruby 2.4.0, grape 0.19.2 and grape-entity 0.6.1:
expose :context_company_id, if: -> { company_context? }
It doesn't help if I reduce the problem, this is still broken:
expose :anything, if: -> { true }
Using Ruby 2.4.1 the following error message appears instead:
caught error of type ArgumentError in after callback inside Grape::Middleware::Formatter : wrong number of arguments (given 2, expected 0)
I tried to attach a debugger at entity.rb:477, but I don't know how to figure out options
. If I try to inspect it, it'll throw the same error. If I print it, it's very long and I have no idea what to look for.
def exec_with_object(options, &block)
debugger
instance_exec(object, options, &block)
end
(byebug) pp options.inspect
*** ArgumentError Exception: wrong number of arguments (given 2, expected 0)
Any ideas?
I would start by adding 2.4.1 to the build matrix, first Grape, then grape-entity.
travis-ci was updated here #271 … @pre … please provide a spec to re-produce it
It turns out that a bug was introduced in ruby 2.4 that allowed #instance_exec
to be executed with the wrong number of arguments.
This was fixed on 2.4.1.
Ruby 2.3.0:
Ruby 2.4.0:
Ruby 2.4.1:
Thanks @tegon !
@LeFnord I haven't retried this (yet, as I used a workaround back then), but it seems that the documentation has this right by giving an example code expose :ip, if: lambda { |instance, options| options[:type] == :full }
.
As I was using a lamda without two options, I encountered the error in Ruby 2.4.1 - although things worked perfectly in Ruby 2.4.0.
If I understood correctly, expose :anything, if: -> { true }
is not even supposed to work, rather the expected syntax presumably is expose :anything, if: -> { |instance, options| true }
The error message is very confusing, though.