From dafacb90c6b01e2f88c3de359ebb2d8d55e0aecc Mon Sep 17 00:00:00 2001 From: Michael Kret <88898367+michael-radency@users.noreply.github.com> Date: Thu, 25 Jan 2024 19:11:23 +0200 Subject: [PATCH] fix(Merge Node): Passing on no items to "Input 2" results in wrong output items (#8438) --- .../test/node/workflow.keep_non_matches.json | 588 ++++++++++++++++++ .../nodes-base/nodes/Merge/v2/MergeV2.node.ts | 5 + 2 files changed, 593 insertions(+) create mode 100644 packages/nodes-base/nodes/Merge/test/node/workflow.keep_non_matches.json diff --git a/packages/nodes-base/nodes/Merge/test/node/workflow.keep_non_matches.json b/packages/nodes-base/nodes/Merge/test/node/workflow.keep_non_matches.json new file mode 100644 index 0000000000000..8c2f85552a14b --- /dev/null +++ b/packages/nodes-base/nodes/Merge/test/node/workflow.keep_non_matches.json @@ -0,0 +1,588 @@ +{ + "name": "merge fix", + "nodes": [ + { + "parameters": {}, + "id": "94003e55-6c4e-492f-802a-49f4fb5b5f4b", + "name": "When clicking \"Test Workflow\"", + "type": "n8n-nodes-base.manualTrigger", + "typeVersion": 1, + "position": [ + 120, + 940 + ] + }, + { + "parameters": { + "jsCode": "return [\n {\n \"id\": 1,\n \"first_name\": \"John\",\n \"last_name\": \"Doe\"\n },\n {\n \"id\": 2,\n \"first_name\": \"Jane\",\n \"last_name\": \"Smith\"\n },\n {\n \"id\": 3,\n \"first_name\": \"Michael\",\n \"last_name\": \"Johnson\"\n },\n {\n \"id\": 4,\n \"first_name\": \"Emily\",\n \"last_name\": \"Davis\"\n },\n {\n \"id\": 5,\n \"first_name\": \"Robert\",\n \"last_name\": \"Taylor\"\n },\n {\n \"id\": 6,\n \"first_name\": \"Olivia\",\n \"last_name\": \"Martin\"\n },\n {\n \"id\": 7,\n \"first_name\": \"William\",\n \"last_name\": \"Brown\"\n },\n {\n \"id\": 8,\n \"first_name\": \"Sophia\",\n \"last_name\": \"Anderson\"\n },\n {\n \"id\": 9,\n \"first_name\": \"Daniel\",\n \"last_name\": \"Wilson\"\n },\n {\n \"id\": 10,\n \"first_name\": \"Ava\",\n \"last_name\": \"Miller\"\n }\n];" + }, + "id": "da82fbb0-b689-4c4e-b310-303361e8411c", + "name": "Mock data A", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 340, + 840 + ] + }, + { + "parameters": { + "jsCode": "return [\n];" + }, + "id": "493ecb02-52e7-43b7-8c73-d45a3f7e76f6", + "name": "Mock data B", + "type": "n8n-nodes-base.code", + "typeVersion": 2, + "position": [ + 340, + 1040 + ] + }, + { + "parameters": { + "mode": "combine", + "mergeByFields": { + "values": [ + { + "field1": "id", + "field2": "id" + } + ] + }, + "joinMode": "keepNonMatches", + "options": {} + }, + "id": "d0c0f123-1461-4b78-9cf8-6410440430d3", + "name": "Merge", + "type": "n8n-nodes-base.merge", + "typeVersion": 2.1, + "position": [ + 700, + 500 + ] + }, + { + "parameters": { + "mode": "combine", + "mergeByFields": { + "values": [ + { + "field1": "id", + "field2": "id" + } + ] + }, + "joinMode": "keepNonMatches", + "options": {} + }, + "id": "5093ec3e-c987-44c7-815f-279ea7842db5", + "name": "Merge1", + "type": "n8n-nodes-base.merge", + "typeVersion": 2.1, + "position": [ + 700, + 680 + ] + }, + { + "parameters": { + "mode": "combine", + "mergeByFields": { + "values": [ + { + "field1": "id", + "field2": "id" + } + ] + }, + "joinMode": "keepNonMatches", + "outputDataFrom": "input1", + "options": {} + }, + "id": "0453bb35-3971-4a23-93b7-31853e4634fb", + "name": "Merge2", + "type": "n8n-nodes-base.merge", + "typeVersion": 2.1, + "position": [ + 700, + 880 + ] + }, + { + "parameters": { + "mode": "combine", + "mergeByFields": { + "values": [ + { + "field1": "id", + "field2": "id" + } + ] + }, + "joinMode": "keepNonMatches", + "outputDataFrom": "input1", + "options": {} + }, + "id": "f09c9327-4557-406b-8ae0-db851055dd08", + "name": "Merge3", + "type": "n8n-nodes-base.merge", + "typeVersion": 2.1, + "position": [ + 700, + 1060 + ], + "alwaysOutputData": true + }, + { + "parameters": { + "mode": "combine", + "mergeByFields": { + "values": [ + { + "field1": "id", + "field2": "id" + } + ] + }, + "joinMode": "keepNonMatches", + "outputDataFrom": "input2", + "options": {} + }, + "id": "d5bc6ee7-141e-4eba-a9aa-cb866b177b89", + "name": "Merge4", + "type": "n8n-nodes-base.merge", + "typeVersion": 2.1, + "position": [ + 700, + 1280 + ], + "alwaysOutputData": true + }, + { + "parameters": { + "mode": "combine", + "mergeByFields": { + "values": [ + { + "field1": "id", + "field2": "id" + } + ] + }, + "joinMode": "keepNonMatches", + "outputDataFrom": "input2", + "options": {} + }, + "id": "4c881679-3f76-491a-a2c9-e35e4c9e28ff", + "name": "Merge5", + "type": "n8n-nodes-base.merge", + "typeVersion": 2.1, + "position": [ + 700, + 1460 + ], + "alwaysOutputData": true + } + ], + "pinData": { + "Merge": [ + { + "json": { + "id": 1, + "first_name": "John", + "last_name": "Doe" + } + }, + { + "json": { + "id": 2, + "first_name": "Jane", + "last_name": "Smith" + } + }, + { + "json": { + "id": 3, + "first_name": "Michael", + "last_name": "Johnson" + } + }, + { + "json": { + "id": 4, + "first_name": "Emily", + "last_name": "Davis" + } + }, + { + "json": { + "id": 5, + "first_name": "Robert", + "last_name": "Taylor" + } + }, + { + "json": { + "id": 6, + "first_name": "Olivia", + "last_name": "Martin" + } + }, + { + "json": { + "id": 7, + "first_name": "William", + "last_name": "Brown" + } + }, + { + "json": { + "id": 8, + "first_name": "Sophia", + "last_name": "Anderson" + } + }, + { + "json": { + "id": 9, + "first_name": "Daniel", + "last_name": "Wilson" + } + }, + { + "json": { + "id": 10, + "first_name": "Ava", + "last_name": "Miller" + } + } + ], + "Merge1": [ + { + "json": { + "id": 1, + "first_name": "John", + "last_name": "Doe" + } + }, + { + "json": { + "id": 2, + "first_name": "Jane", + "last_name": "Smith" + } + }, + { + "json": { + "id": 3, + "first_name": "Michael", + "last_name": "Johnson" + } + }, + { + "json": { + "id": 4, + "first_name": "Emily", + "last_name": "Davis" + } + }, + { + "json": { + "id": 5, + "first_name": "Robert", + "last_name": "Taylor" + } + }, + { + "json": { + "id": 6, + "first_name": "Olivia", + "last_name": "Martin" + } + }, + { + "json": { + "id": 7, + "first_name": "William", + "last_name": "Brown" + } + }, + { + "json": { + "id": 8, + "first_name": "Sophia", + "last_name": "Anderson" + } + }, + { + "json": { + "id": 9, + "first_name": "Daniel", + "last_name": "Wilson" + } + }, + { + "json": { + "id": 10, + "first_name": "Ava", + "last_name": "Miller" + } + } + ], + "Merge2": [ + { + "json": { + "id": 1, + "first_name": "John", + "last_name": "Doe" + } + }, + { + "json": { + "id": 2, + "first_name": "Jane", + "last_name": "Smith" + } + }, + { + "json": { + "id": 3, + "first_name": "Michael", + "last_name": "Johnson" + } + }, + { + "json": { + "id": 4, + "first_name": "Emily", + "last_name": "Davis" + } + }, + { + "json": { + "id": 5, + "first_name": "Robert", + "last_name": "Taylor" + } + }, + { + "json": { + "id": 6, + "first_name": "Olivia", + "last_name": "Martin" + } + }, + { + "json": { + "id": 7, + "first_name": "William", + "last_name": "Brown" + } + }, + { + "json": { + "id": 8, + "first_name": "Sophia", + "last_name": "Anderson" + } + }, + { + "json": { + "id": 9, + "first_name": "Daniel", + "last_name": "Wilson" + } + }, + { + "json": { + "id": 10, + "first_name": "Ava", + "last_name": "Miller" + } + } + ], + "Merge3": [ + { + "json": {} + } + ], + "Merge4": [ + { + "json": {} + } + ], + "Merge5": [ + { + "json": { + "id": 1, + "first_name": "John", + "last_name": "Doe" + } + }, + { + "json": { + "id": 2, + "first_name": "Jane", + "last_name": "Smith" + } + }, + { + "json": { + "id": 3, + "first_name": "Michael", + "last_name": "Johnson" + } + }, + { + "json": { + "id": 4, + "first_name": "Emily", + "last_name": "Davis" + } + }, + { + "json": { + "id": 5, + "first_name": "Robert", + "last_name": "Taylor" + } + }, + { + "json": { + "id": 6, + "first_name": "Olivia", + "last_name": "Martin" + } + }, + { + "json": { + "id": 7, + "first_name": "William", + "last_name": "Brown" + } + }, + { + "json": { + "id": 8, + "first_name": "Sophia", + "last_name": "Anderson" + } + }, + { + "json": { + "id": 9, + "first_name": "Daniel", + "last_name": "Wilson" + } + }, + { + "json": { + "id": 10, + "first_name": "Ava", + "last_name": "Miller" + } + } + ] + }, + "connections": { + "When clicking \"Test Workflow\"": { + "main": [ + [ + { + "node": "Mock data A", + "type": "main", + "index": 0 + }, + { + "node": "Mock data B", + "type": "main", + "index": 0 + } + ] + ] + }, + "Mock data A": { + "main": [ + [ + { + "node": "Merge", + "type": "main", + "index": 0 + }, + { + "node": "Merge1", + "type": "main", + "index": 1 + }, + { + "node": "Merge2", + "type": "main", + "index": 0 + }, + { + "node": "Merge3", + "type": "main", + "index": 1 + }, + { + "node": "Merge4", + "type": "main", + "index": 0 + }, + { + "node": "Merge5", + "type": "main", + "index": 1 + } + ] + ] + }, + "Mock data B": { + "main": [ + [ + { + "node": "Merge", + "type": "main", + "index": 1 + }, + { + "node": "Merge1", + "type": "main", + "index": 0 + }, + { + "node": "Merge2", + "type": "main", + "index": 1 + }, + { + "node": "Merge3", + "type": "main", + "index": 0 + }, + { + "node": "Merge4", + "type": "main", + "index": 1 + }, + { + "node": "Merge5", + "type": "main", + "index": 0 + } + ] + ] + } + }, + "active": false, + "settings": { + "executionOrder": "v1" + }, + "versionId": "54329e46-4d05-491a-919c-e156e77d6d11", + "meta": { + "templateCredsSetupCompleted": true, + "instanceId": "b888bd11cd1ddbb95450babf3e199556799d999b896f650de768b8370ee50363" + }, + "id": "WbgpQNak2tO6spx2", + "tags": [] +} diff --git a/packages/nodes-base/nodes/Merge/v2/MergeV2.node.ts b/packages/nodes-base/nodes/Merge/v2/MergeV2.node.ts index 2df877965818b..5ade42d061ba0 100644 --- a/packages/nodes-base/nodes/Merge/v2/MergeV2.node.ts +++ b/packages/nodes-base/nodes/Merge/v2/MergeV2.node.ts @@ -485,6 +485,11 @@ export class MergeV2 implements INodeType { } if (input1.length === 0 || input2.length === 0) { + if (!input1.length && joinMode === 'keepNonMatches' && outputDataFrom === 'input1') + return [returnData]; + if (!input2.length && joinMode === 'keepNonMatches' && outputDataFrom === 'input2') + return [returnData]; + if (joinMode === 'keepMatches') { // Stop the execution return [[]];