Skip to content

Commit

Permalink
Merge pull request #892 from groyoh/fix-json-nil-association
Browse files Browse the repository at this point in the history
Fixed a bug that appeared when json adapter serialize a nil association
  • Loading branch information
joaomdmoura committed May 5, 2015
2 parents 6a06b90 + 5dcdfaa commit ece43f3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/active_model/serializer/adapter/json.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def serializable_hash(options = {})
end
end
else
if association
if association && association.object
@hash[name] = cache_check(association) do
association.attributes(options)
end
Expand Down
7 changes: 7 additions & 0 deletions test/adapter/json/belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ def test_include_nil_author

assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], blog: {id: 999, name: "Custom blog"}, author: nil}, adapter.serializable_hash)
end

def test_include_nil_author_with_specified_serializer
serializer = PostPreviewSerializer.new(@anonymous_post)
adapter = ActiveModel::Serializer::Adapter::Json.new(serializer)

assert_equal({title: "Hello!!", body: "Hello, world!!", id: 43, comments: [], author: nil}, adapter.serializable_hash)
end
end
end
end
Expand Down

0 comments on commit ece43f3

Please sign in to comment.