Skip to content

Commit bf7e64f

Browse files
committed
Replace Stream usage with iteration to avoid non-null requirements.
SpelEvaluator now iterates over the parameter map instead of using the Java 8 Stream API. Previously, expressions resulting in a null value failed in the collector as Java 8 streams require non-null values for map values. Closes #2904
1 parent 3145a09 commit bf7e64f

File tree

3 files changed

+70
-11
lines changed

3 files changed

+70
-11
lines changed

src/main/java/org/springframework/data/repository/query/SpelEvaluator.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515
*/
1616
package org.springframework.data.repository.query;
1717

18+
import java.util.LinkedHashMap;
1819
import java.util.Map;
19-
import java.util.Map.Entry;
20-
import java.util.stream.Collectors;
2120

2221
import org.springframework.data.repository.query.SpelQueryContext.SpelExtractor;
2322
import org.springframework.data.spel.ExpressionDependencies;
@@ -62,11 +61,12 @@ public Map<String, Object> evaluate(Object[] values) {
6261

6362
Assert.notNull(values, "Values must not be null.");
6463

64+
Map<String, String> parameterMap = extractor.getParameterMap();
65+
Map<String, Object> results = new LinkedHashMap<>(parameterMap.size());
6566

66-
return extractor.getParameters().collect(Collectors.toMap(//
67-
Entry::getKey, //
68-
it -> getSpElValue(it.getValue(), values) //
69-
));
67+
parameterMap.forEach((parameter, expression) -> results.put(parameter, getSpElValue(expression, values)));
68+
69+
return results;
7070
}
7171

7272
/**

src/main/java/org/springframework/data/repository/query/SpelQueryContext.java

-5
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
import java.util.HashMap;
2323
import java.util.List;
2424
import java.util.Map;
25-
import java.util.Map.Entry;
2625
import java.util.function.BiFunction;
2726
import java.util.regex.Matcher;
2827
import java.util.regex.Pattern;
29-
import java.util.stream.Stream;
3028

3129
import org.springframework.data.domain.Range;
3230
import org.springframework.data.domain.Range.Bound;
@@ -288,9 +286,6 @@ Map<String, String> getParameterMap() {
288286
return expressions;
289287
}
290288

291-
Stream<Entry<String, String>> getParameters() {
292-
return expressions.entrySet().stream();
293-
}
294289
}
295290

296291
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2023 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.repository.query;
17+
18+
import static org.assertj.core.api.Assertions.*;
19+
20+
import java.lang.reflect.Method;
21+
22+
import org.junit.jupiter.api.Test;
23+
import org.springframework.data.repository.query.SpelQueryContext.SpelExtractor;
24+
25+
/**
26+
* Unit tests for {@link SpelEvaluator}.
27+
*
28+
* @author Mark Paluch
29+
*/
30+
class SpelEvaluatorUnitTests {
31+
32+
final SpelQueryContext context = SpelQueryContext.of((counter, s) -> String.format("__$synthetic$__%d", counter + 1),
33+
String::concat);
34+
35+
@Test // GH-2904
36+
void shouldEvaluateExpression() throws Exception {
37+
38+
SpelExtractor extractor = context.parse("SELECT :#{#value}");
39+
Method method = MyRepository.class.getDeclaredMethod("simpleExpression", String.class);
40+
SpelEvaluator evaluator = new SpelEvaluator(QueryMethodEvaluationContextProvider.DEFAULT,
41+
new DefaultParameters(method), extractor);
42+
43+
assertThat(evaluator.getQueryString()).isEqualTo("SELECT :__$synthetic$__1");
44+
assertThat(evaluator.evaluate(new Object[] { "hello" })).containsEntry("__$synthetic$__1", "hello");
45+
}
46+
47+
@Test // GH-2904
48+
void shouldAllowNullValues() throws Exception {
49+
50+
SpelExtractor extractor = context.parse("SELECT :#{#value}");
51+
Method method = MyRepository.class.getDeclaredMethod("simpleExpression", String.class);
52+
SpelEvaluator evaluator = new SpelEvaluator(QueryMethodEvaluationContextProvider.DEFAULT,
53+
new DefaultParameters(method), extractor);
54+
55+
assertThat(evaluator.getQueryString()).isEqualTo("SELECT :__$synthetic$__1");
56+
assertThat(evaluator.evaluate(new Object[] { null })).containsEntry("__$synthetic$__1", null);
57+
}
58+
59+
interface MyRepository {
60+
61+
void simpleExpression(String value);
62+
63+
}
64+
}

0 commit comments

Comments
 (0)