Skip to content

Commit

Permalink
Fixed exposure of attributes marked as safe when using hash models. T…
Browse files Browse the repository at this point in the history
…he valid_exposure check now also accepts Hash objects that have a key matching the attribute's name.
  • Loading branch information
croeck committed Mar 10, 2015
1 parent dce9d9a commit e4d3a2b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 1 deletion.
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

0 comments on commit e4d3a2b

Please sign in to comment.