Merge pull request #868 from groyoh/bug-with-nil-association

Fixed a bug that appears when a nil association is included
This commit is contained in:
Alexandre de Oliveira 2015-04-19 16:56:38 -03:00
commit 820db0bee1
2 changed files with 28 additions and 3 deletions

View File

@ -53,10 +53,11 @@ module ActiveModel
end
def add_included(resource_name, serializers, parent = nil)
serializers = Array(serializers) unless serializers.respond_to?(:each)
unless serializers.respond_to?(:each)
return unless serializers.object
serializers = Array(serializers)
end
resource_path = [parent, resource_name].compact.join('.')
if include_assoc?(resource_path)
@hash[:included] ||= []

View File

@ -6,6 +6,7 @@ module ActiveModel
class JsonApi
class LinkedTest < Minitest::Test
def setup
ActionController::Base.cache_store.clear
@author1 = Author.new(id: 1, name: 'Steve K.')
@author2 = Author.new(id: 2, name: 'Tenderlove')
@bio1 = Bio.new(id: 1, content: 'AMS Contributor')
@ -225,6 +226,29 @@ module ActiveModel
assert_equal expected, adapter.serializable_hash[:included]
end
def test_nil_link_with_specified_serializer
@first_post.author = nil
serializer = PostPreviewSerializer.new(@first_post)
adapter = ActiveModel::Serializer::Adapter::JsonApi.new(
serializer,
include: ['author']
)
expected = {
data: {
id: "10",
title: "Hello!!",
body: "Hello, world!!",
type: "posts",
links: {
comments: { linkage: [ { type: "comments", id: '1' }, { type: "comments", id: '2' } ] },
author: { linkage: nil }
}
}
}
assert_equal expected, adapter.serializable_hash
end
end
end
end