From 09070296c2267085b53e235edf7389d9278f054e Mon Sep 17 00:00:00 2001 From: Abdul Ahad Date: Tue, 10 Dec 2024 15:32:43 +0500 Subject: [PATCH] fix: missing context script task (#41) * fix: missing context script task Closes #4614 --- lib/zeebe/VariableResolver.js | 10 ++-- lib/zeebe/util/feelUtility.js | 52 ++++++++++++++++--- test/fixtures/zeebe/mappings/script-task.bpmn | 17 ++++++ test/spec/zeebe/Mappings.spec.js | 30 +++++++++++ 4 files changed, 96 insertions(+), 13 deletions(-) create mode 100644 test/fixtures/zeebe/mappings/script-task.bpmn diff --git a/lib/zeebe/VariableResolver.js b/lib/zeebe/VariableResolver.js index c8cc1ae..996c9fe 100644 --- a/lib/zeebe/VariableResolver.js +++ b/lib/zeebe/VariableResolver.js @@ -1,6 +1,6 @@ import { getProcessVariables } from '@bpmn-io/extract-process-variables/zeebe'; import { BaseVariableResolver } from '../base/VariableResolver'; -import { parseIoMappings } from './util/feelUtility'; +import { parseVariables } from './util/feelUtility'; import { getBusinessObject, is @@ -18,7 +18,7 @@ export default class ZeebeVariableResolver extends BaseVariableResolver { super(eventBus, bpmnjs); this._baseExtractor = getProcessVariables; - eventBus.on('variableResolver.parseVariables', HIGH_PRIORITY, this._resolveIoMappings); + eventBus.on('variableResolver.parseVariables', HIGH_PRIORITY, this._resolveVariables); } async getVariablesForElement(element, moddleElement) { @@ -94,21 +94,21 @@ export default class ZeebeVariableResolver extends BaseVariableResolver { } /** - * Parsed the variables that have io-mappings and resolves the variable schema to kept the + * Parsed the variables and resolves the variable schema to kept the * variable schema throughout the process. * * @param {Event} e * @param {Object} context * @param {Array} context.variables */ - _resolveIoMappings(e, context) { + _resolveVariables(e, context) { const rawVariables = context.variables; const mappedVariables = {}; for (const key in rawVariables) { const variables = rawVariables[key]; - const newVariables = parseIoMappings(variables); + const newVariables = parseVariables(variables); mappedVariables[key] = [ ...variables, ...newVariables ]; } diff --git a/lib/zeebe/util/feelUtility.js b/lib/zeebe/util/feelUtility.js index 75f4096..f140dc2 100644 --- a/lib/zeebe/util/feelUtility.js +++ b/lib/zeebe/util/feelUtility.js @@ -7,13 +7,12 @@ import { EntriesContext } from './VariableContext'; import { getExtensionElementsList } from '../../base/util/ExtensionElementsUtil'; import { getParents } from '../../base/util/scopeUtil'; - -export function parseIoMappings(variables) { +export function parseVariables(variables) { const variablesToResolve = []; - // Step 1 - Parse all io mappings and populate all that don't have references - // to other variables io-mappings + // Step 1 - Parse all variables and populate all that don't have references + // to other variables variables.forEach(variable => { variable.origin.forEach(origin => { const expressionDetails = getExpressionDetails(variable, origin); @@ -159,6 +158,28 @@ export function getResultContext(expression, variables = {}) { * @returns {{ expression: String, unresolved: Array }}} */ function getExpressionDetails(variable, origin) { + const expression = getIoExpression(variable, origin) || getScriptExpression(variable, origin); + + if (!expression) { + return; + } + + const result = getResultContext(expression); + + const unresolved = findUnresolvedVariables(result) ; + + return { expression, unresolved }; +} + +/** + * Given a Variable and a specific origin, return the mapping expression for all + * input outputs mapping. Returns undefined if no mapping exists for the given origin. + * + * @param {ProcessVariable} variable + * @param {djs.model.Base} origin + * @returns { expression: String} + */ +function getIoExpression(variable, origin) { const ioMapping = getExtensionElementsList(origin, 'zeebe:IoMapping')[0]; if (!ioMapping) { @@ -182,13 +203,28 @@ function getExpressionDetails(variable, origin) { return; } - const expression = mapping.source.substring(1); + return mapping.source.substring(1); - const result = getResultContext(expression); +} - const unresolved = findUnresolvedVariables(result) ; +/** + * Given a Variable and a specific origin, return the mapping expression for script + * task result variable. Returns undefined if no mapping exists for the given origin. + * + * @param {ProcessVariable} variable + * @param {djs.model.Base} origin + * @returns { expression: String} + */ +function getScriptExpression(variable, origin) { + const script = getExtensionElementsList(origin, 'zeebe:Script')[0]; - return { expression, unresolved }; + if (!script) { + return; + } + + if (script.resultVariable === variable.name) { + return script.expression.substring(1); + } } /** diff --git a/test/fixtures/zeebe/mappings/script-task.bpmn b/test/fixtures/zeebe/mappings/script-task.bpmn new file mode 100644 index 0000000..a5cc86a --- /dev/null +++ b/test/fixtures/zeebe/mappings/script-task.bpmn @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test/spec/zeebe/Mappings.spec.js b/test/spec/zeebe/Mappings.spec.js index 18dff03..475f89e 100644 --- a/test/spec/zeebe/Mappings.spec.js +++ b/test/spec/zeebe/Mappings.spec.js @@ -12,6 +12,7 @@ import chainedMappingsXML from 'test/fixtures/zeebe/mappings/chained-mappings.bp import primitivesXML from 'test/fixtures/zeebe/mappings/primitives.bpmn'; import mergingXML from 'test/fixtures/zeebe/mappings/merging.bpmn'; import scopeXML from 'test/fixtures/zeebe/mappings/scope.bpmn'; +import scriptTaskXML from 'test/fixtures/zeebe/mappings/script-task.bpmn'; import VariableProvider from 'lib/VariableProvider'; @@ -316,6 +317,35 @@ describe('ZeebeVariableResolver - Variable Mappings', function() { }); + + describe('Script Task', function() { + + beforeEach(bootstrap(scriptTaskXML)); + + + it('should add type annotation for script tasks', inject(async function(variableResolver, elementRegistry) { + + // given + const root = elementRegistry.get('Process_1'); + + // when + const variables = await variableResolver.getVariablesForElement(root.businessObject); + + // then + expect(variables).to.variableEqual([ + { + name: 'scriptResult', + type: 'Context', + info: '', + entries: [ + { name: 'foo', type: 'Number', info: '123', entries: [] }, + ] + } + ]); + })); + + }); + }); // helpers //////////////////////