mirror of
https://github.com/ditkrg/active_model_serializers.git
synced 2026-01-24 14:56:50 +00:00
Fixed a bug that appears when a nil association is included
This commit is contained in:
parent
48ed7cf9ba
commit
6a0564a241
@ -53,10 +53,11 @@ module ActiveModel
|
|||||||
end
|
end
|
||||||
|
|
||||||
def add_included(resource_name, serializers, parent = nil)
|
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('.')
|
resource_path = [parent, resource_name].compact.join('.')
|
||||||
|
|
||||||
if include_assoc?(resource_path)
|
if include_assoc?(resource_path)
|
||||||
@hash[:included] ||= []
|
@hash[:included] ||= []
|
||||||
|
|
||||||
|
|||||||
@ -6,6 +6,7 @@ module ActiveModel
|
|||||||
class JsonApi
|
class JsonApi
|
||||||
class LinkedTest < Minitest::Test
|
class LinkedTest < Minitest::Test
|
||||||
def setup
|
def setup
|
||||||
|
ActionController::Base.cache_store.clear
|
||||||
@author1 = Author.new(id: 1, name: 'Steve K.')
|
@author1 = Author.new(id: 1, name: 'Steve K.')
|
||||||
@author2 = Author.new(id: 2, name: 'Tenderlove')
|
@author2 = Author.new(id: 2, name: 'Tenderlove')
|
||||||
@bio1 = Bio.new(id: 1, content: 'AMS Contributor')
|
@bio1 = Bio.new(id: 1, content: 'AMS Contributor')
|
||||||
@ -225,6 +226,29 @@ module ActiveModel
|
|||||||
|
|
||||||
assert_equal expected, adapter.serializable_hash[:included]
|
assert_equal expected, adapter.serializable_hash[:included]
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user