Skip to content

Ruby 3: Entities exposing fields via Symbol#to_proc are broken #354

Open
@mttkay

Description

@mttkay

With grape-entity, I can write code like the following:

    class Blob < Grape::Entity
      expose :path
      expose :filename, &:path
    end

This utilizes Ruby's somewhat obscure & operator to call Symbol#to_proc and forward filename to path. The proc returned by this function takes the receiver of the method described by the symbol as its first argument, and the actual method parameters as the remaining arguments. Obviously the first argument is required (since it is equivalent to self), but Ruby 2 had a design issue where to_proc for symbols would report signature metadata that is not really correct. See https://rubyreferences.github.io/rubychanges/3.0.html#symbolto_proc-reported-as-lambda for a good summary.

The problem in grape-entity is this:

In Entity::exec_with_object, there is a test where grape reflects on the signature of such a block:

    def exec_with_object(options, &block)
      if block.parameters.count == 1
        instance_exec(object, &block)
      else
        instance_exec(object, options, &block)
      end
    rescue StandardError => e
      ...
    end

Here, block.parameters.count (or, block.arity) will return an incorrect value in Ruby 2:

[4] pry(#<API::Entities::Blob>)> block.parameters
=> [[:rest]]

[6] pry(#<API::Entities::Blob>)> block.arity
=> -1

This is Ruby lying about proc arity, since it tosses all possible arguments into a single optional argument (-1 means all arguments are optional, which is not true; you always need a receiver here.)

Ruby 3 fixes this:

[1] pry(#<API::Entities::Blob>)> block.parameters
=> [[:req], [:rest]]
[4] pry(#<API::Entities::Blob>)> block.arity
=> -2

This is correct: the first argument is now required, since it is the receiver of path. The second argument is the optional parameters (perhaps none.) The arity is also correct now: -2 means 1 required parameter, the rest optional.

What this means is that now grape-entity goes down the wrong code path in exec_with_object, since parameter count is now 2, not 1.

I'm not actually all that familiar with grape, I am just working on the Ruby 3 migration at GitLab so I wanted to raise awareness about this somewhat subtle issue.

A simple workaround is to rewrite the entity like so:

    class Blob < Grape::Entity
      expose :path
      expose :filename do |instance|
        instance.path
      end
    end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions