Skip to content

Commit f951738

Browse files
author
Jon Evans
committed
Always disable root element key for child entities
If you expose :children, :using => ChildEntity, child entities will not be wrapped in a Hash with a root key, even if this is configured as a default for the ChildEntity class.
1 parent cb1db4f commit f951738

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

lib/grape/entity.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ def value_for(attribute, options = {})
195195
if exposure_options[:proc]
196196
exposure_options[:proc].call(object, options)
197197
elsif exposure_options[:using]
198-
exposure_options[:using].represent(object.send(attribute))
198+
exposure_options[:using].represent(object.send(attribute), :root => nil)
199199
else
200200
object.send(attribute)
201201
end

spec/grape/entity_spec.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@
211211
rep.last.serializable_hash[:name].should == 'Friend 2'
212212
end
213213

214+
it 'should disable root key name for child representations' do
215+
class FriendEntity < Grape::Entity
216+
root 'friends', 'friend'
217+
expose :name, :email
218+
end
219+
fresh_class.class_eval do
220+
expose :friends, :using => FriendEntity
221+
end
222+
rep = subject.send(:value_for, :friends)
223+
rep.should be_kind_of(Array)
224+
rep.reject{|r| r.is_a?(FriendEntity)}.should be_empty
225+
rep.first.serializable_hash[:name].should == 'Friend 1'
226+
rep.last.serializable_hash[:name].should == 'Friend 2'
227+
end
228+
214229
it 'should call through to the proc if there is one' do
215230
subject.send(:value_for, :computed, :awesome => 123).should == 123
216231
end

0 commit comments

Comments
 (0)