Skip to content

Commit 4dc9260

Browse files
author
Lester Celestial
committed
don't expose a block generated attribute if criteria is not met
1 parent 0ff858a commit 4dc9260

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

lib/grape/entity.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ def serializable_hash(runtime_options = {})
259259
return nil if object.nil?
260260
opts = options.merge(runtime_options || {})
261261
exposures.inject({}) do |output, (attribute, exposure_options)|
262-
if exposure_options.has_key?(:proc) || object.respond_to?(attribute) && conditions_met?(exposure_options, opts)
262+
if (exposure_options.has_key?(:proc) || object.respond_to?(attribute)) && conditions_met?(exposure_options, opts)
263263
partial_output = value_for(attribute, opts)
264264
output[key_for(attribute)] =
265265
if partial_output.respond_to? :serializable_hash

spec/grape/entity_spec.rb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,8 +253,6 @@
253253

254254
describe '#serializable_hash' do
255255

256-
257-
258256
it 'should not throw an exception if a nil options object is passed' do
259257
expect{ fresh_class.new(model).serializable_hash(nil) }.not_to raise_error
260258
end
@@ -296,6 +294,14 @@
296294
res = fresh_class.new(model).serializable_hash
297295
res.should have_key :non_existant_attribute
298296
end
297+
298+
it "should not expose attributes that are generated by a block but have not passed criteria" do
299+
fresh_class.expose :non_existant_attribute, :proc => lambda {|model, options|
300+
"I exist, but it is not yet my time to shine"
301+
}, :if => lambda { |model, options| false }
302+
res = fresh_class.new(model).serializable_hash
303+
res.should_not have_key :non_existant_attribute
304+
end
299305

300306
context "#serializable_hash" do
301307

0 commit comments

Comments
 (0)