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 bug that would overwrite the content of the response body when using multiple presenters #784

Closed
wants to merge 2 commits into from
Closed
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 @@ -7,6 +7,7 @@
* [#745](https://github.com/intridea/grape/pull/745): Added `:binary, application/octet-stream` content-type - [@akabraham](https://github.com/akabraham).
* [#757](https://github.com/intridea/grape/pull/757): Changed `desc` can now be used with a block syntax - [@dspaeth-faber](https://github.com/dspaeth-faber).
* [#779](https://github.com/intridea/grape/pull/779): Fixed using `values` with a `default` proc - [@ShPakvel](https://github.com/ShPakvel).
* [#784](https://github.com/intridea/grape/pull/784): Fix issue in inside_route#present that would overwrite the previously added contents of the response body when calling `present` with Grape Entities more than once in an endpoint. - [@mfunaro](https://github.com/mfunaro).
* Your contribution here.

0.9.0 (8/27/2014)
Expand Down
7 changes: 6 additions & 1 deletion lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,12 @@ def present(*args)
end

representation = { root => representation } if root
representation = (@body || {}).merge(key => representation) if key
if key
representation = (@body || {}).merge(key => representation)
elsif entity_class.present? && representation.respond_to?('merge')
representation = (@body || {}).merge(representation)
end

body representation
end

Expand Down
29 changes: 29 additions & 0 deletions spec/grape/dsl/inside_route_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,35 @@ def initialize
end
end
end

describe 'with' do
describe 'multiple entities' do
let(:entity_mock1) do
entity_mock1 = Object.new
allow(entity_mock1).to receive(:represent).and_return(dummy1: 'dummy1')
entity_mock1
end

let(:entity_mock2) do
entity_mock2 = Object.new
allow(entity_mock2).to receive(:represent).and_return(dummy2: 'dummy2')
entity_mock2
end

describe 'instance' do
before do
subject.present 'dummy1', with: entity_mock1
subject.present 'dummy2', with: entity_mock2
end

it 'presents both dummy objects' do
expect(subject.body[:dummy1]).to eq 'dummy1'
expect(subject.body[:dummy2]).to eq 'dummy2'
end
end

end
end
end

describe '#declared' do
Expand Down