Skip to content

Commit f39d4de

Browse files
author
Michael Bleigh
committed
Entity#serializable_hash returns nil if the represented object is nil.
1 parent 4d6ec46 commit f39d4de

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

lib/grape/entity.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ def exposures
118118
# representation, this is where you can trigger things from conditional options
119119
# etc.
120120
def serializable_hash(runtime_options = {})
121+
return nil if object.nil?
121122
opts = options.merge(runtime_options || {})
122123
exposures.inject({}) do |output, (attribute, exposure_options)|
123124
output[key_for(attribute)] = value_for(attribute, opts) if conditions_met?(exposure_options, opts)

spec/grape/entity_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,11 @@
9090
it 'should not throw an exception if a nil options object is passed' do
9191
expect{ fresh_class.new(model).serializable_hash(nil) }.not_to raise_error
9292
end
93+
94+
it 'should not blow up when the model is nil' do
95+
fresh_class.expose :name
96+
expect{ fresh_class.new(nil).serializable_hash }.not_to raise_error
97+
end
9398
end
9499

95100
describe '#value_for' do

0 commit comments

Comments
 (0)