Reputation: 51
I'm trying to upgrade spring version from 3.0.5 to 3.2.11.
I'm getting into troubles with SpEL when expression compares null value like this:
new SpelExpressionParser().parseExpression("null < 7").getValue();
Result of above code results in
Reason of this different behavior is that in StandartTypeComparator class, which is internally used in SpEL, there are different implementation of compare method:
version 3.0.5
public int compare(Object left, Object right) throws SpelEvaluationException {
// If one is null, check if the other is
if (left == null) {
return right == null ? 0 : 1;
} else if (right == null) {
return -1; // left cannot be null
}
version 3.2.11
public int compare(Object left, Object right) throws SpelEvaluationException {
// If one is null, check if the other is
if (left == null) {
return right == null ? 0 : -1;
} else if (right == null) {
return 1; // left cannot be null
}
When I observe the above code, I can see that running
new SpelExpressionParser().parseExpression("7 < null").getValue();
will result in:
This basically mean swapping comparison logic and significant change of behavior and has strong impact to our application.
Probably there is conceptual problem - when comparing two values, assuming they are comparable, they can be equal, less or greater then the other one. But null value is not comparable in this sense to nothing except null value right?
Is this a bug?
Is null value comparison ever suppose to be TRUE when comparing with another not null value using <,>, ==, <=, >= operators?
Upvotes: 5
Views: 3663
Reputation: 6158
It's not a bug, it is the designed behavior of Spring team.
From the Spring document:
Greater/less-than comparisons against null follow a simple rule: null is treated as nothing here (i.e. NOT as zero). As a consequence, any other value is always greater than null (X > null is always true) and no other value is ever less than nothing (X < null is always false).
If this behavior affect your logic, I think you can do like following:
" first == null ? false : second == null ? false : first < second "
Upvotes: 2