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 c57c2272a1)
This commit is contained in:
Sam Brannen
2024-08-05 14:15:55 +03:00
parent bf72e92145
commit 2165b1651b
2 changed files with 6 additions and 0 deletions

View File

@@ -630,6 +630,8 @@ public class Indexer extends SpelNodeImpl {
throw new SpelEvaluationException(getStartPosition(), ex,
SpelMessage.EXCEPTION_DURING_PROPERTY_WRITE, this.name, ex.getMessage());
}
throw new SpelEvaluationException(getStartPosition(),
SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE, this.targetObjectTypeDescriptor.toString());
}
@Override

View File

@@ -189,6 +189,10 @@ public class PropertyAccessTests extends AbstractExpressionTests {
assertThatSpelEvaluationException()
.isThrownBy(() -> parser.parseExpression("name='p3'").getValue(context, target))
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE);
assertThatSpelEvaluationException()
.isThrownBy(() -> parser.parseExpression("['name']='p4'").getValue(context, target))
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.INDEXING_NOT_SUPPORTED_FOR_TYPE);
}
@Test