Project

General

Profile

Patch #38772

Updated by Go MAEDA over 1 year ago

`<=>` 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. 

 <pre><code class="irb"> 
 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) 
 </code></pre> 

 The attached patch adds a type check and fixes the behavior to return nil. 

 Before: 
 <pre><code class="irb"> 
 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 
 </code></pre> 

 After: 
 <pre><code class="irb"> class="console"> 
 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 
 </code></pre> 

Back