Skip to content

Commit

Permalink
Merge pull request #2 from Groyoh/bug-with-nil-association
Browse files Browse the repository at this point in the history
Fixed a bug that appears when a nil association is included
  • Loading branch information
TheWudu committed Apr 8, 2015
2 parents 48ed7cf + 6a0564a commit f0b7de2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,11 @@ def add_link(resource, name, serializer)
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] ||= []

Expand Down
24 changes: 24 additions & 0 deletions test/adapter/json_api/linked_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Adapter
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')
Expand Down Expand Up @@ -225,6 +226,29 @@ def test_multiple_references_to_same_resource

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
Expand Down

0 comments on commit f0b7de2

Please sign in to comment.