Skip to content

Commit 5dcdfaa

Browse files
author
groyoh
committed
Fixed a bug that appeared when json adapter serialize a nil association
1 parent fc6276c commit 5dcdfaa

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

lib/active_model/serializer/adapter/json.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ def serializable_hash(options = {})
2323
end
2424
end
2525
else
26-
if association
26+
if association && association.object
2727
@hash[name] = cache_check(association) do
2828
association.attributes(options)
2929
end

test/adapter/json/belongs_to_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,13 @@ def test_include_nil_author
3434

3535
assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}, adapter.serializable_hash)
3636
end
37+
38+
def test_include_nil_author_with_specified_serializer
39+
serializer = PostPreviewSerializer.new(@anonymous_post)
40+
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)
41+
42+
assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}, adapter.serializable_hash)
43+
end
3744
end
3845
end
3946
end

0 commit comments

Comments
 (0)