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

Safe exposure of Hash objects is failing #106

Closed
croeck opened this issue Jan 17, 2015 · 1 comment
Closed

Safe exposure of Hash objects is failing #106

croeck opened this issue Jan 17, 2015 · 1 comment
Labels

Comments

@croeck
Copy link
Contributor

croeck commented Jan 17, 2015

When using :expose and specifying the exposure as safe: true, objects of the type Hash seem to never get exposed. It does not matter whether the Hash contains a key that matches the attribute.

If i mark the :exposewith safe: false, the response does show the value of the Hash, but of course only as long as the Hash has such a key.

A quick lookup in the source showed me the valid_exposure? method in the entity.rb (around line 570) to be the issue. This is the current check:

(nested_exposures.any? && nested_exposures.all? { |a, o| valid_exposure?(a, o) }) || \
      exposure_options.key?(:proc) || \
      !exposure_options[:safe] || \
      object.respond_to?(self.class.name_for(attribute))

What I don't understand (I'm quite new to ruby and especially grape) is the third line, which actually says that safe exposures are not valid, unless they have nested elements or have a :proc block.

Nevertheless, similar to the fourth line, which checks for a method matching the attribute's name, I would have added a fifth condition that checks if the object is a Hash and does have a key matching the attribute's name:

object.is_a?(Hash) && object.key?(self.class.name_for(attribute))

This would resolve my issue and be more in line with the implementation of the primary output of the serializable_hash method, which does resolve the values of Hash objects.

Can s.o. please verify my issue?
Thank you all!

@dblock dblock added the bug? label Jan 17, 2015
croeck added a commit to croeck/grape-entity that referenced this issue Jan 17, 2015
… Hash objects that have a key matching the attribute's name.
@croeck
Copy link
Contributor Author

croeck commented Mar 10, 2015

fixed by pull #110

@croeck croeck closed this as completed Mar 10, 2015
croeck added a commit to croeck/grape-entity that referenced this issue Mar 16, 2015
… Hash objects that have a key matching the attribute's name.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants