Skip to content

Commit

Permalink
Merge pull request #373 from Martinarbez/fix/multiple.includes.nested…
Browse files Browse the repository at this point in the history
…Resuorces

fix/multiple.includes.nestedResources
  • Loading branch information
marcemira authored Dec 22, 2023
2 parents d741f0f + d383e52 commit cbd5337
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/processors/knex-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ const parseOperationIncludedRelationships = (
.filter((include) => include.length > 1)
.reduce((acumRelationships, [nestedOrigin, nestedRelationshipName]) => {
acumRelationships[nestedOrigin] = {
...acumRelationships[nestedOrigin],
[nestedRelationshipName]: relationships[nestedOrigin].type().schema.relationships[nestedRelationshipName],
};
return acumRelationships;
Expand Down
4 changes: 1 addition & 3 deletions src/serializers/serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,6 @@ export default class JsonApiSerializer implements IJsonApiSerializer {
}
},
);
return [...new Set(includedData.map((item: Resource) => `${item.type}_${item.id}`))].map((typeId) =>
includedData.find((item: Resource) => `${item.type}_${item.id}` === typeId),
);
return includedData
}
}
12 changes: 12 additions & 0 deletions tests/test-suite/acceptance/factories/nestedResources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,17 @@ export default {
data: getFactoryObject(comments)(1),
included: [getFactoryObject(comments)(2), getFactoryObject(comments)(3)],
},
authorAndAuthorVotesAndAuthorCommentsOf1stArticle: {
data: {
...getFactoryObject(articles)(1),
relationships: {
...getExtraRelationships(users, "author")([1], "Object"),
},
meta: {
hello: "world",
},
},
included: [getFactoryObject(users)(1), ...getFactoryObjects(votes)([1, 2]), ...getFactoryObjects(comments)([1, 3])],
},
},
};
7 changes: 7 additions & 0 deletions tests/test-suite/acceptance/nestedResources.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ describe.each(transportLayers)("Transport Layer: %s", (transportLayer) => {
expect(result.status).toEqual(200);
expect(result.body).toEqual(nested.get.parentCommentAndParentCommentsParentCommentOf1stComment);
});

it("Get the 1st article'Author's Votes and Comments (*-1-*)", async () => {
const authData = await getAuthenticationData();
const result = await request.get("/articles/1?include=author.votes,author.comments").set("Authorization", authData.token);
expect(result.status).toEqual(200);
expect(result.body).toEqual(nested.get.authorAndAuthorVotesAndAuthorCommentsOf1stArticle);
});
});
});
});

0 comments on commit cbd5337

Please sign in to comment.