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

expose_nil delegates to object, ignores methods defined in Entities #305

Open
al opened this issue Aug 10, 2018 · 4 comments
Open

expose_nil delegates to object, ignores methods defined in Entities #305

al opened this issue Aug 10, 2018 · 4 comments

Comments

@al
Copy link

al commented Aug 10, 2018

expose_nil delegates to the underlying object and doesn't test whether the method is delegatable? This can lead to odd behaviour and errors.

You'd probably have to be doing inheritance with your Entities in order to encounter it in the wild, but as a contrived example:

class MyEntity < ::Grape::Entity
  expose :foo, expose_nil: false

  def foo
    'forty-foo'
  end
end

class FooableClass
  def initialize(foo = nil)
    @foo = foo
  end

  def foo
    @foo
  end
end

class NoFooFoYou
end

# In all 3 of the following, I'd argue that the expected result is {:foo=>'forty-foo'}

MyEntity.represent(FooableClass.new(42)).as_json
# => {:foo=>'forty-foo'}

MyEntity.represent(FooableClass.new).as_json
# => {}

MyEntity.represent(NoFooFoYou.new).as_json
NoMethodError: undefined method `foo' for #<NoFooFoYou:>

Would you agree this is an issue?

@dsantosmerino
Copy link

Just found today the same issue. I have a spec reproducing this behavior. Now I'm trying to do something to fix it 😄.

@teoulas
Copy link

teoulas commented Jan 25, 2019

Also encountered this and it seems someone has already fixed this in their fork ☝️.

@tscholz
Copy link

tscholz commented May 11, 2020

Hi there, any news on that?

@dchandekstark
Copy link

I think this should be considered a bug. At least it's not mentioned in the README.

Thanks for grape-entity! :)

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

5 participants