From 12d27892d574ef5bd13e5e4e7369c8952ab41732 Mon Sep 17 00:00:00 2001 From: James Glover Date: Tue, 8 Jun 2021 14:35:58 +0100 Subject: [PATCH] Fix empty relationships on included resources JSONAPI::ResourceTree#load_included was failing to correctly populate the resource fragments of included resources, such that the `include_related` parameter was null. This was resulting in the relationships object lacking a data attribute for nil or empty resources. The JSON-API specification states that a null or empty array should be returned in these circumstances: https://jsonapi.org/format/#document-resource-object-linkage The underlying issue appears to be the use of `include_related` rather than the symbol `:include_related` when initializing nested resource fragments. --- lib/jsonapi/resource_tree.rb | 2 +- test/controllers/controller_test.rb | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/jsonapi/resource_tree.rb b/lib/jsonapi/resource_tree.rb index 7f873c324..0d8437f1c 100644 --- a/lib/jsonapi/resource_tree.rb +++ b/lib/jsonapi/resource_tree.rb @@ -85,7 +85,7 @@ def load_included(resource_klass, source_resource_tree, include_related, options find_related_resource_options) related_resource_tree = source_resource_tree.get_related_resource_tree(relationship) - related_resource_tree.add_resource_fragments(related_fragments, include_related[key][include_related]) + related_resource_tree.add_resource_fragments(related_fragments, include_related[key][:include_related]) # Now recursively get the related resources for the currently found resources load_included(relationship.resource_klass, diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index af72da60e..e2568f979 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -4371,7 +4371,8 @@ def test_complex_includes_things_nested_things "links" => { "self" => "http://test.host/api/things/40/relationships/things", "related" => "http://test.host/api/things/40/things" - } + }, + "data"=>[] } } },