Skip to content

Commit a0e523a

Browse files
committed
Merge pull request #154 from cerebris/pr/150
Pr/150
2 parents 282667a + 45a629d commit a0e523a

5 files changed

Lines changed: 76 additions & 1 deletion

File tree

lib/jsonapi/resource_serializer.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ def process_primary(source, requested_associations)
110110
add_included_object(@primary_class_name, id, object_hash(resource, requested_associations), true)
111111
end
112112
else
113+
return {} if source.nil?
114+
113115
resource = source
114116
id = resource.id
115117
# ToDo: See if this is actually needed

test/controllers/controller_test.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,19 @@ def test_show_has_many_relationship_invalid_id
13351335
assert_response :bad_request
13361336
assert_match /2,1 is not a valid value for id/, response.body
13371337
end
1338+
1339+
def test_show_has_one_relationship_nil
1340+
get :show_association, {post_id: '17', association: 'author'}
1341+
assert_response :success
1342+
assert_hash_equals json_response,
1343+
{
1344+
data: nil,
1345+
links: {
1346+
self: 'http://test.host/posts/17/links/author',
1347+
related: 'http://test.host/posts/17/author'
1348+
}
1349+
}
1350+
end
13381351
end
13391352

13401353
class TagsControllerTest < ActionController::TestCase
@@ -1678,6 +1691,50 @@ def test_valid_filter_value
16781691
assert_equal json_response['data'][0]['id'], '1'
16791692
assert_equal json_response['data'][0]['name'], 'Joe Author'
16801693
end
1694+
1695+
def test_get_related_resource
1696+
get :get_related_resource, {post_id: '2', association: 'author', :source=>'posts'}
1697+
assert_response :success
1698+
assert_hash_equals json_response,
1699+
{
1700+
data: {
1701+
id: '1',
1702+
type: 'people',
1703+
name: 'Joe Author',
1704+
email: 'joe@xyz.fake',
1705+
dateJoined: '2013-08-07 16:25:00 -0400',
1706+
links: {
1707+
self: 'http://test.host/people/1',
1708+
comments: {
1709+
self: 'http://test.host/people/1/links/comments',
1710+
related: 'http://test.host/people/1/comments'
1711+
},
1712+
posts: {
1713+
self: 'http://test.host/people/1/links/posts',
1714+
related: 'http://test.host/people/1/posts'
1715+
},
1716+
preferences: {
1717+
self: 'http://test.host/people/1/links/preferences',
1718+
related: 'http://test.host/people/1/preferences',
1719+
linkage: {
1720+
type: 'preferences',
1721+
id: '1'
1722+
}
1723+
}
1724+
}
1725+
}
1726+
}
1727+
end
1728+
1729+
def test_get_related_resource_nil
1730+
get :get_related_resource, {post_id: '17', association: 'author', :source=>'posts'}
1731+
assert_response :success
1732+
assert_hash_equals json_response,
1733+
{
1734+
data: nil
1735+
}
1736+
1737+
end
16811738
end
16821739

16831740
class Api::V5::AuthorsControllerTest < ActionController::TestCase

test/fixtures/posts.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,10 @@ post_16:
9393
id: 16
9494
title: SDFGH
9595
body: Not First!!!!
96-
author_id: 3
96+
author_id: 3
97+
98+
post_17:
99+
id: 17
100+
title: No Author!!!!!!
101+
body: This post has no Author
102+
author_id:

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
puts "Testing With RAILS VERSION #{Rails.version}"
2828

2929
class TestApp < Rails::Application
30+
config.eager_load = false
3031
config.root = File.dirname(__FILE__)
3132
config.session_store :cookie_store, key: 'session'
3233
config.secret_key_base = 'secret'

test/unit/serializer/serializer_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ def test_serializer
5757
)
5858
end
5959

60+
def test_serializer_nil_handling
61+
assert_hash_equals(
62+
{
63+
data: nil
64+
},
65+
JSONAPI::ResourceSerializer.new(PostResource).serialize_to_hash(nil)
66+
)
67+
end
68+
6069
def test_serializer_namespaced_resource
6170
assert_hash_equals(
6271
{

0 commit comments

Comments
 (0)