Skip to content

Commit

Permalink
Merge pull request #248 from arempe93/fix-nil-merge
Browse files Browse the repository at this point in the history
Fix nil values causing errors when merge option passed
  • Loading branch information
peter scholz committed Nov 15, 2016
2 parents 305dcc9 + 01bdb33 commit 3ebb409
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

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

### 0.5.2 (2016-11-14)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def add(exposure, result)
# If we have an array which should not be merged - save it with a key as a hash
# If we have hash which should be merged - save it without a key (merge)
if exposure.for_merge
return unless result
@output_hash.merge! result, &merge_strategy(exposure.for_merge)
else
@output_hash[exposure.key] = result
Expand Down
12 changes: 12 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,18 @@
subject.expose(:special, merge: ->(_, v1, v2) { v1 && v2 ? 'brand new val' : v2 })
expect(subject.represent(nested_hash).serializable_hash).to eq(like_nested_hash: 'brand new val')
end

context 'and nested object is nil' do
let(:nested_hash) do
{ something: nil, special: { like_nested_hash: '12' } }
end

it 'adds nothing to output' do
subject.expose(:something, merge: true)
subject.expose(:special)
expect(subject.represent(nested_hash).serializable_hash).to eq(special: { like_nested_hash: '12' })
end
end
end

context 'with a block' do
Expand Down

0 comments on commit 3ebb409

Please sign in to comment.