mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-23 06:16:50 +00:00
The Array serializer should not make a child root for every item in the Array
This commit is contained in:
parent
1e0c9ef93b
commit
e23553be23
@ -26,7 +26,8 @@ module ActiveModel
|
|||||||
|
|
||||||
def as_json(*args)
|
def as_json(*args)
|
||||||
@hash = {}
|
@hash = {}
|
||||||
array = serializable_array.as_json(*args)
|
|
||||||
|
array = serializable_array.map(&:serializable_hash)
|
||||||
|
|
||||||
if root = @options[:root]
|
if root = @options[:root]
|
||||||
@hash.merge!(root => array)
|
@hash.merge!(root => array)
|
||||||
|
|||||||
@ -416,6 +416,20 @@ class SerializerTest < ActiveModel::TestCase
|
|||||||
], serializer.as_json)
|
], serializer.as_json)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def test_array_serializer
|
||||||
|
comment1 = Comment.new(:title => "Comment1", :id => 1)
|
||||||
|
comment2 = Comment.new(:title => "Comment2", :id => 2)
|
||||||
|
|
||||||
|
array = [ comment1, comment2 ]
|
||||||
|
|
||||||
|
serializer = array.active_model_serializer.new(array, nil, :root => :comments)
|
||||||
|
|
||||||
|
assert_equal({ :comments => [
|
||||||
|
{ :title => "Comment1" },
|
||||||
|
{ :title => "Comment2" }
|
||||||
|
]}, serializer.as_json)
|
||||||
|
end
|
||||||
|
|
||||||
class CustomBlog < Blog
|
class CustomBlog < Blog
|
||||||
attr_accessor :public_posts, :public_user
|
attr_accessor :public_posts, :public_user
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user