grape-entity icon indicating copy to clipboard operation
grape-entity copied to clipboard

Expose with :if and lambda stopped working on Ruby 2.4.1

Open pre opened this issue 7 years ago • 4 comments

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?

pre avatar May 03 '17 13:05 pre

I would start by adding 2.4.1 to the build matrix, first Grape, then grape-entity.

dblock avatar May 05 '17 11:05 dblock

travis-ci was updated here #271 … @pre … please provide a spec to re-produce it

LeFnord avatar Jul 12 '17 23:07 LeFnord

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: screen shot 2018-01-03 at 14 58 17

Ruby 2.4.0: screen shot 2018-01-03 at 14 59 03

Ruby 2.4.1: screen shot 2018-01-03 at 14 59 36

tegon avatar Jan 03 '18 18:01 tegon

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.

pre avatar Jan 04 '18 12:01 pre