Skip to content

Doc: SpEL behavior when comparing value to null [SPR-14361] #18933

Closed
@spring-projects-issues

Description

@spring-projects-issues

Marko Talijanac opened SPR-14361 and commented

When in SpEL a number is compared to null, a result is interpreted as "null is always the lowest number". Behavior is unexpected and undocumented.

As null is not a number expected behavior would be either exception or false in all cases except when comparing with other null value.

For example expression "x == null && x < 100" should always yield false (or raise exception) as x is either null or a number with a value.

Behavior is not overridable with user provided TypeComparator (except by throwing exception from TypeComparator) as operators never invoke TypeComparator#canCompare method, and always flatten comparator result of (-1,0,1) to true|false result.

Also current behaviour is not consistent with past spring versions. See this stack overflow question

{{examples of popular comparisons
null == null => true

null < 1 => true
1 > null => true
null <= 1 => true
1 >= null => true

null < -1 => true
-1 > null => true
null <= -1 => true
-1 >= null => true

null > 1 => false
1 < null => false
null >= 1 => false
1 <= null => false

null > -1 => false
-1 < null => false
null >= -1 => false
-1 <= null => false}}


Affects: 3.2.16, 4.3 GA

Issue Links:

Referenced from: commits 6bdc5bf, 05579fd, 5ccc8e3, c22cad1

Backported to: 4.2.9, 3.2.18

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)status: backportedAn issue that has been backported to maintenance branchestype: taskA general task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions