Skip to content

Support BigDecimals with SpEL [SPR-9164] #13802

Closed

Description

@spring-projects-issues

Oliver Becker opened SPR-9164 and commented

When doing number arithmetic in SpEL the result type is apparently one of double, long or int.
This has the unwanted effect that for example float or BigDecimal values will be changed to int.

new java.math.BigDecimal("12.34")
evaluates to 12.34

-(new java.math.BigDecimal("12.34"))
evaluates to -12

see org.springframework.expression.spel.ast
OpPlus, OpMinus, OpMultiply, OpDivide


Affects: 3.1.1

Reference URL: #80

Attachments:

Issue Links:

1 votes, 7 watchers

Activity

  1. spring-projects-issues commented on Feb 26, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Added Andy Clement as watcher

    @Andy, could you take a look at this? Any comments would be appreciated; please assign to yourself if you can spare the time to fix it directly.

    Thanks!

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

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Becker commented

    I think the algorithm should be something along

    • if one of the types is double or float then use doubleValue
    • else if one of the types is byte, short, int or long then use longValue
    • else either add BigDecimal/BigInteger support or fallback to double
  3. spring-projects-issues commented on Mar 5, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Andy Clement commented

    This does need doing. I feel like widening byte/short/int to long value if not necessary might be overkill but I don't feel strongly enough that it shouldn't be done. Supporting BigDecimal/BigInteger in those referenced Op* classes should be added. Although not hard, care would need taking to do it in a way such that the code doesn't get too bloated with instanceof checks. Also needs test coverage, ideally for all cases/combinations. Don't think we have to take the further step of handling custom Number subtypes, that could be looked at when the scenario comes up.

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

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Andy, is this something you're willing to commit to for 3.2 M1 / M2? I'll assign to you if so.

  5. spring-projects-issues commented on Mar 15, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Stevo Slavić commented

    #13358 is somewhat related issue.

  6. spring-projects-issues commented on Apr 16, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Giovanni Dall'Oglio Risso commented

    Hi...

    I'm the reporter for the #13358 issue (related to this).

    We managed to solve this problem, adding BigDecimals; we also made a pull request: #80

    You can take that pull request, or start by that code (we added quite test cases).

    PS: maybe you can consider to add also the BigIntegers support...

  7. spring-projects-issues commented on May 14, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    I've asked Giovanni to touch up his pull request per the contributor guidelines. We can start with that support at least, which covers BigDecimal.

  8. spring-projects-issues commented on Jun 6, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Satyapal Reddy commented

    I have two functions:

    public class FunctionsClass {
    public int abs(int value) {
    return Math.abs(value);
    }
    public float abs(float value) {
    return Math.abs(value);
    }
    }

    and when I execute the test with following code:

    FunctionsClass testObject = new FunctionsClass();
    StandardEvaluationContext context = new StandardEvaluationContext();
    org.springframework.expression.Expression expression =  parser.parseExpression("abs(-10.2f)");
    Number result = expression.getValue(context, testObject, Number.class);
    

    it fails with:

    Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1033E:(pos 0): Method call of 'abs' is ambiguous, supported type conversions allow multiple variants to match

  9. spring-projects-issues commented on Jun 6, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Satyapal Reddy commented

    When I have two functions with different inputs int and float, eventhough I pass float, it is complaining about this being ambiguous

  10. spring-projects-issues commented on Jun 6, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Satyapal Reddy commented

    test case

  11. spring-projects-issues commented on Jun 8, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Giovanni Dall'Oglio Risso commented

    @Satyapal

    I think that your problem (SpEL unable to find the correct method) should be described in a new issue.
    This report deals with the problems with numeric operators, but you speak of indecisiveness in the resolution of the method to be applied.

    Regarding your problem, it seems that (at first glance) SPEL tries to find a function with the required signature, if not, try to find one that might fit (using converters), finds two candidates and does not know which one to apply.

    You might think to put in place a priority system depending on the type to convert.
    For example, when I have a float, I try first to see if there is a function that accepts a double, and if it finds it then use that, otherwise I try to see if there is one that accept long, int, etc..

    ...but pay attention, imagine you have this situation:

    public int someFunction (int a, double b);
    public int someFunction (double a, int b);
    

    Then you go and evaluate

    "someFunction (1f, 1f)"
    

    Which one of the two functions would you choose?

    So I think that it is necessary to open a new issue, and discuss it carefully in there.

  12. spring-projects-issues commented on Jun 8, 2012

    @spring-projects-issues
    CollaboratorAuthor

    Satyapal Reddy commented

    @Giovanni

    Thanks for the feedback. I will open a new bug. The possible fix that I tried locally (and that resolved my issue) involved introducing FloatLiteral.java and adding additional instanceof for Float.

    Satyapal

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

    @spring-projects-issues
    CollaboratorAuthor

    Andy Clement commented

    I just reviewed the PR Giovanni again - made a couple of comments on it but in principal it is in reasonable shape. There seems to be some code duplication in the tests and the coding standards aren't quite right for the if blocks, but it works and all the existing expression tests are passing for me. Chris/Phil - would you expect the submitter to fix the code duplication and styling thing in the PR and resubmit or would you do that?

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

    @spring-projects-issues
    CollaboratorAuthor

    Giovanni Dall'Oglio Risso commented

    This weekend I'm out, if you can wait, the first days of the next week I'll rebase and clean everything...

    Thanks

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

    @spring-projects-issues
    CollaboratorAuthor

    Giovanni Dall'Oglio Risso commented

    Hello.
    Everything is now OK, plus some additions:

    • From the original pull request to now, SpEL advanced [operator++, between function].
      • Now is everything covered (the new features), updated and tested.
    • In addition, some polish (like Phil Webb commit e83bdda)

    I rebased on master, hope is OK...
    Thanks

  16. spring-projects-issues commented on Nov 21, 2013

    @spring-projects-issues
    CollaboratorAuthor

    Oliver Becker commented

    Big thanks to Giovanni and Andy for fixing this!

  17. added
    type: enhancement
    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: enhancement

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions