Introduce null-safe index operator in SpEL

See gh-29847
This commit is contained in:
Grigory Stepanov
2023-01-18 20:14:06 +03:00
committed by Sam Brannen
parent 2a1abb5553
commit 9f4d46fe33
3 changed files with 37 additions and 11 deletions

View File

@@ -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;
}
}