From 6c35ffa82c45434dadee0354b75a901d3f3d6e98 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: Wed, 29 Mar 2023 21:36:56 +0200 Subject: [PATCH] fix(core): Handle Date and RegExp objects in AugmentObject (#5809) --- packages/workflow/src/AugmentObject.ts | 53 ++++++++++---------- packages/workflow/test/AugmentObject.test.ts | 8 ++- 2 files changed, 34 insertions(+), 27 deletions(-) diff --git a/packages/workflow/src/AugmentObject.ts b/packages/workflow/src/AugmentObject.ts index cc6528560..a8b29d066 100644 --- a/packages/workflow/src/AugmentObject.ts +++ b/packages/workflow/src/AugmentObject.ts @@ -1,6 +1,24 @@ import type { IDataObject } from './Interfaces'; import util from 'util'; +function augment(value: T): T { + if ( + typeof value !== 'object' || + value === null || + util.types.isProxy(value) || + value instanceof RegExp + ) + return value; + + if (value instanceof Date) return new Date(value.valueOf()) as T; + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + if (Array.isArray(value)) return augmentArray(value) as T; + + // eslint-disable-next-line @typescript-eslint/no-use-before-define + return augmentObject(value) as T; +} + export function augmentArray(data: T[]): T[] { let newData: unknown[] | undefined = undefined; @@ -17,24 +35,12 @@ export function augmentArray(data: T[]): T[] { }, get(target, key: string, receiver): unknown { const value = Reflect.get(newData !== undefined ? newData : target, key, receiver) as unknown; - - if (typeof value === 'object') { - if (value === null || util.types.isProxy(value)) { - return value; - } - + const newValue = augment(value); + if (newValue !== value) { newData = getData(); - - if (Array.isArray(value)) { - Reflect.set(newData, key, augmentArray(value)); - } else { - // eslint-disable-next-line @typescript-eslint/no-use-before-define - Reflect.set(newData, key, augmentObject(value as IDataObject)); - } - - return Reflect.get(newData, key); + Reflect.set(newData, key, newValue); + return newValue; } - return value; }, getOwnPropertyDescriptor(target, key) { @@ -83,18 +89,13 @@ export function augmentObject(data: T): T { // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment const value = Reflect.get(target, key, receiver); - - if (value !== null && typeof value === 'object') { - if (Array.isArray(value)) { - newData[key] = augmentArray(value); - } else { - newData[key] = augmentObject(value as IDataObject); - } - - return newData[key]; + const newValue = augment(value); + if (newValue !== value) { + Object.assign(newData, { [key]: newValue }); + return newValue; } - return value as string; + return value; }, deleteProperty(target, key: string) { if (key in newData) { diff --git a/packages/workflow/test/AugmentObject.test.ts b/packages/workflow/test/AugmentObject.test.ts index e038779f1..f30989aec 100644 --- a/packages/workflow/test/AugmentObject.test.ts +++ b/packages/workflow/test/AugmentObject.test.ts @@ -193,11 +193,15 @@ describe('AugmentObject', () => { describe('augmentObject', () => { test('should work with simple values on first level', () => { + const date = new Date(1680089084200); + const regexp = new RegExp('^test$', 'ig'); const originalObject: IDataObject = { 1: 11, 2: '22', a: 111, b: '222', + d: date, + r: regexp, }; const copyOriginal = JSON.parse(JSON.stringify(originalObject)); @@ -221,7 +225,7 @@ describe('AugmentObject', () => { augmentedObject.c = 3; - expect(originalObject).toEqual(copyOriginal); + expect({ ...originalObject, d: date.toJSON(), r: {} }).toEqual(copyOriginal); expect(augmentedObject).toEqual({ 1: 911, @@ -229,6 +233,8 @@ describe('AugmentObject', () => { a: 9111, b: '9222', c: 3, + d: date, + r: regexp, }); });