Skip to content

Commit

Permalink
Improved: Update end bracket detection en groovy scriplet (OFBIZ-13178)
Browse files Browse the repository at this point in the history
When you use double brackets on groovy scriptlet, the end bracket isn't correctly manage.

The follow simple code failed :

${groovy: if (true) {return 0}}
  • Loading branch information
nmalin committed Nov 19, 2024
1 parent 4851fd6 commit 6393e21
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -309,17 +309,11 @@ protected static FlexibleStringExpander[] getStrElems(char[] chars, int offset,
}
if (expression.indexOf("groovy:", start + 2) == start + 2 && !escapedExpression) {
// checks to see if this starts with a "groovy:", if so treat the rest of the expression as a groovy scriptlet
end = getMatchingClosingBracket(expression, start, origLen, end);
strElems.add(new ScriptElem(chars, start, Math.min(end + 1, start + length) - start, start + 9, end - start - 9));
} else {
// Scan for matching closing bracket
int ptr = expression.indexOf("{", start + 2);
while (ptr != -1 && end != -1 && ptr < end) {
end = expression.indexOf(CLOSE_BRACKET, end + 1);
ptr = expression.indexOf("{", ptr + 1);
}
if (end == -1) {
end = origLen;
}
end = getMatchingClosingBracket(expression, start, origLen, end);
// Evaluation sequence is important - do not change it
if (escapedExpression) {
strElems.add(new ConstOffsetElem(chars, start, end + 1 - start));
Expand Down Expand Up @@ -352,6 +346,18 @@ protected static FlexibleStringExpander[] getStrElems(char[] chars, int offset,
return strElems.toArray(new FlexibleStringExpander[strElems.size()]);
}

private static int getMatchingClosingBracket(String expression, int start, int origLen, int end) {
int ptr = expression.indexOf("{", start + 2);
while (ptr != -1 && end != -1 && ptr < end) {
end = expression.indexOf(CLOSE_BRACKET, end + 1);
ptr = expression.indexOf("{", ptr + 1);
}
if (end == -1) {
end = origLen;
}
return end;
}

// Note: a character array is used instead of a String to keep the memory footprint small.
private final char[] chars;
private int hint = 20;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,8 +316,10 @@ public void testEverything() {
fseTest("UEL integration: missing", "${noList[0]}", testMap, null, null, "", null, false);
fseTest("Escaped expression", "This is an \\${escaped} expression", testMap, "This is an ${escaped} expression", false);
fseTest("Escaped(groovy) expression", "This is an \\${groovy:escaped} expression", testMap, "This is an ${groovy:escaped} expression", false);
fseTest("Bracket en groovy", "This is a groovy ${groovy: if (true) {return 'bracket'}} expression", testMap, "This is a groovy bracket expression", false);
fseTest("Bracket en groovy again", "This is a groovy ${groovy: if (true) {if (true) {return 'with 2 brackets'}}} expression", testMap, "This is a groovy with 2 brackets expression", false);

// TODO: Find a better way to setup or handle the big decimal value. If new ones are not instanciated in the test
// TODO: Find a better way to setup or handle the big decimal value. If new ones are not instantiated in the test
// it fails because of the comparison between object pointers..
fseTest("nested UEL integration(return BigDecimal)", "${a${'moun'}t}", testMap, null, LOCALE_TO_TEST,
"1,234,567.89", new BigDecimal("1234567.89"), false);
Expand Down

0 comments on commit 6393e21

Please sign in to comment.