From 36bc164da486f2e2d05091b457b8eea6521ca22e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E0=A4=95=E0=A4=BE=E0=A4=B0=E0=A4=A4=E0=A5=8B=E0=A4=AB?= =?UTF-8?q?=E0=A5=8D=E0=A4=AB=E0=A5=87=E0=A4=B2=E0=A4=B8=E0=A5=8D=E0=A4=95?= =?UTF-8?q?=E0=A5=8D=E0=A4=B0=E0=A4=BF=E0=A4=AA=E0=A5=8D=E0=A4=9F=E2=84=A2?= Date: Tue, 21 Jan 2025 11:49:43 +0100 Subject: [PATCH] fix(core): AugmentObject should handle the constructor property correctly (#12744) --- packages/workflow/src/AugmentObject.ts | 3 +++ packages/workflow/test/AugmentObject.test.ts | 28 ++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/packages/workflow/src/AugmentObject.ts b/packages/workflow/src/AugmentObject.ts index 24d6ca0a16..bb060a167d 100644 --- a/packages/workflow/src/AugmentObject.ts +++ b/packages/workflow/src/AugmentObject.ts @@ -36,6 +36,7 @@ export function augmentArray(data: T[]): T[] { return Reflect.deleteProperty(getData(), key); }, get(target, key: string, receiver): unknown { + if (key === 'constructor') return Array; const value = Reflect.get(newData ?? target, key, receiver) as unknown; const newValue = augment(value); if (newValue !== value) { @@ -83,6 +84,8 @@ export function augmentObject(data: T): T { const proxy = new Proxy(data, { get(target, key: string, receiver): unknown { + if (key === 'constructor') return Object; + if (deletedProperties.has(key)) { return undefined; } diff --git a/packages/workflow/test/AugmentObject.test.ts b/packages/workflow/test/AugmentObject.test.ts index 1bbe64561f..831f08e020 100644 --- a/packages/workflow/test/AugmentObject.test.ts +++ b/packages/workflow/test/AugmentObject.test.ts @@ -10,6 +10,8 @@ describe('AugmentObject', () => { const augmentedObject = augmentArray(originalObject); + expect(augmentedObject.constructor.name).toEqual('Array'); + expect(augmentedObject.push(5)).toEqual(6); expect(augmentedObject).toEqual([1, 2, 3, 4, null, 5]); expect(originalObject).toEqual(copyOriginal); @@ -207,6 +209,8 @@ describe('AugmentObject', () => { const augmentedObject = augmentObject(originalObject); + expect(augmentedObject.constructor.name).toEqual('Object'); + augmentedObject[1] = 911; expect(originalObject[1]).toEqual(11); expect(augmentedObject[1]).toEqual(911); @@ -589,5 +593,29 @@ describe('AugmentObject', () => { delete augmentedObject.toString; expect(augmentedObject.toString).toBeUndefined(); }); + + test('should handle constructor property correctly', () => { + const originalObject: any = { + a: { + b: { + c: { + d: '4', + }, + }, + }, + }; + const augmentedObject = augmentObject(originalObject); + + expect(augmentedObject.constructor.name).toEqual('Object'); + expect(augmentedObject.a.constructor.name).toEqual('Object'); + expect(augmentedObject.a.b.constructor.name).toEqual('Object'); + expect(augmentedObject.a.b.c.constructor.name).toEqual('Object'); + + augmentedObject.constructor = {}; + expect(augmentedObject.constructor.name).toEqual('Object'); + + delete augmentedObject.constructor; + expect(augmentedObject.constructor.name).toEqual('Object'); + }); }); });