Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix/multiple.includes.nestedResources #373

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
});
});
});
});
Loading