From d383e52e8f658eaf650f2d8c7a9043e129570920 Mon Sep 17 00:00:00 2001 From: Martin Arbez Date: Fri, 22 Dec 2023 09:09:31 -0300 Subject: [PATCH] fix/multiple.includes.nestedResources --- src/processors/knex-processor.ts | 1 + src/serializers/serializer.ts | 4 +--- .../acceptance/factories/nestedResources.ts | 12 ++++++++++++ tests/test-suite/acceptance/nestedResources.test.ts | 7 +++++++ 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/processors/knex-processor.ts b/src/processors/knex-processor.ts index 85f482f..8aa16d4 100644 --- a/src/processors/knex-processor.ts +++ b/src/processors/knex-processor.ts @@ -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; diff --git a/src/serializers/serializer.ts b/src/serializers/serializer.ts index c15e390..5c4af97 100644 --- a/src/serializers/serializer.ts +++ b/src/serializers/serializer.ts @@ -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 } } diff --git a/tests/test-suite/acceptance/factories/nestedResources.ts b/tests/test-suite/acceptance/factories/nestedResources.ts index 72e73e2..8fae73c 100644 --- a/tests/test-suite/acceptance/factories/nestedResources.ts +++ b/tests/test-suite/acceptance/factories/nestedResources.ts @@ -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])], + }, }, }; diff --git a/tests/test-suite/acceptance/nestedResources.test.ts b/tests/test-suite/acceptance/nestedResources.test.ts index f9e2ab8..2198951 100644 --- a/tests/test-suite/acceptance/nestedResources.test.ts +++ b/tests/test-suite/acceptance/nestedResources.test.ts @@ -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); + }); }); }); });