Description
What's the problem
Retrieving a relationship where the items are located in the primary "data" collection instead of the "included" collection won't return anything.
Steps to reproduce
For example: The JSON-API returns a collection of students and the studentGroups the students are assigned to. The studentGroups are available in the included segment of the response. The data segment contains all the students. There's a hasOne relationship from a student to a studentGroup, and a hasMany from a studentGroup to the containing students. The JSON:
{
"data": [{
"type": "student",
"id": "1",
"attributes": {
"name": "Rick"
},
"relationships": {
"student_group": {
"data": {
"type": "student_group",
"id": "1"
}
}
}
},
{
"type": "student",
"id": "2",
"attributes": {
"name": "Morty"
},
"relationships": {
"student_group": {
"data": {
"type": "student_group",
"id": "1"
}
}
}
},
{
"type": "student",
"id": "2",
"attributes": {
"name": "Jerry"
},
"relationships": {
"student_group": {
"data": {
"type": "student_group",
"id": "2"
}
}
}
}
],
"included": [{
"type": "student_group",
"id": "1",
"attributes": {
"description": "Awesome people",
"active": true
},
"relationships": {
"students": {
"data": [{
"type": "student",
"id": "1"
}, {
"type": "student",
"id": "2"
}]
}
}
},
{
"type": "student_group",
"id": "2",
"attributes": {
"description": "Annoying people",
"active": true
},
"relationships": {
"students": {
"data": [{
"type": "student",
"id": "3"
}]
}
}
}
]
}
When I try to retrieve all the students in a studentGroup, it will return an empty collection, since the students are not in the included-segment of the response. The JSON API specification says about this:
Compound documents require “full linkage”, meaning that every included resource MUST be identified by at least one resource identifier object in the same document. These resource identifier objects could either be primary data or represent resource linkage contained within primary or included resources.
Just adding the students to the included-segment as well, would be a violation of the spec (also, devour will enter an infinite loop when I do):
A compound document MUST NOT include more than one resource object for each type and id pair.
See: http://jsonapi.org/format/#document-compound-documents
How could this be fixed
It looks like the code only takes the items in included in to account when parsing relationships. This should be concatted with the items in the primary data as well.