Skip to content

Commit e2784d9

Browse files
authored
Merge pull request #353 from dugsmith/fix_included_data
fix issue #352: relationships w/o included data
2 parents 2b2033a + 3939323 commit e2784d9

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
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+
- [#353](https://github.com/JsonApiClient/json_api_client/pull/353) - fix to support deserializing resources with relationships without those related resources being included in the response (issue [#352](https://github.com/JsonApiClient/json_api_client/issues/352)).
6+
57
## 1.14.0
68

79
- [#338](https://github.com/JsonApiClient/json_api_client/pull/338) - implement hash and eql? for builder class

lib/json_api_client/included_data.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ def data_for(method_name, definition)
3636

3737
if data.is_a?(Array)
3838
# has_many link
39-
data.map do |link_def|
40-
record_for(link_def)
41-
end
39+
data.map(&method(:record_for)).compact
4240
else
4341
# has_one link
4442
record_for(data)
@@ -53,7 +51,8 @@ def has_link?(name)
5351

5452
# should return a resource record of some type for this linked document
5553
def record_for(link_def)
56-
data[link_def["type"]][link_def["id"]]
54+
record = data[link_def["type"]]
55+
record[link_def["id"]] if record
5756
end
5857
end
5958
end
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
require 'test_helper'
2+
3+
class CompoundNonIncludedDocumentTest < MiniTest::Test
4+
5+
TEST_DATA = %{
6+
{
7+
"links": {
8+
"self": "http://example.com/posts",
9+
"next": "http://example.com/posts?page[offset]=2",
10+
"last": "http://example.com/posts?page[offset]=10"
11+
},
12+
"data": [{
13+
"type": "posts",
14+
"id": "1",
15+
"attributes": {
16+
"title": "JSON API paints my bikeshed!"
17+
},
18+
"relationships": {
19+
"author": {
20+
"links": {
21+
"self": "http://example.com/posts/1/relationships/author",
22+
"related": "http://example.com/posts/1/author"
23+
},
24+
"data": { "type": "people", "id": "9" }
25+
},
26+
"comments": {
27+
"links": {
28+
"self": "http://example.com/posts/1/relationships/comments",
29+
"related": "http://example.com/posts/1/comments"
30+
},
31+
"data": [
32+
{ "type": "comments", "id": "5" },
33+
{ "type": "comments", "id": "12" }
34+
]
35+
}
36+
},
37+
"links": {
38+
"self": "http://example.com/posts/1"
39+
}
40+
}]
41+
}
42+
}
43+
44+
def test_can_handle_related_data_without_included
45+
stub_request(:get, "http://example.com/articles")
46+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: TEST_DATA)
47+
48+
articles = Article.all
49+
50+
assert articles.is_a?(JsonApiClient::ResultSet)
51+
assert_equal 1, articles.length
52+
53+
article = articles.first
54+
assert_equal "1", article.id
55+
assert_equal "JSON API paints my bikeshed!", article.title
56+
57+
# has_one is nil if not included
58+
assert_nil article.author
59+
60+
# has_many is empty if not included
61+
assert_equal 0, article.comments.size
62+
end
63+
end

0 commit comments

Comments
 (0)