Skip to content

Commit

Permalink
Fix empty relationships on included resources (#1372)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JamesGlover authored Aug 9, 2021
1 parent 695bb7c commit bf4b4cd
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/jsonapi/resource_tree.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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"=>[]
}
}
},
Expand Down

0 comments on commit bf4b4cd

Please sign in to comment.