From 0bd13c71739c4fb34feab4f7a169ee89bc77eee8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Wed, 7 Dec 2022 12:07:32 +0100 Subject: [PATCH] fix(core): Make expression resolution improvements (#4829) :zap: Make expression resolution improvements --- packages/workflow/src/Expression.ts | 9 ++--- packages/workflow/src/WorkflowDataProxy.ts | 40 ++++++++++++++++++++-- packages/workflow/test/Workflow.test.ts | 18 ++++++++++ 3 files changed, 59 insertions(+), 8 deletions(-) diff --git a/packages/workflow/src/Expression.ts b/packages/workflow/src/Expression.ts index d0bacdc7cbd9f..ee049e98af9f3 100644 --- a/packages/workflow/src/Expression.ts +++ b/packages/workflow/src/Expression.ts @@ -254,6 +254,7 @@ export class Expression { // Execute the expression const returnValue = this.renderExpression(parameterValue, data); if (typeof returnValue === 'function') { + if (returnValue.name === '$') throw new Error('invalid syntax'); throw new Error('Expression resolved to a function. Please add "()"'); } else if (typeof returnValue === 'string') { return returnValue; @@ -278,12 +279,8 @@ export class Expression { } } - if ( - error instanceof Error && - typeof error.message === 'string' && - error.name === 'SyntaxError' - ) { - throw new Error(error.message); + if (error instanceof Error && error.name === 'SyntaxError') { + throw new Error('invalid syntax'); } } diff --git a/packages/workflow/src/WorkflowDataProxy.ts b/packages/workflow/src/WorkflowDataProxy.ts index 547b5622f85f3..45cb7775d60de 100644 --- a/packages/workflow/src/WorkflowDataProxy.ts +++ b/packages/workflow/src/WorkflowDataProxy.ts @@ -126,6 +126,10 @@ export class WorkflowDataProxy { const that = this; const node = this.workflow.nodes[nodeName]; + if (!that.runExecutionData?.executionData && that.connectionInputData.length > 1) { + return {}; // incoming connection has pinned data, so stub context object + } + if (!that.runExecutionData?.executionData) { throw new ExpressionError( `The workflow hasn't been executed yet, so you can't reference any context data`, @@ -332,7 +336,7 @@ export class WorkflowDataProxy { ); if (nodeConnection === undefined) { - throw new ExpressionError(`connect ${that.activeNodeName} to ${nodeName}`, { + throw new ExpressionError(`connect "${that.activeNodeName}" to "${nodeName}"`, { runIndex: that.runIndex, itemIndex: that.itemIndex, }); @@ -569,7 +573,39 @@ export class WorkflowDataProxy { {}, { get(target, name, receiver) { - return that.nodeDataGetter(name.toString()); + const nodeName = name.toString(); + + if (that.workflow.getNode(nodeName) === null) { + throw new ExpressionError(`"${nodeName}" node doesn't exist`, { + runIndex: that.runIndex, + itemIndex: that.itemIndex, + failExecution: true, + }); + } + + if ( + nodeName !== that.activeNodeName && + !that.runExecutionData?.resultData.runData?.hasOwnProperty(nodeName) + ) { + throw new ExpressionError(`no data, execute "${nodeName}" node first`, { + runIndex: that.runIndex, + itemIndex: that.itemIndex, + failExecution: true, + }); + } + + if ( + nodeName !== that.activeNodeName && + !that.workflow.getNodeConnectionIndexes(that.activeNodeName, nodeName, 'main') + ) { + throw new ExpressionError(`connect "${that.activeNodeName}" to "${nodeName}"`, { + runIndex: that.runIndex, + itemIndex: that.itemIndex, + failExecution: true, + }); + } + + return that.nodeDataGetter(nodeName); }, }, ); diff --git a/packages/workflow/test/Workflow.test.ts b/packages/workflow/test/Workflow.test.ts index 2f8fa012542a9..98c151988be38 100644 --- a/packages/workflow/test/Workflow.test.ts +++ b/packages/workflow/test/Workflow.test.ts @@ -1057,6 +1057,11 @@ describe('Workflow', () => { description: 'return resolved value when referencing another property with expression when a node has spaces (long "$node["{NODE}"].parameter" syntax)', input: { + 'Node 4 with spaces': { + parameters: { + value1: '', + }, + }, Node1: { parameters: { value1: 'valueNode1', @@ -1190,6 +1195,17 @@ describe('Workflow', () => { ], ], }, + 'Node 4 with spaces': { + main: [ + [ + { + node: 'Node2', + type: 'main', + index: 0, + }, + ], + ], + }, }; const workflow = new Workflow({ nodes, connections, active: false, nodeTypes }); @@ -1219,6 +1235,8 @@ describe('Workflow', () => { }, }, ], + Node2: [], + 'Node 4 with spaces': [], }, }, };