Skip to content

Commit c57c227

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. Closes gh-33310
1 parent 8ec23a0 commit c57c227

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

+2
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,8 @@ public void setValue(@Nullable Object newValue) {
630630
throw new SpelEvaluationException(getStartPosition(), ex,
631631
SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE, this.name, ex.getMessage());
632632
}
633+
throw new SpelEvaluationException(getStartPosition(),
634+
SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, this.targetObjectTypeDescriptor.toString());
633635
}
634636

635637
@Override

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

+4
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)