Introduce null-safe index operator in SpEL
See gh-29847
This commit is contained in:
committed by
Sam Brannen
parent
2a1abb5553
commit
9f4d46fe33
@@ -376,6 +376,20 @@ class IndexingTests {
|
||||
assertThat(expression.getValue(this, String.class)).isEqualTo("apple");
|
||||
}
|
||||
|
||||
@Test
|
||||
void nullSafeIndex() {
|
||||
ContextWithNullCollections testContext = new ContextWithNullCollections();
|
||||
StandardEvaluationContext context = new StandardEvaluationContext(testContext);
|
||||
Expression expr = new SpelExpressionParser().parseRaw("nullList?.[4]");
|
||||
assertThat(expr.getValue(context)).isNull();
|
||||
|
||||
expr = new SpelExpressionParser().parseRaw("nullArray?.[4]");
|
||||
assertThat(expr.getValue(context)).isNull();
|
||||
|
||||
expr = new SpelExpressionParser().parseRaw("nullMap:?.[4]");
|
||||
assertThat(expr.getValue(context)).isNull();
|
||||
}
|
||||
|
||||
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
@@ -436,4 +450,11 @@ class IndexingTests {
|
||||
|
||||
}
|
||||
|
||||
|
||||
static class ContextWithNullCollections {
|
||||
public List nullList = null;
|
||||
public String[] nullArray = null;
|
||||
public Map nullMap = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user