Skip to content

Commit dacc671

Browse files
committed
Merge pull request ruby-grape#48 from Matchbook/master
regard an exposure as safe if its nested exposures are safe
2 parents 4284ee5 + 094fe20 commit dacc671

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

lib/grape_entity/entity.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,14 @@ def self.key_for(attribute)
388388
exposures[attribute.to_sym][:as] || name_for(attribute)
389389
end
390390

391+
def self.nested_exposures_for(attribute)
392+
exposures.select { |a, _| a.to_s =~ /^#{attribute}__/ }
393+
end
394+
391395
def value_for(attribute, options = {})
392396
exposure_options = exposures[attribute.to_sym]
393397

394-
nested_exposures = exposures.select { |a, _| a.to_s =~ /^#{attribute}__/ }
398+
nested_exposures = self.class.nested_exposures_for(attribute)
395399

396400
if exposure_options[:using]
397401
using_options = options.dup
@@ -438,6 +442,8 @@ def delegate_attribute(attribute)
438442
end
439443

440444
def valid_exposure?(attribute, exposure_options)
445+
nested_exposures = self.class.nested_exposures_for(attribute)
446+
(nested_exposures.any? && nested_exposures.all? { |a, o| valid_exposure?(a, o) }) || \
441447
exposure_options.has_key?(:proc) || \
442448
!exposure_options[:safe] || \
443449
object.respond_to?(self.class.name_for(attribute))

spec/grape_entity/entity_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,21 @@ class BogusEntity < Grape::Entity
105105
another_nested: "value"
106106
}
107107
end
108+
109+
it 'is safe if its nested exposures are safe' do
110+
subject.with_options safe: true do
111+
subject.expose :awesome do
112+
subject.expose(:nested) { |_| "value" }
113+
end
114+
subject.expose :not_awesome do
115+
subject.expose :nested
116+
end
117+
end
118+
119+
valid_keys = subject.represent({}).valid_exposures.keys
120+
valid_keys.include?(:awesome).should == true && \
121+
valid_keys.include?(:not_awesome).should == false
122+
end
108123
end
109124
end
110125

0 commit comments

Comments
 (0)