New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The .equal? method should not be mapped to .isEqual for Java objects (e.g. Joda LocalDate) #5990
Comments
Furthermore, this bug has consequences beyond just producing unexpected truthy comparisons. For example, the following spec will fail with a describe Java::OrgJodaTime::LocalDate do
it 'does not crash when compared to a LocalDateTime' do
a = Java::OrgJodaTime::LocalDate.new(2018, 12, 4)
b = Java::OrgJodaTime::LocalDateTime.new(2018, 12, 4, 0, 0)
expect {a.equal? b}.not_to raise_exception
end
end Such broken comparisons can also happen unexpectedly in the background. For example, replacing the |
Odd...I would not have expected us to map this in this way. Agree, |
I see the issue here; because we try to turn boolean isSomething methods into their equivalent question-mark methods for Ruby, we are breaking the standard |
We do have an overridden #6000 will fix this by marking |
Environment
Expected Behavior
The following RSpec should pass:
Actual Behavior
The last
expect
fails, sincea.equal?
ends up calling the JavaisEqual
method on the LocalDate object, which implements value equality.Per https://ruby-doc.org/core-2.5.3/BasicObject.html#method-i-equal-3F, the
equal?
method should never be overridden and should always implement object identity comparison. However, some Java classes (notably including any JodaTime classes inheriting from AbstractPartial) may have anisEqual
method which implements some other, weaker type of comparison and which JRuby automatically calls whenequal?
is called on such an object from Ruby.IMO this is not a bug in JodaTime, since it's a pure Java library that is not designed with either Ruby's conventions or JRuby's idiosyncratic method resolution in mind. Instead, I would consider it a bug in JRuby itself: in attempting to bridge the different method naming conventions in Ruby and Java, it should also respect other established conventions of the two platforms and the "principle of least surprise", e.g. by not overriding
equal?
for classes whose author did not intend to do so.This is particularly relevant since JRuby itself ships with JodaTime, which includes classes that trigger this bug.
FWIW, we've worked around this issue in our own codebase with a monkey patch like this:
which restores the expected behavior. However, this is really only hiding the problem, and in any case only addresses it for one single class.
The text was updated successfully, but these errors were encountered: