Skip to content

SpelExpressionParser throws IllegalStateException instead of ParseException for invalid expression #31097

@andre-aktivconsultancy

Description

@andre-aktivconsultancy

Environment Details
Spring Framework Version: 6.0.6
Spring Boot Version: 3.0.4
Java Version: 17

Description
According to the Spring documentation, invoking parseExpression on a SpelExpressionParser should only result in a ParseException when provided with an invalid expression. However, in practice, it throws an IllegalStateException with the message "No node".

Steps to Reproduce

  • Create a new SpelExpressionParser instance.
  • Attempt to parse an invalid expression, e.g., ")".
  • Observe the exception thrown.

Test Code

@Test
void parse_invalidExpression_shouldThrowParseException() {
    final var expressionParser = new SpelExpressionParser();
    final var invalidExpression = ")";
    
    final var thrown = catchThrowable(() -> expressionParser.parseExpression(invalidExpression));

    assertThat(thrown).isInstanceOf(ParseException.class);
}

Expected Behavior
The test should pass, with ParseException being thrown for the invalid expression.

Actual Behavior
The test fails with the following output:

java.lang.AssertionError: 
Expecting actual throwable to be an instance of:
  org.springframework.expression.ParseException
but was:
  java.lang.IllegalStateException: No node
	at org.springframework.util.Assert.state(Assert.java:76)
	at org.springframework.expression.spel.standard.InternalSpelExpressionParser.doParseExpression(InternalSpelExpressionParser.java:132)
	at org.springframework.expression.spel.standard.SpelExpressionParser.doParseExpression(SpelExpressionParser.java:61)

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: regressionA bug that is also a regression

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions