Skip to content

ClassCastException when evaluating the eq/ne operator in the EL [SPR-11708] #16330

@spring-projects-issues

Description

@spring-projects-issues

Oliver Becker opened SPR-11708 and commented

I suppose evaluating '=' or '!=' should never ever throw a ClassCastException. However, this happens with comparable operands of different types.

Example: 10 != 'ten' should evaluate to true IMO. But it doesn't. To check for yourself, add the following line to the corresponding test OperatorTests.testNotEqual()

evaluate("10 ne '10'", true, Boolean.class);

The cause is the call to compare(..) if the first operand is comparable.
See class org.springframework.expression.spel.ast.Operator, method equalityCheck. Below the number handling, in line 99 the code contains the following three lines

if (left != null && (left instanceof Comparable)) {
	return (state.getTypeComparator().compare(left, right) == 0);
}

Simply removing these lines would fix the bug. Moreover, there is no failing test case after doing so, I'm not sure what kind of use case has been in mind when adding these lines to the evaluation of equals.

Alternatively catching the exception would solve the problem, too. But if you do so then please add a test that shows the necessity for using the compare logic.

My vote is for removing the cited code snippet.


Affects: 4.0.3

Issue Links:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions