Skip to content

Commit

Permalink
fix(Merge Node): Do not error if expected key is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-radency authored and netroy committed May 2, 2023
1 parent 2672896 commit 8b59564
Show file tree
Hide file tree
Showing 11 changed files with 1,257 additions and 74 deletions.
40 changes: 26 additions & 14 deletions packages/nodes-base/nodes/CompareDatasets/CompareDatasets.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ import type {
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import { checkInput, checkMatchFieldsInput, findMatches } from './GenericFunctions';
import {
checkInput,
checkInputAndThrowError,
checkMatchFieldsInput,
findMatches,
} from './GenericFunctions';

export class CompareDatasets implements INodeType {
description: INodeTypeDescription = {
displayName: 'Compare Datasets',
name: 'compareDatasets',
icon: 'file:compare.svg',
group: ['transform'],
version: [1, 2, 2.1],
version: [1, 2, 2.1, 2.2],
description: 'Compare two inputs for changes',
defaults: { name: 'Compare Datasets' },
// eslint-disable-next-line n8n-nodes-base/node-class-description-inputs-wrong-regular-node
Expand Down Expand Up @@ -254,19 +259,26 @@ export class CompareDatasets implements INodeType {
options.fuzzyCompare = this.getNodeParameter('fuzzyCompare', 0, false) as boolean;
}

const input1 = checkInput(
this.getInputData(0),
matchFields.map((pair) => pair.field1),
(options.disableDotNotation as boolean) || false,
'Input A',
);
let input1 = this.getInputData(0);
let input2 = this.getInputData(1);
if (options.nodeVersion < 2.2) {
input1 = checkInputAndThrowError(
input1,
matchFields.map((pair) => pair.field1),
(options.disableDotNotation as boolean) || false,
'Input A',
);

const input2 = checkInput(
this.getInputData(1),
matchFields.map((pair) => pair.field2),
(options.disableDotNotation as boolean) || false,
'Input B',
);
input2 = checkInputAndThrowError(
input2,
matchFields.map((pair) => pair.field2),
(options.disableDotNotation as boolean) || false,
'Input B',
);
} else {
input1 = checkInput(input1);
input2 = checkInput(input2);
}

const resolve = this.getNodeParameter('resolve', 0, '') as string;
options.resolve = resolve;
Expand Down
10 changes: 9 additions & 1 deletion packages/nodes-base/nodes/CompareDatasets/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,15 @@ export function checkMatchFieldsInput(data: IDataObject[]) {
return data as PairToMatch[];
}

export function checkInput(
export function checkInput(input: INodeExecutionData[]) {
if (!input) return [];
if (input.some((item) => isEmpty(item.json))) {
input = input.filter((item) => !isEmpty(item.json));
}
return input;
}

export function checkInputAndThrowError(
input: INodeExecutionData[],
fields: string[],
disableDotNotation: boolean,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
{
"name": "do not error on missing keys",
"nodes": [
{
"parameters": {},
"id": "c7c0cf66-790a-4da7-81c8-ba9e4bbcec9a",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [720, 300]
},
{
"parameters": {
"jsCode": "return [\n {id: 1},\n {id: 2},\n {id: 3},\n {id: 4},\n];"
},
"id": "cec18624-ced0-4de1-8987-d4b184b136b9",
"name": "Code",
"type": "n8n-nodes-base.code",
"typeVersion": 1,
"position": [1020, 200]
},
{
"parameters": {
"jsCode": "return [\n];"
},
"id": "754d549c-82ce-4625-ba2b-6f8edcbf715e",
"name": "Code1",
"type": "n8n-nodes-base.code",
"typeVersion": 1,
"position": [1020, 400]
},
{
"parameters": {
"mergeByFields": {
"values": [
{
"field1": "idd",
"field2": "idd"
}
]
},
"options": {}
},
"id": "1b3660a2-a490-4524-a8ac-cd42fa2b340b",
"name": "Compare Datasets",
"type": "n8n-nodes-base.compareDatasets",
"typeVersion": 2.2,
"position": [1300, 420]
},
{
"parameters": {
"mergeByFields": {
"values": [
{
"field1": "idd",
"field2": "idd"
}
]
},
"options": {}
},
"id": "17e9b83b-828c-41a0-a7ba-e33cc66d37ad",
"name": "Any skipped",
"type": "n8n-nodes-base.compareDatasets",
"typeVersion": 2.1,
"position": [1300, 180],
"continueOnFail": true
},
{
"parameters": {},
"id": "551ed574-607a-4d98-9b06-350df92c805e",
"name": "No Operation, do nothing",
"type": "n8n-nodes-base.noOp",
"typeVersion": 1,
"position": [1540, 200]
},
{
"parameters": {},
"id": "00e75760-d88e-413c-b276-c759db72411f",
"name": "No Operation, do nothing1",
"type": "n8n-nodes-base.noOp",
"typeVersion": 1,
"position": [1540, 360]
}
],
"pinData": {
"No Operation, do nothing": [
{
"json": {
"id": 1
}
},
{
"json": {
"id": 2
}
},
{
"json": {
"id": 3
}
},
{
"json": {
"id": 4
}
}
],
"No Operation, do nothing1": [
{
"json": {
"id": 1
}
},
{
"json": {
"id": 2
}
},
{
"json": {
"id": 3
}
},
{
"json": {
"id": 4
}
}
]
},
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "Code",
"type": "main",
"index": 0
},
{
"node": "Code1",
"type": "main",
"index": 0
}
]
]
},
"Code": {
"main": [
[
{
"node": "Compare Datasets",
"type": "main",
"index": 0
},
{
"node": "Any skipped",
"type": "main",
"index": 0
}
]
]
},
"Code1": {
"main": [
[
{
"node": "Compare Datasets",
"type": "main",
"index": 1
},
{
"node": "Any skipped",
"type": "main",
"index": 1
}
]
]
},
"Any skipped": {
"main": [
[
{
"node": "No Operation, do nothing",
"type": "main",
"index": 0
}
]
]
},
"Compare Datasets": {
"main": [
[
{
"node": "No Operation, do nothing1",
"type": "main",
"index": 0
}
]
]
}
},
"active": false,
"settings": {},
"versionId": "11347bae-96af-4b28-80d7-fe272a165790",
"id": "8",
"meta": {
"instanceId": "6ebec4953fe56f1c009e7c3b107578b375137523af057073c0b5da17350651bd"
},
"tags": []
}
32 changes: 32 additions & 0 deletions packages/nodes-base/nodes/CompareDatasets/test/utils/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { INodeExecutionData } from 'n8n-workflow';
import { checkInput } from '../../GenericFunctions';

describe('Test Compare Datasets Node utils', () => {
it('test checkInput', () => {
const input1 = [
{ json: {} },
{
json: {
name: 'Test',
age: 30,
},
},
{
json: {
name: 'Test2',
age: 30,
},
},
];

expect(checkInput(input1).length).toEqual(2);

const input2: INodeExecutionData[] = [{ json: {} }];

expect(checkInput(input2).length).toEqual(0);

const input3 = undefined;

expect(checkInput(input3 as unknown as INodeExecutionData[]).length).toEqual(0);
});
});
3 changes: 2 additions & 1 deletion packages/nodes-base/nodes/ItemLists/ItemLists.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ export class ItemLists extends VersionedNodeType {
group: ['input'],
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Helper for working with lists of items and transforming arrays',
defaultVersion: 2,
defaultVersion: 2.1,
};

const nodeVersions: IVersionedNodeType['nodeVersions'] = {
1: new ItemListsV1(baseDescription),
2: new ItemListsV2(baseDescription),
2.1: new ItemListsV2(baseDescription),
};

super(nodeVersions, baseDescription);
Expand Down
Loading

0 comments on commit 8b59564

Please sign in to comment.