Actions
Patch #38772
closed<=> should return nil when invoked with an incomparable object
Description
`<=>` method of Ruby's standard classes returns nil if it is invoked with an incomparable object. For example, `1 <=> nil` returns nil. But in Redmine, `<=>` methods in most classes raises an exception.
One of the problems that this can cause is the following indirect error messages when comparisons are made between incomparable objects.
irb(main):002:0> [IssuePriority.first, nil].sort
Traceback (most recent call last):
1: from app/models/enumeration.rb:94:in `<=>'
NoMethodError (undefined method `position' for nil:NilClass)
The attached patch adds a type check and fixes the behavior to return nil.
Before:
irb(main):001:0> IssuePriority.first <=> nil
Traceback (most recent call last):
1: from app/models/enumeration.rb:94:in `<=>'
NoMethodError (undefined method `position' for nil:NilClass)
irb(main):002:0> [IssuePriority.first, nil].sort
Traceback (most recent call last):
1: from app/models/enumeration.rb:94:in `<=>'
NoMethodError (undefined method `position' for nil:NilClass)
irb(main):003:0> IssuePriority.first <=> Tracker.last
=> -1
After:
irb(main):001:0> IssuePriority.first <=> nil
=> nil
irb(main):002:0> [IssuePriority.first, nil].sort
(irb):2:in `sort': comparison of IssuePriority with nil failed (ArgumentError)
irb(main):003:0> IssuePriority.first <=> Tracker.last
=> nil
Files
Related issues
Actions