-
Notifications
You must be signed in to change notification settings - Fork 38.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SpEL vararg method invocation fails if string literal contains a comma #27582
Comments
Hi @bmoraes-axur, Thanks for creating your first issue for the Spring Framework! 👍 We'll investigate. Related issues:
|
I have confirmed that this is a bug which applies to method invocations in general (not just to custom functions). |
hello, I have find it is because if just two args, the second args will have another logic |
first into org.springframework.expression.spel.ast.FunctionReference#executeFunctionJLRMethod.
|
but I have no idea to fix the problem, in the doc say it is a feature if just one argument should convert |
Thanks for sharing your thoughts, @xixingya. I already had a fix in place and pushed it to |
@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. |
Will do, thank you very much! |
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 in a SpEL expression, ReflectionHelper currently throws a NullPointerException instead of leaving the null value unchanged. This commit fixes this regression. Closes gh-27719
Warning: I had a regression on my web app I think due to this correction. I think it's correct but some persons like me can be impacted with a minor evolution of spring (12 -> 13). In Spring Security, I was using |
That's a good point. If you were relying on this bug, you'll need to switch to the proper syntax for multiple vararg values.
That is indeed the correct way to use |
we encountered the same regression when upgrading from earlier versions. We use a property to store the list of authorized roles or groups, and now due to this change, entire list is treated as one role -- and "hasAnyAuthority" is failing because of that. @PreAuthorize("hasAnyAuthority(@propertyResolver.getProperty('admin.authority'))") Because a property is a string, this regression only impacts varargs uses like some workaround suggested from @yabqiu: split the array in the expression, or add another wrapper to help infer the correct type. @SpringBootApplication
public class Main implements CommandLineRunner {
static {
System.setProperty("property1", "{'a','b'}");
}
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
public static int setProperty(String... args) {
return args.length;
}
public static String[] identity(String[] arr) {
return arr;
}
@Autowired
public void printLength(@Value("#{@main.setProperty(systemProperties['property1'])}") int value) {
System.out.println("property length: " + value);
}
@Autowired
public void printLengthSplit(@Value("#{T(test.Main).setProperty(systemProperties['property1'].split(','))}") int value) {
System.out.println("length with split: " + value);
}
@Autowired
public void printLengthId(@Value("#{T(test.Main).setProperty(T(test.Main).identity(systemProperties['property1'] ))}") int value) {
System.out.println("length with type cast: " + value);
}
@Override
public void run(String... args) throws Exception {
}
} |
@sbrannen Still facing the issue when the method is |
I've been encountering some issues that seem to be related to this. It seems to me that the behavior to handle vararg parameters is inconsistent. To explain, below are a couple of Kotlin functions that I used to test: object Functions {
@JvmStatic
fun formatAny(format: String, vararg args: Any?): String? {
if (args.any { it == null }) return null
return format.format(*args)
}
@JvmStatic
fun formatString(format: String, vararg args: String?): String? {
if (args.any { it == null }) return null
return format.format(*args)
}
} Both functions accept vararg parameters. The only difference between the two is that the first function can work with non-string objects (which is normally what people would want for the string formatting use cases). I did a few tests with the latest version that I can find in maven, which is Test 1: single
Test 2: single empty string
Test 3: single string with comma
Test 4: double string with comma
My conclusion is that:
|
Hi @manoharank and @weiw005, Please note that this issue was closed almost three years ago. If you are still encountering issues against supported versions of Spring Framework, please create a new GitHub issue and provide a sample application that reproduces the issue which we can run ourselves -- for example, a public Git repository or a ZIP-file attached to the issue. Thanks |
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:
This is one of the registered functions:
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:
The text was updated successfully, but these errors were encountered: