fix(core): make deepCopy backward compatible (#4505)

* fix(core): make `deepCopy` backward compatible

`JSON.parse(JSON.stringify())`  uses `.toJSON` when available. so should `deepCopy`

* fix(core): prevent double quotes on luxon datetimes (#4508)

* 🐛 Prevent double quotes on luxon datetimes

*  Generalize solution

* update the types in packages/workflow/src/utils.ts

* add `toJSON` check to NodeErrors.isTraversableObject as well

* move the toJSON check before the cyclic dependency check

* fix(core): keep backward compatibility in deepCopy by calling `toJSON` on objects that have it

* fix(core): updating deepCopy typings

* Revert "fix(core): updating deepCopy typings"

This reverts commit 100a0f1f3d7ddac5425ccc8498381324f418d7a2.

* fix(core): temporarily removing Date cloning from deepCopy

* fix(core): updating deepCopy types

* fix(core): updating deepCopy

* fix(core): updating deepCopy get prototype of object

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: Csaba Tuncsik <csaba@n8n.io>
This commit is contained in:
कारतोफ्फेलस्क्रिप्ट™
2022-11-02 17:44:12 +01:00
committed by GitHub
parent 721ef26d5f
commit b282c7e5d9
4 changed files with 41 additions and 24 deletions

View File

@@ -19,6 +19,11 @@ describe('jsonParse', () => {
describe('deepCopy', () => {
it('should deep copy an object', () => {
const serializable = {
x: 1,
y: 2,
toJSON: () => 'x:1,y:2',
};
const object = {
deep: {
props: {
@@ -26,6 +31,7 @@ describe('deepCopy', () => {
},
arr: [1, 2, 3],
},
serializable,
arr: [
{
prop: {
@@ -34,17 +40,18 @@ describe('deepCopy', () => {
},
],
func: () => {},
date: new Date(),
date: new Date(1667389172201),
undef: undefined,
nil: null,
bool: true,
num: 1,
};
const copy = deepCopy(object);
expect(copy).toEqual(object);
expect(copy).not.toBe(object);
expect(copy.arr).toEqual(object.arr);
expect(copy.arr).not.toBe(object.arr);
expect(copy.date).toBe('2022-11-02T11:39:32.201Z');
expect(copy.serializable).toBe(serializable.toJSON());
expect(copy.deep.props).toEqual(object.deep.props);
expect(copy.deep.props).not.toBe(object.deep.props);
});
@@ -65,7 +72,7 @@ describe('deepCopy', () => {
},
],
func: () => {},
date: new Date(),
date: new Date(1667389172201),
undef: undefined,
nil: null,
bool: true,
@@ -74,14 +81,16 @@ describe('deepCopy', () => {
object.circular = object;
object.deep.props.circular = object;
object.deep.arr.push(object)
object.deep.arr.push(object);
const copy = deepCopy(object);
expect(copy).toEqual(object);
expect(copy).not.toBe(object);
expect(copy.arr).toEqual(object.arr);
expect(copy.arr).not.toBe(object.arr);
expect(copy.deep.props).toEqual(object.deep.props);
expect(copy.deep.props).not.toBe(object.deep.props);
expect(copy.date).toBe('2022-11-02T11:39:32.201Z');
expect(copy.deep.props.circular).toBe(copy);
expect(copy.deep.props.circular).not.toBe(object);
expect(copy.deep.arr.slice(-1)[0]).toBe(copy);
expect(copy.deep.arr.slice(-1)[0]).not.toBe(object);
});
});