diff --git a/CHANGELOG.md b/CHANGELOG.md index c870868e25..8d6c5bda94 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * [#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). * [#799](https://github.com/intridea/grape/pull/799): Fixed custom validators with required `Hash`, `Array` types - [@bwalex](https://github.com/bwalex). +* [#784](https://github.com/intridea/grape/pull/784): Fixed `present` to not overwrite the previously added contents of the response body whebn called more than once - [@mfunaro](https://github.com/mfunaro). * Your contribution here. 0.9.0 (8/27/2014) diff --git a/lib/grape/dsl/inside_route.rb b/lib/grape/dsl/inside_route.rb index 57f936396e..589653b79d 100644 --- a/lib/grape/dsl/inside_route.rb +++ b/lib/grape/dsl/inside_route.rb @@ -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 diff --git a/spec/grape/dsl/inside_route_spec.rb b/spec/grape/dsl/inside_route_spec.rb index f59e5c8012..c0618d8c56 100644 --- a/spec/grape/dsl/inside_route_spec.rb +++ b/spec/grape/dsl/inside_route_spec.rb @@ -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