Skip to content

Commit edc52d5

Browse files
authored
Merge branch 'master' into feature/fix_pagination_issues
2 parents 0cd860b + 02e0fb3 commit edc52d5

File tree

5 files changed

+96
-16
lines changed

5 files changed

+96
-16
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
- [#348](https://github.com/JsonApiClient/json_api_client/pull/348) - add NestedParamPaginator to address inconsistency in handling of pagination query string params (issue [#347](https://github.com/JsonApiClient/json_api_client/issues/347)).
66

7+
## 1.12.2
8+
9+
- [#350](https://github.com/JsonApiClient/json_api_client/pull/350) - fix resource including with blank `relationships` response data
10+
11+
## 1.12.1
12+
13+
- [#349](https://github.com/JsonApiClient/json_api_client/pull/349) - fix resource including for STI objects
14+
715
## 1.12.0
816

917
- [#345](https://github.com/JsonApiClient/json_api_client/pull/345) - track the real HTTP reason of ApiErrors

lib/json_api_client/included_data.rb

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,30 @@ class IncludedData
44

55
def initialize(result_set, data)
66
record_class = result_set.record_class
7-
included_set = data.map do |datum|
8-
type = datum["type"]
7+
grouped_data = data.group_by{|datum| datum["type"]}
8+
grouped_included_set = grouped_data.each_with_object({}) do |(type, records), h|
99
klass = Utils.compute_type(record_class, record_class.key_formatter.unformat(type).singularize.classify)
10-
params = klass.parser.parameters_from_resource(datum)
11-
resource = klass.load(params)
12-
resource.last_result_set = result_set
13-
resource
10+
h[type] = records.map do |record|
11+
params = klass.parser.parameters_from_resource(record)
12+
klass.load(params).tap do |resource|
13+
resource.last_result_set = result_set
14+
end
15+
end
1416
end
1517

16-
included_set.concat(result_set) if record_class.search_included_in_result_set
17-
@data = included_set.group_by(&:type).inject({}) do |h, (type, resources)|
18-
h[type] = resources.index_by(&:id)
19-
h
18+
if record_class.search_included_in_result_set
19+
# deep_merge overrides the nested Arrays o_O
20+
# {a: [1,2]}.deep_merge(a: [3,4]) # => {a: [3,4]}
21+
grouped_included_set.merge!(result_set.group_by(&:type)) do |_, resources1, resources2|
22+
resources1 + resources2
23+
end
2024
end
25+
26+
grouped_included_set.each do |type, resources|
27+
grouped_included_set[type] = resources.index_by(&:id)
28+
end
29+
30+
@data = grouped_included_set
2131
end
2232

2333
def data_for(method_name, definition)

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)

lib/json_api_client/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module JsonApiClient
2-
VERSION = "1.12.0"
2+
VERSION = "1.12.2"
33
end

test/unit/association_test.rb

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -854,6 +854,70 @@ def test_get_with_defined_relationship_for_model_with_custom_type
854854
assert user.files.first.is_a?(DocumentFile)
855855
end
856856

857+
def test_get_with_type_attribute
858+
stub_request(:get, "http://example.com/document_users/1?include=file")
859+
.to_return(headers: {content_type: "application/vnd.api+json"}, body: {
860+
data: [
861+
{
862+
id: '1',
863+
type: 'document_users',
864+
attributes: {
865+
name: 'John Doe'
866+
},
867+
relationships: {
868+
file: {
869+
links: {
870+
self: 'http://example.com/document_users/1/relationships/file',
871+
related: 'http://example.com/document_users/1/file'
872+
},
873+
data: {
874+
id: '2',
875+
type: 'document--files'
876+
}
877+
}
878+
}
879+
}
880+
],
881+
included: [
882+
{
883+
id: '2',
884+
type: 'document--files',
885+
attributes: {
886+
type: 'STIDocumentFile',
887+
url: 'http://example.com/downloads/2.pdf'
888+
}
889+
}
890+
]
891+
}.to_json)
892+
893+
user = DocumentUser.includes('file').find(1).first
894+
895+
assert_equal 'STIDocumentFile', user.file.type
896+
assert user.file.is_a?(DocumentFile)
897+
end
898+
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+
857921
def test_load_include_from_dataset
858922
stub_request(:get, 'http://example.com/employees?include=chief&page[per_page]=2')
859923
.to_return(

0 commit comments

Comments
 (0)