Skip to content

Commit f328779

Browse files
authored
Merge pull request #350 from stokarenko/fix-including-with-blank-relationships
Fix including with blank relationships
2 parents 184c3d5 + 2483f48 commit f328779

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## Unreleased
44

5+
- [#350](https://github.com/JsonApiClient/json_api_client/pull/350) - fix resource including with blank `relationships` response data
6+
57
## 1.12.1
68

79
- [#349](https://github.com/JsonApiClient/json_api_client/pull/349) - fix resource including for STI objects

lib/json_api_client/resource.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -546,12 +546,10 @@ def relationship_data_for(name, relationship_definition)
546546
end
547547
end
548548

549-
url = relationship_definition["links"]["related"]
550-
if relationship_definition["links"] && url
551-
return association_for(name).data(url)
552-
end
549+
return unless links = relationship_definition["links"]
550+
return unless url = links["related"]
553551

554-
nil
552+
association_for(name).data(url)
555553
end
556554

557555
def relation_objects_for(name, relationship_definition)

test/unit/association_test.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,28 @@ def test_get_with_type_attribute
896896
assert user.file.is_a?(DocumentFile)
897897
end
898898

899+
def test_include_with_blank_relationships
900+
stub_request(:get, "http://example.com/document_users/1?include=file")
901+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
902+
data: [
903+
{
904+
id: '1',
905+
type: 'document_users',
906+
attributes: {
907+
name: 'John Doe'
908+
},
909+
relationships: {
910+
file: {
911+
}
912+
}
913+
}
914+
],
915+
}.to_json)
916+
917+
user = DocumentUser.includes('file').find(1).first
918+
assert_nil user.file
919+
end
920+
899921
def test_load_include_from_dataset
900922
stub_request(:get, 'http://example.com/employees?include=chief&page[per_page]=2')
901923
.to_return(

0 commit comments

Comments
 (0)