Skip to content

Commit

Permalink
fix(core): Fix entity hydration of nested array entities
Browse files Browse the repository at this point in the history
Fixes #2013
  • Loading branch information
michaelbromley committed Aug 10, 2023
1 parent 4a3bdec commit 55009a5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 12 deletions.
3 changes: 2 additions & 1 deletion packages/core/e2e/entity-hydrator.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -257,13 +257,14 @@ describe('Entity hydration', () => {
orderResultGuard.assertSuccess(addItemToOrder);
const channel = await server.app.get(ChannelService).getDefaultChannel();
// This is ugly, but in our real life example we use a CTX constructed by Vendure
const internalOrderId = +addItemToOrder.id.replace(/^\D+/g, '');
const ctx = new RequestContext({
channel,
authorizedAsOwnerOnly: true,
apiType: 'shop',
isAuthorized: true,
session: {
activeOrderId: addItemToOrder.id,
activeOrderId: internalOrderId,
activeChannelId: 1,
user: {
id: 2,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -212,20 +212,34 @@ export class EntityHydrator {
entity: VendureEntity,
path: string[],
): VendureEntity | VendureEntity[] | undefined {
let relation: any = entity;
for (let i = 0; i < path.length; i++) {
const part = path[i];
const isLast = i === path.length - 1;
if (relation[part]) {
relation =
Array.isArray(relation[part]) && relation[part].length && !isLast
? relation[part][0]
: relation[part];
} else {
let isArrayResult = false;
const result: VendureEntity[] = [];

function visit(parent: any, parts: string[]): any {
if (parts.length === 0) {
return;
}
const part = parts.shift() as string;
const target = parent[part];
if (Array.isArray(target)) {
isArrayResult = true;
if (parts.length === 0) {
result.push(...target);
} else {
for (const item of target) {
visit(item, parts.slice());
}
}
} else {
if (parts.length === 0) {
result.push(target);
} else {
visit(target, parts.slice());
}
}
}
return relation;
visit(entity, path.slice());
return isArrayResult ? result : result[0];
}

private getRelationEntityTypeAtPath(entity: VendureEntity, path: string): Type<VendureEntity> {
Expand Down

0 comments on commit 55009a5

Please sign in to comment.