Skip to content

Commit

Permalink
Respond with valid json when relationship is nil
Browse files Browse the repository at this point in the history
```
    book = Book.new(author: nil)
    book.as_json(includes: [:author])

    {
        relationships: {
            author: { data: { meta: {} } }
        }
    }
```
  • Loading branch information
subbarao committed Aug 1, 2016
1 parent 99c8ebe commit 831a194
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def initialize(parent_serializer, serializer, serializable_resource_options, arg

def as_json
hash = {}
hash[:data] = data || {} if association_options[:include_data]
hash[:data] = data || { meta: {} } if association_options[:include_data]
links = self.links
hash[:links] = links if links.any?
meta = self.meta
Expand Down
2 changes: 1 addition & 1 deletion test/action_controller/json_api/linked_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_render_resource_with_nested_has_many_include
'relationships' => {
'posts' => { 'data' => [] },
'roles' => { 'data' => [{ 'type' => 'roles', 'id' => '1' }, { 'type' => 'roles', 'id' => '2' }] },
'bio' => { 'data' => nil }
'bio' => { 'data' => { 'meta' => {} } }
}
}, {
'id' => '1',
Expand Down
6 changes: 3 additions & 3 deletions test/adapter/json_api/belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_include_nil_author
serializer = PostSerializer.new(@anonymous_post)
adapter = ActiveModelSerializers::Adapter::JsonApi.new(serializer)

assert_equal({ comments: { data: [] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: nil } }, adapter.serializable_hash[:data][:relationships])
assert_equal({ comments: { data: [] }, blog: { data: { type: 'blogs', id: '999' } }, author: { data: { meta: {} } } }, adapter.serializable_hash[:data][:relationships])
end

def test_include_type_for_association_when_different_than_name
Expand Down Expand Up @@ -119,7 +119,7 @@ def test_include_linked_resources_with_type_name
relationships: {
posts: { data: [] },
roles: { data: [] },
bio: { data: nil }
bio: { data: { meta: {} } }
}
}, {
id: '42',
Expand All @@ -143,7 +143,7 @@ def test_include_linked_resources_with_type_name
relationships: {
comments: { data: [] },
blog: { data: { type: 'blogs', id: '999' } },
author: { data: nil }
author: { data: { meta: {} } }
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion test/adapter/json_api/has_many_explicit_serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_includes_author_id
def test_explicit_serializer_with_null_resource
@post.author = nil

expected = { data: nil }
expected = { data: { meta: {} } }

assert_equal(expected, @adapter.serializable_hash[:data][:relationships][:author])
end
Expand Down
8 changes: 4 additions & 4 deletions test/adapter/json_api/has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def test_includes_linked_comments
},
relationships: {
post: { data: { type: 'posts', id: '1' } },
author: { data: nil }
author: { data: { meta: {} } }
}
}, {
id: '2',
Expand All @@ -60,7 +60,7 @@ def test_includes_linked_comments
},
relationships: {
post: { data: { type: 'posts', id: '1' } },
author: { data: nil }
author: { data: { meta: {} } }
}
}]
assert_equal expected, @adapter.serializable_hash[:included]
Expand All @@ -73,14 +73,14 @@ def test_limit_fields_of_linked_comments
type: 'comments',
relationships: {
post: { data: { type: 'posts', id: '1' } },
author: { data: nil }
author: { data: { meta: {} } }
}
}, {
id: '2',
type: 'comments',
relationships: {
post: { data: { type: 'posts', id: '1' } },
author: { data: nil }
author: { data: { meta: {} } }
}
}]
assert_equal expected, @adapter.serializable_hash[:included]
Expand Down
6 changes: 3 additions & 3 deletions test/adapter/json_api/linked_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_include_multiple_posts_and_linked_array
},
relationships: {
post: { data: { type: 'posts', id: '10' } },
author: { data: nil }
author: { data: { meta: {} } }
}
}, {
id: '2',
Expand All @@ -102,7 +102,7 @@ def test_include_multiple_posts_and_linked_array
},
relationships: {
post: { data: { type: 'posts', id: '10' } },
author: { data: nil }
author: { data: { meta: {} } }
}
}, {
id: '1',
Expand Down Expand Up @@ -274,7 +274,7 @@ def test_nil_link_with_specified_serializer
},
relationships: {
comments: { data: [{ type: 'comments', id: '1' }, { type: 'comments', id: '2' }] },
author: { data: nil }
author: { data: { meta: {} } }
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions test/adapter/json_api/relationship_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ def test_relationship_with_data

def test_relationship_with_nil_model
@serializer = BlogSerializer.new(nil)
expected = { data: nil }
expected = { data: { meta: {} } }
test_relationship(expected, options: { include_data: true })
end

def test_relationship_with_nil_serializer
@serializer = nil
expected = { data: nil }
expected = { data: { meta: {} } }
test_relationship(expected, options: { include_data: true })
end

Expand Down

0 comments on commit 831a194

Please sign in to comment.