-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Description
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:
- SpEL: OpEQ should use equals() [SPR-9194] #13832 SpEL: OpEQ should use equals()