Skip to content

Commit aeece32

Browse files
author
Pedro Padron
committed
fixes a bug introduced by ruby-grape#203 where entities that expose attributes returned by a block (therefore absent from the object) would not be serialized
1 parent 8d98e1d commit aeece32

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
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 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: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,9 @@
252252
subject{ fresh_class.new(model) }
253253

254254
describe '#serializable_hash' do
255+
256+
257+
255258
it 'should not throw an exception if a nil options object is passed' do
256259
expect{ fresh_class.new(model).serializable_hash(nil) }.not_to raise_error
257260
end
@@ -285,6 +288,14 @@
285288
res.should_not have_key :non_existant_attribute
286289
res.should_not have_key :non_existant_attribute2
287290
end
291+
292+
it "should expose attributes that don't exist on the object only when they are generated by a block" do
293+
fresh_class.expose :non_existant_attribute do |model, options|
294+
"well, I do exist after all"
295+
end
296+
res = fresh_class.new(model).serializable_hash
297+
res.should have_key :non_existant_attribute
298+
end
288299

289300
context "#serializable_hash" do
290301

0 commit comments

Comments
 (0)