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

Fixed duplicate root keys fixes #521 #529

Closed
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
14 changes: 13 additions & 1 deletion lib/active_model/serializable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ module Serializable
def as_json(options={})
if root = options.fetch(:root, json_key)
hash = { root => serializable_object }
hash.merge!(serializable_data)

merge_hash(hash, serializable_data)

hash
else
serializable_object
Expand All @@ -21,5 +23,15 @@ def serializable_data
def embedded_in_root_associations
{}
end

def merge_hash(destination, source)
source.each_pair do |key, value|
if destination[key].is_a?(Array)
destination[key].concat(value)
else
destination[key] = value
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/active_model/serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ def embedded_in_root_associations
if included_associations.include? name
if association.embed_in_root?
association_serializer = build_serializer(association)
hash.merge! association_serializer.embedded_in_root_associations
merge_hash(hash, association_serializer.embedded_in_root_associations)

serialized_data = association_serializer.serializable_object
key = association.root_key
Expand Down
14 changes: 14 additions & 0 deletions test/fixtures/poro.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,15 @@ def comments
class Comment < Model
end

class Catalog < Model
def sub_catalogs
@attributes[:sub_catalogs] ||= [
Catalog.new(name: 'Catalog 1', :sub_catalogs => []),
Catalog.new(name: 'Catalog 2', :sub_catalogs => [])
]
end
end

###
## Serializers
###
Expand Down Expand Up @@ -62,3 +71,8 @@ class PostSerializer < ActiveModel::Serializer
class CommentSerializer < ActiveModel::Serializer
attributes :content
end

class CatalogSerializer < ActiveModel::Serializer
attributes :name
has_many :sub_catalogs, root: :catalogs, embed: :ids, embed_in_root: true #serializer: CatalogSerializer
end
17 changes: 17 additions & 0 deletions test/unit/active_model/array_serializer/root_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,22 @@ def test_root_as_argument_takes_precedence
}, @rooted_serializer.as_json(root: :argument))
end
end

class DuplicateRootKeysTest < Minitest::Test
def setup
@catalog = Catalog.new({ name: 'Catalog root' })
@serializer = ArraySerializer.new([@catalog], root: :catalogs)
end

def test_duplicate_root_keys_using_as_json
assert_equal({
catalogs: [
{ name: 'Catalog root', 'sub_catalog_ids' => @catalog.sub_catalogs.map(&:object_id) },
{ name: 'Catalog 1', 'sub_catalog_ids' => [] },
{ name: 'Catalog 2', 'sub_catalog_ids' => [] },
]
}, @serializer.as_json)
end
end
end
end