Skip to content

Commit 7e39078

Browse files
committed
Throw exception for failure to set property as index in SpEL
Prior to this commit, the Indexer in the Spring Expression Language (SpEL) silently ignored a failure to set a property via the indexed property syntax (['<property name>'] = <new value>) – for example, if property write access was disabled in the EvaluationContext. This commit addresses this issue by properly throwing a SpelEvaluationException in PropertyIndexingValueRef.setValue(Object) if the property could not be set. See gh-33310 Closes gh-33312 (cherry picked from commit c57c227)
1 parent d5c5c91 commit 7e39078

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,8 @@ public void setValue(@Nullable Object newValue) {
634634
throw new SpelEvaluationException(getStartPosition(), ex,
635635
SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE, this.name, ex.getMessage());
636636
}
637+
throw new SpelEvaluationException(getStartPosition(),
638+
SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, this.targetObjectTypeDescriptor.toString());
637639
}
638640

639641
@Override

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ void propertyReadOnly() {
189189
assertThatSpelEvaluationException()
190190
.isThrownBy(() -> parser.parseExpression("name='p3'").getValue(context, target))
191191
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE);
192+
193+
assertThatSpelEvaluationException()
194+
.isThrownBy(() -> parser.parseExpression("['name']='p4'").getValue(context, target))
195+
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE);
192196
}
193197

194198
@Test

0 commit comments

Comments
 (0)