Browse Source

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
pull/33365/head
Sam Brannen 1 year ago
parent
commit
c57c2272a1
  1. 2
      spring-expression/src/main/java/org/springframework/expression/spel/ast/Indexer.java
  2. 4
      spring-expression/src/test/java/org/springframework/expression/spel/PropertyAccessTests.java

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

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

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

@ -189,6 +189,10 @@ class PropertyAccessTests extends AbstractExpressionTests {
assertThatSpelEvaluationException() assertThatSpelEvaluationException()
.isThrownBy(() -> parser.parseExpression("name='p3'").getValue(context, target)) .isThrownBy(() -> parser.parseExpression("name='p3'").getValue(context, target))
.extracting(SpelEvaluationException::getMessageCode).isEqualTo(SpelMessage.PROPERTY_OR_FIELD_NOT_WRITABLE); .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 @Test

Loading…
Cancel
Save