spring-framework: SpEL vararg method invocation fails if string literal contains a comma
Affects: 4.1.2 and up
I’m trying to use my own matching functions with spring-expression, but I came across a possible bug.
Here’s some code for context:
import org.springframework.expression.spel.support.StandardEvaluationContext;
public class RuleEvaluationContext extends StandardEvaluationContext {
public RuleEvaluationContext(Object rootObject) {
super(rootObject);
registerFunctions();
}
public RuleEvaluationContext() {
super();
registerFunctions();
}
private void registerFunctions() {
for (Functions fun : Functions.values()) {
this.registerFunction(fun.funcName(), fun.method());
}
}
}
enum Functions {
/* Functions commented for the sake of simplicity
CONTAINS(Contains.FUNC_NAME,
getDeclaredMethod(Contains.class, Contains.FUNC_NAME, String.class, String[].class)),
STARTS_WITH(StartsWith.FUNC_NAME,
getDeclaredMethod(StartsWith.class, StartsWith.FUNC_NAME, String.class, String[].class)),
*/
MATCHES(Matches.FUNC_NAME,
getDeclaredMethod(Matches.class, Matches.FUNC_NAME, String.class, String[].class)),
/*
EQUALS(Equals.FUNC_NAME,
getDeclaredMethod(Equals.class, Equals.FUNC_NAME, String.class, String.class)),
GREATER(GreaterThan.FUNC_NAME,
getDeclaredMethod(GreaterThan.class, GreaterThan.FUNC_NAME, String.class, String.class)),
LESS_THAN(LessThan.FUNC_NAME,
getDeclaredMethod(LessThan.class, LessThan.FUNC_NAME, String.class, String.class));
*/
private final String funcName;
private final Method method;
Functions(String funcName, Method method) {
this.funcName = funcName;
this.method = method;
}
public String funcName() {
return funcName;
}
public Method method() {
return method;
}
private static Method getDeclaredMethod(Class c, String name, Class<?>... parameterTypes) {
try {
return c.getDeclaredMethod(name, parameterTypes);
} catch (NoSuchMethodException e) {
throw new FunctionsException(e);
}
}
}
This is one of the registered functions:
public class Matches {
public static final String FUNC_NAME = "matches";
private Matches() {
super();
}
public static boolean matches(String text, String... regexps) {
if (text != null) {
for (String regex : regexps) {
Pattern pattern = Pattern.compile(regex, Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE);
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
return true;
}
}
}
return false;
}
}
@Test
public void test() {
final RuleEvaluationContext context = new RuleEvaluationContext(new FakeObject());
String originalRule = "#matches(prop, 'xyz,xyz')";
Expression expression = parser.parseExpression(originalRule);
Boolean result = expression.getValue(context, Boolean.class);
assertThat(result, is(false));
}
private final ExpressionParser parser = new SpelExpressionParser();
private static class FakeObject {
private String prop = "xyz";
public String getProp() {
return prop;
}
}
This test fails when it should pass. Upon further inspection I’ve found that the problem is that spring-expression breaks the ‘xyz,xyz’ into [“xyz”, “xyz”] as can be seen below.

However I’ve also found that using old versions of the library (pre 4.1.2), this doesn’t happen.

As does putting more strings inside the argument:
@Test
public void test() {
final RuleEvaluationContext context = new RuleEvaluationContext(new FakeObject());
String originalRule = "#matches(prop, 'abc', 'xyz,xyz')";
Expression expression = parser.parseExpression(originalRule);
Boolean result = expression.getValue(context, Boolean.class);
assertThat(result, is(false));
}

About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 15 (9 by maintainers)
Commits related to this issue
- Fix SpEL vararg method invocation for strings containing commas Prior to this commit, if a SpEL expression invoked a method or registered function that declares a String varargs argument, there were ... — committed to sbrannen/spring-framework by sbrannen 5 years ago
- Add explicit tests for SpEL functions for fix for gh-27582 — committed to sbrannen/spring-framework by sbrannen 5 years ago
- Fix regression for null varargs in SpEL expressions A regression was introduced in gh-27582. Specifically, when null is supplied as the single argument for a varargs parameter in a method or function... — committed to spring-projects/spring-framework by sbrannen 5 years ago
I have confirmed that this is a bug which applies to method invocations in general (not just to custom functions).
@sbrannen Sounds good. I have created https://github.com/spring-projects/spring-framework/issues/33013
@sbrannen Still facing the issue when the method is
print(Object... args)and invokingprint(name)and name has comma. No issue when invokingprint(name, age)(more than one arguments).@bmoraes-axur, feel free to try this out in the next 5.3.13 snapshot and let us know if you run into any issues.
Hi @bmoraes-axur,
Thanks for creating your first issue for the Spring Framework! 👍
We’ll investigate.
Related issues: