mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-22 22:06:50 +00:00
Fix ArraySerializer's output when falling back on DefaultSerializer
Change ArraySerializer's initialization of DefaultSerializer to pass :root => false in order to avoid adding root keys to each of the items in the array. This ensures consistent results between serializing arrays of objects with serializers and arrays of objects without serializers. Fixes #495.
This commit is contained in:
parent
76fe4e754c
commit
cfcd712123
@ -83,7 +83,7 @@ module ActiveModel
|
|||||||
serializer = item.active_model_serializer
|
serializer = item.active_model_serializer
|
||||||
end
|
end
|
||||||
|
|
||||||
serializable = serializer ? serializer.new(item, @options) : DefaultSerializer.new(item, @options)
|
serializable = serializer ? serializer.new(item, @options) : DefaultSerializer.new(item, @options.merge(:root => false))
|
||||||
|
|
||||||
if serializable.respond_to?(:serializable_hash)
|
if serializable.respond_to?(:serializable_hash)
|
||||||
serializable.serializable_hash
|
serializable.serializable_hash
|
||||||
|
|||||||
@ -2,6 +2,27 @@ require "test_helper"
|
|||||||
require "test_fakes"
|
require "test_fakes"
|
||||||
|
|
||||||
class ArraySerializerTest < ActiveModel::TestCase
|
class ArraySerializerTest < ActiveModel::TestCase
|
||||||
|
|
||||||
|
def test_array_items_do_not_have_root
|
||||||
|
array = [
|
||||||
|
BasicActiveModel.new(:name => "First model"),
|
||||||
|
BasicActiveModel.new(:name => "Second model")
|
||||||
|
]
|
||||||
|
expected = { "root" => [
|
||||||
|
{ :name => "First model" },
|
||||||
|
{ :name => "Second model" }
|
||||||
|
] }
|
||||||
|
|
||||||
|
default_serializer = array.active_model_serializer.new(array, :root => "root")
|
||||||
|
each_serializer = array.active_model_serializer.new(array, :root => "root", :each_serializer => BasicSerializer)
|
||||||
|
|
||||||
|
default_json = default_serializer.as_json
|
||||||
|
each_json = each_serializer.as_json
|
||||||
|
|
||||||
|
assert_equal(expected, default_json)
|
||||||
|
assert_equal(expected, each_json)
|
||||||
|
end
|
||||||
|
|
||||||
# serialize different typed objects
|
# serialize different typed objects
|
||||||
def test_array_serializer
|
def test_array_serializer
|
||||||
model = Model.new
|
model = Model.new
|
||||||
|
|||||||
@ -1,3 +1,26 @@
|
|||||||
|
class BasicActiveModel
|
||||||
|
include ActiveModel::Serializers::JSON
|
||||||
|
def initialize(hash = {})
|
||||||
|
@attributes = hash
|
||||||
|
end
|
||||||
|
|
||||||
|
def serializable_hash(*)
|
||||||
|
@attributes
|
||||||
|
end
|
||||||
|
|
||||||
|
def method_missing(method)
|
||||||
|
if @attributes.key? method
|
||||||
|
@attributes[method]
|
||||||
|
else
|
||||||
|
raise NoMethodError
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
class BasicSerializer < ActiveModel::Serializer
|
||||||
|
attributes :name
|
||||||
|
end
|
||||||
|
|
||||||
class Model
|
class Model
|
||||||
def initialize(hash={})
|
def initialize(hash={})
|
||||||
@attributes = hash
|
@attributes = hash
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user