Skip to content

SpEL: OpEQ should use equals() [SPR-9194] #13832

Closed

Description

@spring-projects-issues

Oliver Becker opened SPR-9194 and commented

The implementation of the SpEL == operator in org.springframework.expression.spel.ast.OpEQ does some special handling for Number types and Comparables, but finally uses left == right.
I think it should always call left.equals(right) - or for null-safety left == null ? right == null : left.equals(right)
This would be consistent with Scala or Groovy, for example.

As for the number types: a float 1.2 would be equal to a short 1 (see also https://jira.springsource.org/browse/SPR-9164)


Affects: 3.1.1

Issue Links:

2 votes, 6 watchers

Activity

  1. spring-projects-issues commented on Mar 2, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Becker commented

    I just noted that at least all of the standard Java Number subclasses implement Comparable.
    So the special code for Number might be completely dropped.

  2. spring-projects-issues commented on Mar 8, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Added Andy Clement as a watcher. @Andy please take a look at this an comment if you have a moment.

  3. spring-projects-issues commented on Aug 6, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    ping for comments from @Andy

  4. spring-projects-issues commented on Aug 10, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Andy Clement commented

    The change sounds fine to me. But the pull request in that other issue to add a floatliteral will likely also fix the 'a float 1.2 would be equal to a short 1' situation.

  5. spring-projects-issues commented on Aug 12, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Thanks, Andy. Linked to #13802 and slating for the general backlog then.

  6. spring-projects-issues commented on Jun 16, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Becker commented

    I recently fell again in the trap described in this issue (with database entity objects instead of numbers).
    Since Spring 4.0 aims at a stronger Groovy integration I think it is time to bring this issue back on the table. Thanks.

  7. spring-projects-issues commented on Nov 5, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Martin Frey commented

    Definately vote up. This is a common pitfall which is not necessary. Jboss EL implements it too, as does thymeleaf.

  8. spring-projects-issues commented on Nov 6, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Andy Clement commented

    @Oliver: I'm taking a look at this. I just want to understand your proposal - you want to remove everything in the implementation of OpEq and replace it with:

    Object left = getLeftOperand().getValueInternal(state).getValue();
    Object right = getRightOperand().getValueInternal(state).getValue();
    return BooleanTypedValue.forValue(left==null?right==null:left.equals(right));
    

    This removes the ability for a SpEL consumer to plug in their own type comparator - not necessarily a problem. But if it were removed from here it makes sense to also remove it from the other mathematical operators (otherwise we could get into some weirdness where == returns one answer but is contradicted by the <,<=,>=,> answer for the same operands). We are then left with: if the user needs objects to be comparable they should ensure the classes for those objects implement Comparable.

  9. spring-projects-issues commented on Nov 6, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Andy Clement commented

    Or were you just proposing to change the == line to use .equals?

  10. spring-projects-issues commented on Nov 7, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Andy Clement commented

    Removing the whole body of OpEq won't do the right thing for numbers. They may implement Comparable but they don't do the widening for us. Without the special handling will mean checks like this fail "2.0d == 2" which currently work. So I propose under this bug simply to change the == to use .equals in the final clause of the comparison method.

  11. spring-projects-issues commented on Nov 7, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Martin Frey commented

    Why not do the most obvious tests upfront?

        Object left = ...
        Object right = ...
    
        if (left == right)
          return true;
        else if (left != null && left instanceof Object && left.equals(right))
          return true;
        else
          restofthetests;
    

    After that I would put this code into an utility class and reuse the code from the "not equals" processor and just negate the outcome.

    What do you think?

  12. spring-projects-issues commented on Nov 7, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Andy Clement commented

    My only hesitation with that is that it disturbs the ordering where right now Comparable is consulted ahead of falling back on equals. Someone may have built a system that depends on it being like that. Maybe they implemented Comparable but didn't wire up equals to use compareTo so it does something different... It may be a bug in their app but the important thing is that the behaviour will change when they update to a new spring level and it may not be obvious why.

    Of course you could argue a similar point about switching from == to equals...

    If you need the expressions to go fast, you want the JIT that I have in a branch which compiles SpEL expressions to bytecode.

  13. spring-projects-issues commented on Nov 7, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Andy Clement commented

    Pull request submitted to Spring-Framework: #406

    I made the change I proposed but also did as Martin suggested and pulled it up into a common utility method in the superclass for use in == and !=

  14. spring-projects-issues commented on Nov 7, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Martin Frey commented

    Thanks for the work on it.
    I still dont see the point on not calling == and .equals upfront. A comparable should still have a valid equals implementation, right? Also from the performance point of view this would be better i would say.

    In the end this pull request should solve the pitfall.

    Thank you

  15. spring-projects-issues commented on Nov 8, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Becker commented

    Looks fine to with one exception: the order of Number class checks. BigDecimals won't be compared correctly. I suppose new BigDecimal("1.2") == new BigDecimal("1.3") would return true (fallback to int).

    I wonder if both are numbers then why not simply compare double values (for all number types). Would this break anything?
    Do you have a unit test that fails with this simplification?

    if (left instanceof Number && right instanceof Number) {
        Number op1 = (Number) left;
        Number op2 = (Number) right;
        return (op1.doubleValue() == op2.doubleValue());
    }
    
  16. spring-projects-issues commented on Nov 8, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Andy Clement commented

    Basically in an attempt to get the simplest thing in, I did the minimum in the PR - switch from == to .equals(). Given the bug has been open for 18months I thought I'd do something we could just get in and then we can move on. If we want further jiras to explore changing the ordering of trying comparable then equals by promoting equals to be tried first, we can perhaps look at that. I'm pretty sure there are many areas of SpEL that could be optimized.

    Does the other big PR related to BigDecimal address what you are describing Oliver? I haven't looked at that one yet, it seemed to include handling of the BigX types everywhere.

  17. spring-projects-issues commented on Nov 8, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Andy Clement commented

    @Oliver that alternative version of checking the numbers does behave for the regression tests I have but it will also break down if there is precision loss when the BigDecimal is converted to a double. I suspect proper Big handling as in that other PR is the right thing to do to support these types properly.

  18. spring-projects-issues commented on Nov 11, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Phil Webb commented

    #406

  19. added
    type: bug
    in: core
    on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: coretype: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions