Skip to content

Commit a3544c3

Browse files
committed
Fix including with blank relationships
1 parent 184c3d5 commit a3544c3

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
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: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,7 @@ def relationship_data_for(name, relationship_definition)
546546
end
547547
end
548548

549-
url = relationship_definition["links"]["related"]
550-
if relationship_definition["links"] && url
549+
if url = relationship_definition.dig("links", "related")
551550
return association_for(name).data(url)
552551
end
553552

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)