Skip to content

Commit ad7cdc5

Browse files
committed
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 in a SpEL expression, ReflectionHelper currently throws a NullPointerException instead of leaving the null value unchanged. This commit fixes this regression. Closes gh-27719
1 parent 6cc9538 commit ad7cdc5

File tree

5 files changed

+99
-69
lines changed

5 files changed

+99
-69
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/support/ReflectionHelper.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,17 +290,18 @@ static boolean convertArguments(TypeConverter converter, Object[] arguments, Exe
290290
Object argument = arguments[varargsPosition];
291291
TypeDescriptor targetType = new TypeDescriptor(methodParam);
292292
TypeDescriptor sourceType = TypeDescriptor.forObject(argument);
293-
// If the argument type is equal to the varargs element type, there is no need
294-
// to convert it or wrap it in an array. For example, using StringToArrayConverter
295-
// to convert a String containing a comma would result in the String being split
296-
// and repackaged in an array when it should be used as-is.
297-
if (!sourceType.equals(targetType.getElementTypeDescriptor())) {
293+
// If the argument is null or the argument type is equal to the varargs element type,
294+
// there is no need to convert it or wrap it in an array. For example, using
295+
// StringToArrayConverter to convert a String containing a comma would result in the
296+
// String being split and repackaged in an array when it should be used as-is.
297+
if (argument != null && !sourceType.equals(targetType.getElementTypeDescriptor())) {
298298
arguments[varargsPosition] = converter.convertValue(argument, sourceType, targetType);
299299
}
300-
// Three outcomes of the above if-block:
301-
// 1) the input argument was correct type but not wrapped in an array, and nothing was done.
302-
// 2) the input argument was already compatible (i.e., array of valid type), and nothing was done.
303-
// 3) the input argument was the wrong type and got converted and wrapped in an array.
300+
// Possible outcomes of the above if-block:
301+
// 1) the input argument was null, and nothing was done.
302+
// 2) the input argument was correct type but not wrapped in an array, and nothing was done.
303+
// 3) the input argument was already compatible (i.e., array of valid type), and nothing was done.
304+
// 4) the input argument was the wrong type and got converted and wrapped in an array.
304305
if (argument != arguments[varargsPosition] &&
305306
!isFirstEntryInArray(argument, arguments[varargsPosition])) {
306307
conversionOccurred = true; // case 3

spring-expression/src/test/java/org/springframework/expression/spel/MethodInvocationTests.java

Lines changed: 42 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -234,26 +234,33 @@ public void testAddingMethodResolvers() {
234234

235235
@Test
236236
public void testVarargsInvocation01() {
237-
// Calling 'public int aVarargsMethod(String... strings)' - returns number of arguments
238-
evaluate("aVarargsMethod('a','b','c')", 3, Integer.class);
239-
evaluate("aVarargsMethod('a')", 1, Integer.class);
240-
evaluate("aVarargsMethod()", 0, Integer.class);
241-
evaluate("aVarargsMethod(1,2,3)", 3, Integer.class); // all need converting to strings
242-
evaluate("aVarargsMethod(1)", 1, Integer.class); // needs string conversion
243-
evaluate("aVarargsMethod(1,'a',3.0d)", 3, Integer.class); // first and last need conversion
244-
evaluate("aVarargsMethod(new String[]{'a','b','c'})", 3, Integer.class);
237+
// Calling 'public String aVarargsMethod(String... strings)'
238+
evaluate("aVarargsMethod('a','b','c')", "[a, b, c]", String.class);
239+
evaluate("aVarargsMethod('a')", "[a]", String.class);
240+
evaluate("aVarargsMethod()", "[]", String.class);
241+
evaluate("aVarargsMethod(1,2,3)", "[1, 2, 3]", String.class); // all need converting to strings
242+
evaluate("aVarargsMethod(1)", "[1]", String.class); // needs string conversion
243+
evaluate("aVarargsMethod(1,'a',3.0d)", "[1, a, 3.0]", String.class); // first and last need conversion
244+
evaluate("aVarargsMethod(new String[]{'a','b','c'})", "[a, b, c]", String.class);
245+
evaluate("aVarargsMethod(new String[]{})", "[]", String.class);
246+
evaluate("aVarargsMethod(null)", "[null]", String.class);
247+
evaluate("aVarargsMethod(null,'a')", "[null, a]", String.class);
248+
evaluate("aVarargsMethod('a',null,'b')", "[a, null, b]", String.class);
245249
}
246250

247251
@Test
248252
public void testVarargsInvocation02() {
249-
// Calling 'public int aVarargsMethod2(int i, String... strings)' - returns int + length_of_strings
250-
evaluate("aVarargsMethod2(5,'a','b','c')", 8, Integer.class);
251-
evaluate("aVarargsMethod2(2,'a')", 3, Integer.class);
252-
evaluate("aVarargsMethod2(4)", 4, Integer.class);
253-
evaluate("aVarargsMethod2(8,2,3)", 10, Integer.class);
254-
evaluate("aVarargsMethod2(9)", 9, Integer.class);
255-
evaluate("aVarargsMethod2(2,'a',3.0d)", 4, Integer.class);
256-
evaluate("aVarargsMethod2(8,new String[]{'a','b','c'})", 11, Integer.class);
253+
// Calling 'public String aVarargsMethod2(int i, String... strings)'
254+
evaluate("aVarargsMethod2(5,'a','b','c')", "5-[a, b, c]", String.class);
255+
evaluate("aVarargsMethod2(2,'a')", "2-[a]", String.class);
256+
evaluate("aVarargsMethod2(4)", "4-[]", String.class);
257+
evaluate("aVarargsMethod2(8,2,3)", "8-[2, 3]", String.class);
258+
evaluate("aVarargsMethod2(2,'a',3.0d)", "2-[a, 3.0]", String.class);
259+
evaluate("aVarargsMethod2(8,new String[]{'a','b','c'})", "8-[a, b, c]", String.class);
260+
evaluate("aVarargsMethod2(8,new String[]{})", "8-[]", String.class);
261+
evaluate("aVarargsMethod2(8,null)", "8-[null]", String.class);
262+
evaluate("aVarargsMethod2(8,null,'a')", "8-[null, a]", String.class);
263+
evaluate("aVarargsMethod2(8,'a',null,'b')", "8-[a, null, b]", String.class);
257264
}
258265

259266
@Test
@@ -284,6 +291,25 @@ public void testVarargsInvocation03() {
284291
evaluate("aVarargsMethod3('foo', 'bar,baz')", "foo-bar,baz", String.class);
285292
}
286293

294+
@Test
295+
public void testVarargsOptionalInvocation() {
296+
// Calling 'public String optionalVarargsMethod(Optional<String>... values)'
297+
evaluate("optionalVarargsMethod()", "[]", String.class);
298+
evaluate("optionalVarargsMethod(new String[0])", "[]", String.class);
299+
evaluate("optionalVarargsMethod('a')", "[Optional[a]]", String.class);
300+
evaluate("optionalVarargsMethod('a','b','c')", "[Optional[a], Optional[b], Optional[c]]", String.class);
301+
evaluate("optionalVarargsMethod(9)", "[Optional[9]]", String.class);
302+
evaluate("optionalVarargsMethod(2,3)", "[Optional[2], Optional[3]]", String.class);
303+
evaluate("optionalVarargsMethod('a',3.0d)", "[Optional[a], Optional[3.0]]", String.class);
304+
evaluate("optionalVarargsMethod(new String[]{'a','b','c'})", "[Optional[a], Optional[b], Optional[c]]", String.class);
305+
// The following should actually evaluate to [Optional.empty] instead of [null],
306+
// but ReflectionHelper.convertArguments() currently does not provide explicit
307+
// Optional support for a single argument passed to a varargs array.
308+
evaluate("optionalVarargsMethod(null)", "[null]", String.class);
309+
evaluate("optionalVarargsMethod(null,'a')", "[Optional.empty, Optional[a]]", String.class);
310+
evaluate("optionalVarargsMethod('a',null,'b')", "[Optional[a], Optional.empty, Optional[b]]", String.class);
311+
}
312+
287313
@Test
288314
public void testInvocationOnNullContextObject() {
289315
evaluateAndCheckError("null.toString()",SpelMessage.METHOD_CALL_ON_NULL_OBJECT_NOT_ALLOWED);

spring-expression/src/test/java/org/springframework/expression/spel/TestScenarioCreator.java

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.expression.spel;
1818

19+
import java.util.Arrays;
1920
import java.util.GregorianCalendar;
2021

2122
import org.springframework.expression.spel.support.StandardEvaluationContext;
@@ -51,10 +52,10 @@ private static void populateFunctions(StandardEvaluationContext testContext) {
5152
TestScenarioCreator.class.getDeclaredMethod("reverseInt", Integer.TYPE, Integer.TYPE, Integer.TYPE));
5253
testContext.registerFunction("reverseString",
5354
TestScenarioCreator.class.getDeclaredMethod("reverseString", String.class));
54-
testContext.registerFunction("varargsFunctionReverseStringsAndMerge",
55-
TestScenarioCreator.class.getDeclaredMethod("varargsFunctionReverseStringsAndMerge", String[].class));
56-
testContext.registerFunction("varargsFunctionReverseStringsAndMerge2",
57-
TestScenarioCreator.class.getDeclaredMethod("varargsFunctionReverseStringsAndMerge2", Integer.TYPE, String[].class));
55+
testContext.registerFunction("varargsFunction",
56+
TestScenarioCreator.class.getDeclaredMethod("varargsFunction", String[].class));
57+
testContext.registerFunction("varargsFunction2",
58+
TestScenarioCreator.class.getDeclaredMethod("varargsFunction2", Integer.TYPE, String[].class));
5859
}
5960
catch (Exception ex) {
6061
throw new IllegalStateException(ex);
@@ -108,25 +109,12 @@ public static String reverseString(String input) {
108109
return backwards.toString();
109110
}
110111

111-
public static String varargsFunctionReverseStringsAndMerge(String... strings) {
112-
StringBuilder sb = new StringBuilder();
113-
if (strings != null) {
114-
for (int i = strings.length - 1; i >= 0; i--) {
115-
sb.append(strings[i]);
116-
}
117-
}
118-
return sb.toString();
112+
public static String varargsFunction(String... strings) {
113+
return Arrays.toString(strings);
119114
}
120115

121-
public static String varargsFunctionReverseStringsAndMerge2(int j, String... strings) {
122-
StringBuilder sb = new StringBuilder();
123-
sb.append(j);
124-
if (strings != null) {
125-
for (int i = strings.length - 1; i >= 0; i--) {
126-
sb.append(strings[i]);
127-
}
128-
}
129-
return sb.toString();
116+
public static String varargsFunction2(int i, String... strings) {
117+
return String.valueOf(i) + "-" + Arrays.toString(strings);
130118
}
131119

132120
}

spring-expression/src/test/java/org/springframework/expression/spel/VariableAndFunctionTests.java

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,33 @@ public void testFunctionAccess02() {
5959

6060
@Test
6161
public void testCallVarargsFunction() {
62-
evaluate("#varargsFunctionReverseStringsAndMerge('a,b')", "a,b", String.class);
63-
evaluate("#varargsFunctionReverseStringsAndMerge('a', 'b,c', 'd')", "db,ca", String.class);
64-
evaluate("#varargsFunctionReverseStringsAndMerge('a','b','c')", "cba", String.class);
65-
evaluate("#varargsFunctionReverseStringsAndMerge('a')", "a", String.class);
66-
evaluate("#varargsFunctionReverseStringsAndMerge()", "", String.class);
67-
evaluate("#varargsFunctionReverseStringsAndMerge('b',25)", "25b", String.class);
68-
evaluate("#varargsFunctionReverseStringsAndMerge(25)", "25", String.class);
62+
evaluate("#varargsFunction()", "[]", String.class);
63+
evaluate("#varargsFunction(new String[0])", "[]", String.class);
64+
evaluate("#varargsFunction('a')", "[a]", String.class);
65+
evaluate("#varargsFunction('a','b','c')", "[a, b, c]", String.class);
66+
// Conversion from int to String
67+
evaluate("#varargsFunction(25)", "[25]", String.class);
68+
evaluate("#varargsFunction('b',25)", "[b, 25]", String.class);
69+
// Strings that contain a comma
70+
evaluate("#varargsFunction('a,b')", "[a,b]", String.class);
71+
evaluate("#varargsFunction('a', 'x,y', 'd')", "[a, x,y, d]", String.class);
72+
// null values
73+
evaluate("#varargsFunction(null)", "[null]", String.class);
74+
evaluate("#varargsFunction('a',null,'b')", "[a, null, b]", String.class);
6975

70-
evaluate("#varargsFunctionReverseStringsAndMerge2(1, 'a,b')", "1a,b", String.class);
71-
evaluate("#varargsFunctionReverseStringsAndMerge2(1,'a','b','c')", "1cba", String.class);
72-
evaluate("#varargsFunctionReverseStringsAndMerge2(1, 'a', 'b,c', 'd')", "1db,ca", String.class);
73-
evaluate("#varargsFunctionReverseStringsAndMerge2(2,'a')", "2a", String.class);
74-
evaluate("#varargsFunctionReverseStringsAndMerge2(3)", "3", String.class);
75-
evaluate("#varargsFunctionReverseStringsAndMerge2(4,'b',25)", "425b", String.class);
76-
evaluate("#varargsFunctionReverseStringsAndMerge2(5,25)", "525", String.class);
76+
evaluate("#varargsFunction2(9)", "9-[]", String.class);
77+
evaluate("#varargsFunction2(9, new String[0])", "9-[]", String.class);
78+
evaluate("#varargsFunction2(9,'a')", "9-[a]", String.class);
79+
evaluate("#varargsFunction2(9,'a','b','c')", "9-[a, b, c]", String.class);
80+
// Conversion from int to String
81+
evaluate("#varargsFunction2(9,25)", "9-[25]", String.class);
82+
evaluate("#varargsFunction2(9,'b',25)", "9-[b, 25]", String.class);
83+
// Strings that contain a comma:
84+
evaluate("#varargsFunction2(9, 'a,b')", "9-[a,b]", String.class);
85+
evaluate("#varargsFunction2(9, 'a', 'x,y', 'd')", "9-[a, x,y, d]", String.class);
86+
// null values
87+
evaluate("#varargsFunction2(9,null)", "9-[null]", String.class);
88+
evaluate("#varargsFunction2(9,'a',null,'b')", "9-[a, null, b]", String.class);
7789
}
7890

7991
@Test

spring-expression/src/test/java/org/springframework/expression/spel/testresources/Inventor.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@
1717
package org.springframework.expression.spel.testresources;
1818

1919
import java.util.ArrayList;
20+
import java.util.Arrays;
2021
import java.util.Date;
2122
import java.util.HashMap;
2223
import java.util.LinkedHashMap;
2324
import java.util.List;
2425
import java.util.Map;
26+
import java.util.Optional;
2527

2628
import org.springframework.util.ObjectUtils;
2729

@@ -191,16 +193,17 @@ public String joinThreeStrings(String a, String b, String c) {
191193
return a + b + c;
192194
}
193195

194-
public int aVarargsMethod(String... strings) {
195-
if (strings == null)
196-
return 0;
197-
return strings.length;
196+
public String aVarargsMethod(String... strings) {
197+
return Arrays.toString(strings);
198198
}
199199

200-
public int aVarargsMethod2(int i, String... strings) {
201-
if (strings == null)
202-
return i;
203-
return strings.length + i;
200+
public String aVarargsMethod2(int i, String... strings) {
201+
return String.valueOf(i) + "-" + Arrays.toString(strings);
202+
}
203+
204+
@SuppressWarnings("unchecked")
205+
public String optionalVarargsMethod(Optional<String>... values) {
206+
return Arrays.toString(values);
204207
}
205208

206209
public String aVarargsMethod3(String str1, String... strings) {

0 commit comments

Comments
 (0)