Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug that appears when a nil association is included #868

Merged
merged 1 commit into from
Apr 19, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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