Skip to content

Commit 2165b16

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-33311 (cherry picked from commit c57c227)
1 parent bf72e92 commit 2165b16

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)