Skip to content

Commit c3a1acd

Browse files
committed
Merge pull request #110 from croeck/safe-exposure-of-hash-objects
Fixed exposure of attributes marked as safe when using hash models.
2 parents dce9d9a + e4d3a2b commit c3a1acd

File tree

3 files changed

+10
-1
lines changed

3 files changed

+10
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Next Release
22
============
33

44
* Your contribution here.
5+
* [#110](https://github.com/intridea/grape-entity/pull/110): Fixed safe exposure when using `Hash` models - [@croeck](https://github.com/croeck).
56
* [#109](https://github.com/intridea/grape-entity/pull/109): Add unexpose method - [@jonmchan](https://github.com/jonmchan).
67
* [#98](https://github.com/intridea/grape-entity/pull/98): Add nested conditionals - [@zbelzer](https://github.com/zbelzer).
78
* [#91](https://github.com/intridea/grape-entity/pull/91): Fix OpenStruct serializing - [@etehtsea](https://github.com/etehtsea).

lib/grape_entity/entity.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -576,7 +576,8 @@ def valid_exposure?(attribute, exposure_options)
576576
(nested_exposures.any? && nested_exposures.all? { |a, o| valid_exposure?(a, o) }) || \
577577
exposure_options.key?(:proc) || \
578578
!exposure_options[:safe] || \
579-
object.respond_to?(self.class.name_for(attribute))
579+
object.respond_to?(self.class.name_for(attribute)) || \
580+
object.is_a?(Hash) && object.key?(self.class.name_for(attribute))
580581
end
581582

582583
def conditions_met?(exposure_options, options)

spec/grape_entity/entity_spec.rb

+7
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,13 @@ class Parent < Person
683683
expect(res).to have_key :name
684684
end
685685

686+
it 'does expose attributes marked as safe if model is a hash object' do
687+
fresh_class.expose :name, safe: true
688+
689+
res = fresh_class.new(name: 'myname').serializable_hash
690+
expect(res).to have_key :name
691+
end
692+
686693
it "does not expose attributes that don't exist on the object, even with criteria" do
687694
fresh_class.expose :email
688695
fresh_class.expose :nonexistent_attribute, safe: true, if: lambda { false }

0 commit comments

Comments
 (0)