Handle duplicate includes #41
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR enables rendering multiple included resources together by grouping them in arrays first (if their
jsonapi_typeandjsonapi_idare the same) and then rendering them one-by-one while merging the rendering results.Rationale: we use Rails and Graphiti and encountered the following problem: we added a few relationships (e.g.
specific) that are not represented in corresponding models to GraphitiResources (that are basically JSON:API wrappers). Then we have a child resource (that obviously goes into@includedarray) that contains these specific relationships and is referenced by two different paths from a parent resource (e.g.path1andpath2). Then we have a complex request for the parent resource, including two references to the same child object with the first reference requesting the specific resource while the second reference is not (something likeinclude=path1.specific,path2). When Graphiti is processing the request it enriches the first referenced object with these specific relationships while leaving the second object alone with no modifications. In this case we have two different objects with the samejsonapi_typeandjsonapi_idbut with slightly different behavior. Sadly when squashing the references tree into simple@primaryand@includedobjects (intraverse_resources) we enrich all includes (by merging includes in the@include_rels) but we leave only one resource (which may miss these specific relationships). And so when trying to process includes from this resource we end up with a relationship withdata: nilsince this resource misses thespecificrelationship.BTW it is interesting that somehow the rightmost reference in the
includeparameter is processed first, so the result is dependent on the order of includes. If we useinclude=path2,path1.specificwe will have proper data in the response.Edit: it is not the rightmost reference that matters but a combination of a field (relationship) definition inside the resource and the
includereference (which field is used in theincludesection)