Skip to content

A platform for community discussion. Free, open, simple.

FIX: Evaluate workflow expressions containing braces as whole values

Régis Hanol324afee65b54
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/condition-builder.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/condition-builder.gjs
index 12285bf81f7..9abe7c77a59 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/condition-builder.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/condition-builder.gjs
@@ -14,7 +14,10 @@ import {
inputIndexForConnection,
outputIndexForConnection,
} from "../../../lib/workflows/expression-paths";
-import { isExpression } from "../../../lib/workflows/property-engine";
+import {
+ isExpression,
+ wholeExpressionBody,
+} from "../../../lib/workflows/property-engine";
import {
inputConnectionsForNode,
previousNodeForConnection,
@@ -75,10 +78,7 @@ function leafKey(fieldPath) {
}
path = String(path);
- const exprMatch = path.match(/^=\{\{\s*(.*?)\s*\}\}$/);
- if (exprMatch) {
- path = exprMatch[1];
- }
+ path = wholeExpressionBody(path) ?? path;
const bracketSegments = [
...path.matchAll(/\[((?:"(?:\\.|[^"\\])*")|(?:'(?:\\.|[^'\\])*'))\]/g),
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/expression-wrapper.gjs b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/expression-wrapper.gjs
index 3af21c39a54..0f232217a7f 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/expression-wrapper.gjs
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/components/workflows/configurators/expression-wrapper.gjs
@@ -14,6 +14,7 @@ import {
import {
fieldType,
isExpression,
+ wholeExpressionBody,
} from "../../../lib/workflows/property-engine";
import ExpressionInput from "./expression-input";
@@ -50,13 +51,6 @@ function schemaType(schema) {
return fieldType(schema);
}
-function wholeExpressionBody(value) {
- const expressionBody = value.slice(1).trim();
- const match = expressionBody.match(/^\{\{\s*([\s\S]*?)\s*\}\}$/);
-
- return match?.[1]?.trim() || null;
-}
-
function parseJsonLiteral(value) {
try {
return JSON.parse(value);
diff --git a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js
index a9ea213b141..438dc055667 100644
--- a/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js
+++ b/plugins/discourse-workflows/admin/assets/javascripts/admin/lib/workflows/property-engine.js
@@ -431,6 +431,49 @@ export function isExpression(value) {
return typeof value === "string" && value.startsWith("=");
}
+/**
+ * Returns the body of an expression value that is exactly one `{{ ... }}`,
+ * e.g. `$json.id` for `={{ $json.id }}`, and null otherwise.
+ */
+export function wholeExpressionBody(value) {
+ if (!isExpression(value)) {
+ return null;
+ }
+
+ const template = value.slice(1).trim();
+ if (!template.startsWith("{{")) {
+ return null;
+ }
+
+ return findMatchingClose(template, 2) === template.length - 2
+ ? template.slice(2, -2).trim() || null
+ : null;
+}
+
+/** Mirrors ExpressionResolver.find_matching_close on the server. */
+function findMatchingClose(template, start) {
+ let depth = 1;
+ let cursor = start;
+
+ while (cursor < template.length - 1) {
+ const pair = template.slice(cursor, cursor + 2);
+
+ if (pair === "{{") {
+ depth++;
+ cursor += 2;
+ } else if (pair === "}}") {
+ if (--depth === 0) {
+ return cursor;
+ }
+ cursor += 2;
+ } else {
+ cursor++;
+ }
+ }
+
+ return null;
+}
+
export function credentialTypesForSlot(slot = {}) {
return slot.credential_types || [slot.credential_type].filter(Boolean);
}
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/expression_resolver.rb b/plugins/discourse-workflows/lib/discourse_workflows/expression_resolver.rb
index d3a362107c9..f1fb578a119 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/expression_resolver.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/expression_resolver.rb
@@ -2,8 +2,6 @@
module DiscourseWorkflows
class ExpressionResolver
- WHOLE_EXPRESSION_REGEX = /\A\{\{\s*([^{}]*?)\s*\}\}\z/
-
def self.find_matching_close(template, start)
depth = 1
cursor = start
@@ -60,7 +58,7 @@ module DiscourseWorkflows
return value unless resolvable_string?(value)
expression_body = value[1..].strip
- whole_expression = expression_body.match(WHOLE_EXPRESSION_REGEX)&.captures&.first
+ whole_expression = whole_expression_body(expression_body)
whole_expression ? js_evaluator.evaluate(whole_expression) : render_template(expression_body)
end
@@ -180,6 +178,13 @@ module DiscourseWorkflows
end
end
+ def whole_expression_body(template)
+ return if !template.start_with?("{{")
+
+ close = self.class.find_matching_close(template, 2)
+ template[2...close].strip if close == template.length - 2
+ end
+
def resolvable_string?(value)
value.is_a?(String) && value.start_with?("=")
end
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/expression_resolver_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/expression_resolver_spec.rb
index 166c4fb3d2a..dbaad77668f 100644
--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/expression_resolver_spec.rb
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/expression_resolver_spec.rb
@@ -321,6 +321,25 @@ RSpec.describe DiscourseWorkflows::ExpressionResolver do
expect(resolver.resolve("={{ $trigger.tags }}")).to eq(%w[bug help])
end
+ it "evaluates a single whole expression only when its braces close at the end" do
+ {
+ "={{ { id: $trigger.topic_id } }}" => {
+ "id" => 42,
+ },
+ "={{ {a: {b: 1} } }}" => {
+ "a" => {
+ "b" => 1,
+ },
+ },
+ "={{ (() => { return 1 })() }}" => 1,
+ "= {{ $trigger.topic_id }} " => 42,
+ "={{ JSON.stringify({ a: { b: 1 } }) }}" => '{"a":{"b":1}}',
+ "={{ 1 }} x {{ 2 }}" => "1 x 2",
+ "={{ $trigger.topic_id" => "{{ $trigger.topic_id",
+ "={{ }}" => nil,
+ }.each { |value, expected| expect(resolver.resolve(value)).to eq(expected), value }
+ end
+
it "formats arrays when interpolating into a larger string" do
expect(resolver.resolve("=tags: {{ $trigger.tags }}")).to eq("tags: bug, help")
end
diff --git a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js
index c03b019af7e..0fc40301559 100644
--- a/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js
+++ b/plugins/discourse-workflows/test/javascripts/unit/lib/workflows-property-engine-test.js
@@ -21,6 +21,7 @@ import {
propertyPlaceholder,
propertySelectNoneKey,
pruneModeHiddenFields,
+ wholeExpressionBody,
} from "discourse/plugins/discourse-workflows/admin/lib/workflows/property-engine";
module("Unit | Utility | workflows property engine", function () {
@@ -544,4 +545,23 @@ module("Unit | Utility | workflows property engine", function () {
);
assert.strictEqual(normalizeCodeEditorValue("plain", "json"), "plain");
});
+
+ test("wholeExpressionBody", function (assert) {
+ [
+ ["={{ $json.id }}", "$json.id"],
+ ["={{ { val: 'x' } }}", "{ val: 'x' }"],
+ ["={{ {a: {b: 1} } }}", "{a: {b: 1} }"],
+ ["={{ `${x}` }}", "`${x}`"],
+ ["={{ (() => { return 1 })() }}", "(() => { return 1 })()"],
+ ["= {{ a }} ", "a"],
+ ["={{ a }} x {{ b }}", null],
+ ["=a {{ b }} c {{ d }}", null],
+ ["={{ a", null],
+ ["={{ }}", null],
+ ["{{ a }}", null],
+ [42, null],
+ ].forEach(([input, expected]) => {
+ assert.strictEqual(wholeExpressionBody(input), expected, String(input));
+ });
+ });
});