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

Fixed exposure of attributes marked as safe when using hash models. #110

Merged
merged 1 commit into from
Mar 10, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Next Release
============

* Your contribution here.
* [#110](https://github.com/intridea/grape-entity/pull/110): Fixed safe exposure when using `Hash` models - [@croeck](https://github.com/croeck).
* [#109](https://github.com/intridea/grape-entity/pull/109): Add unexpose method - [@jonmchan](https://github.com/jonmchan).
* [#98](https://github.com/intridea/grape-entity/pull/98): Add nested conditionals - [@zbelzer](https://github.com/zbelzer).
* [#91](https://github.com/intridea/grape-entity/pull/91): Fix OpenStruct serializing - [@etehtsea](https://github.com/etehtsea).
Expand Down
3 changes: 2 additions & 1 deletion lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -576,7 +576,8 @@ def valid_exposure?(attribute, exposure_options)
(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))
object.respond_to?(self.class.name_for(attribute)) || \
object.is_a?(Hash) && object.key?(self.class.name_for(attribute))
end

def conditions_met?(exposure_options, options)
Expand Down
7 changes: 7 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -683,6 +683,13 @@ class Parent < Person
expect(res).to have_key :name
end

it 'does expose attributes marked as safe if model is a hash object' do
fresh_class.expose :name, safe: true

res = fresh_class.new(name: 'myname').serializable_hash
expect(res).to have_key :name
end

it "does not expose attributes that don't exist on the object, even with criteria" do
fresh_class.expose :email
fresh_class.expose :nonexistent_attribute, safe: true, if: lambda { false }
Expand Down