Skip to content

Commit 3ebb409

Browse files
author
peter scholz
authored
Merge pull request #248 from arempe93/fix-nil-merge
Fix nil values causing errors when merge option passed
2 parents 305dcc9 + 01bdb33 commit 3ebb409

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#### Fixes
88

9+
* [#248](https://github.com/ruby-grape/grape-entity/pull/248): Fix `nil` values causing errors when `merge` option passed - [@arempe93](https://github.com/arempe93).
910
* Your contribution here.
1011

1112
### 0.5.2 (2016-11-14)

lib/grape_entity/exposure/nesting_exposure/output_builder.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ def add(exposure, result)
1717
# If we have an array which should not be merged - save it with a key as a hash
1818
# If we have hash which should be merged - save it without a key (merge)
1919
if exposure.for_merge
20+
return unless result
2021
@output_hash.merge! result, &merge_strategy(exposure.for_merge)
2122
else
2223
@output_hash[exposure.key] = result

spec/grape_entity/entity_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@
5050
subject.expose(:special, merge: ->(_, v1, v2) { v1 && v2 ? 'brand new val' : v2 })
5151
expect(subject.represent(nested_hash).serializable_hash).to eq(like_nested_hash: 'brand new val')
5252
end
53+
54+
context 'and nested object is nil' do
55+
let(:nested_hash) do
56+
{ something: nil, special: { like_nested_hash: '12' } }
57+
end
58+
59+
it 'adds nothing to output' do
60+
subject.expose(:something, merge: true)
61+
subject.expose(:special)
62+
expect(subject.represent(nested_hash).serializable_hash).to eq(special: { like_nested_hash: '12' })
63+
end
64+
end
5365
end
5466

5567
context 'with a block' do

0 commit comments

Comments
 (0)