Skip to content

Commit

Permalink
fix(cloud-templates): cast default number and boolean numbers to feel
Browse files Browse the repository at this point in the history
  • Loading branch information
marstamm committed Sep 12, 2024
1 parent d60bbd2 commit 793fb59
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 21 deletions.
7 changes: 7 additions & 0 deletions src/cloud-element-templates/Helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getBusinessObject } from 'bpmn-js/lib/util/ModelUtil';
import { is, isAny } from 'bpmn-js/lib/util/ModelUtil';

import { v4 as uuid } from 'uuid';
import { isSpecialFeelProperty, toFeelExpression } from './util/feelUtil';

/**
* The BPMN 2.0 extension attribute name under
Expand Down Expand Up @@ -177,6 +178,12 @@ export function findZeebeSubscription(message) {

export function getDefaultValue(property) {

if (
isSpecialFeelProperty(property)
) {
return toFeelExpression(property.value, property.type);
}

if (property.value !== undefined) {
return property.value;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FeelNumberEntry, NumberFieldEntry } from '@bpmn-io/properties-panel';
import { useService } from 'bpmn-js-properties-panel';
import { isSpecialFeelProperty, propertyValidator, usePropertyAccessors } from './util';
import { propertyValidator, usePropertyAccessors } from './util';
import { isSpecialFeelProperty } from '../../../util/feelUtil';
import { PropertyDescription } from '../../../../components/PropertyDescription';
import { PropertyTooltip } from '../../components/PropertyTooltip';
import { useCallback } from '@bpmn-io/properties-panel/preact/hooks';
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { find, groupBy } from 'min-dash';
import { getPropertyValue, setPropertyValue, validateProperty } from '../../../util/propertyUtil';
import { isSpecialFeelProperty, toFeelExpression } from '../../../util/feelUtil';

import { useCallback, useState } from '@bpmn-io/properties-panel/preact/hooks';

export function usePropertyAccessors(bpmnFactory, commandStack, element, property) {
Expand Down Expand Up @@ -38,26 +40,6 @@ export function usePropertyAccessors(bpmnFactory, commandStack, element, propert
return [ get, set ];
}

export const isSpecialFeelProperty = (property) => {
return [ 'optional', 'static' ].includes(property.feel) && [ 'Boolean', 'Number' ].includes(property.type);
};

const toFeelExpression = (value, type) => {
if (typeof value === 'string' && value.startsWith('=')) {
return value;
}

if (type === 'Boolean') {
value = value === 'false' ? false : value;
return '=' + !!value;
}

if (typeof value === 'undefined') {
return value;
}

return '=' + value.toString();
};

const fromFeelExpression = (value, type) => {
if (typeof value === 'undefined') {
Expand Down
20 changes: 20 additions & 0 deletions src/cloud-element-templates/util/feelUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export const isSpecialFeelProperty = (property) => {
return [ 'optional', 'static' ].includes(property.feel) && [ 'Boolean', 'Number' ].includes(property.type);
};

export const toFeelExpression = (value, type) => {
if (typeof value === 'string' && value.startsWith('=')) {
return value;
}

if (type === 'Boolean') {
value = value === 'false' ? false : value;
return '=' + !!value;
}

if (typeof value === 'undefined') {
return value;
}

return '=' + value.toString();
};
Original file line number Diff line number Diff line change
Expand Up @@ -4265,6 +4265,51 @@ describe('cloud-element-templates/cmd - ChangeElementTemplateHandler', function(

});


describe('FEEL Boolean and Numbers', function() {

beforeEach(bootstrap(require('./casted-values.bpmn').default));

describe('Boolean', function() {

const template = require('./casted-values.json')[0];

it('should apply generated value (uuid)', inject(function(elementRegistry) {

// given
let task = elementRegistry.get('Task_1');

// when
task = changeTemplate(task, template);

// then
expect(getZeebeProperty(task, 'StaticBooleanProperty').value).to.eql('=true');
expect(getZeebeProperty(task, 'OptionalBooleanProperty').value).to.eql('=true');
}));

});

describe('Number', function() {

const template = require('./casted-values.json')[1];

it('should apply generated value (uuid)', inject(function(elementRegistry) {

// given
let task = elementRegistry.get('Task_1');

// when
task = changeTemplate(task, template);

// then
expect(getZeebeProperty(task, 'StaticNumberProperty').value).to.eql('=123');
expect(getZeebeProperty(task, 'OptionalNumberProperty').value).to.eql('=123');
}));

});

});

});


Expand Down
17 changes: 17 additions & 0 deletions test/spec/cloud-element-templates/cmd/casted-values.bpmn
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<bpmn:definitions xmlns:bpmn="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:dc="http://www.omg.org/spec/DD/20100524/DC" id="Definitions_039bbyr" targetNamespace="http://bpmn.io/schema/bpmn" exporter="Camunda Modeler" exporterVersion="4.4.0">
<bpmn:process id="Process_1" isExecutable="true">
<bpmn:serviceTask id="Task_1" />
<bpmn:intermediateThrowEvent id="Event_1" />
</bpmn:process>
<bpmndi:BPMNDiagram id="BPMNDiagram_1">
<bpmndi:BPMNPlane id="BPMNPlane_1" bpmnElement="Process_1">
<bpmndi:BPMNShape id="Task_1_di" bpmnElement="Task_1">
<dc:Bounds x="0" y="0" width="100" height="80" />
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="Event_0f371po_di" bpmnElement="Event_1">
<dc:Bounds x="92" y="172" width="36" height="36" />
</bpmndi:BPMNShape>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</bpmn:definitions>
62 changes: 62 additions & 0 deletions test/spec/cloud-element-templates/cmd/casted-values.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
[
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"name": "booleanField optional",
"id": "booleanField.feel.optional",
"appliesTo": [
"bpmn:ServiceTask"
],
"properties": [
{
"label": "DefaultStaticBooleanProperty",
"type": "Boolean",
"feel": "optional",
"value": true,
"binding": {
"type": "zeebe:property",
"name": "StaticBooleanProperty"
}
},
{
"label": "DefaultOptionalBooleanProperty",
"type": "Boolean",
"feel": "optional",
"value": true,
"binding": {
"type": "zeebe:property",
"name": "OptionalBooleanProperty"
}
}
]
},
{
"$schema": "https://unpkg.com/@camunda/zeebe-element-templates-json-schema/resources/schema.json",
"name": "FEEL optional",
"id": "numberField.feel.optional",
"appliesTo": [
"bpmn:ServiceTask"
],
"properties": [
{
"label": "DefaultStaticNumberProperty",
"type": "Number",
"feel": "optional",
"value": 123,
"binding": {
"type": "zeebe:property",
"name": "StaticNumberProperty"
}
},
{
"label": "DefaultOptionalNumberProperty",
"type": "Number",
"feel": "optional",
"value": 123,
"binding": {
"type": "zeebe:property",
"name": "OptionalNumberProperty"
}
}
]
}
]

0 comments on commit 793fb59

Please sign in to comment.