Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nil values causing errors when merge option passed #248

Merged
merged 1 commit into from
Nov 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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