mapstruct icon indicating copy to clipboard operation
mapstruct copied to clipboard

Conversion of BigDecimal to primitive double wrong with 1.5.2

Open urswiss opened this issue 3 years ago • 3 comments

After migrating from 1.4.2 to 1.5.2 a test failed. The result from BigDecimal to double is not the same anymore. Please have a look. Thanks.

https://github.com/urswiss/mapstruct-bigdecimal-to-double

urswiss avatar Jun 29 '22 15:06 urswiss

Here is the analysis:

available method to use:

    default Long mapAmount(BigDecimal amount) {
        return amount.movePointRight(2).longValue();
    }

Reason

As it is legal to assign a long to a double, mapstruct will use this method because it is user defined.

Generated code

1.4.2

simpleDestination.setRoundingDiff( source.getRoundingDifference().doubleValue() );

1.5.2

simpleDestination.setRoundingDiff( mapAmount( source.getRoundingDifference() ) );

work around

Use qualifiers to mark the mapAmount method as one specific for certain mappings and use qualfiedBy there to link them.

What will we do about this

I think this is also related to: #2840 and #1216 Because if we pick the exact match, there might be a better workaround by manually creating the default double map(BigDecimal decimal) method.

@filiphr: WDYT ?

Zegveld avatar Jun 29 '22 18:06 Zegveld

Thanks for the analysis @Zegveld. This is most definitely related to #2840 and #1216. We'll need to figure out what actually broke this behaviour.

I think that we need to fix this inconsistency, because mapping from BigDecimal to double shouldn't use BigDecimal to long.

filiphr avatar Jun 30 '22 19:06 filiphr

After doing some analysis on this one and #2849 it seems like the work we did in #2320 broke this. I still need to analyse why this happened. This was not supposed to happen when we did that PR. What do you think @sjaakd? It feels like the fact that the return type can be assigned to the expected type is causing the problem.

filiphr avatar Aug 21 '22 17:08 filiphr

I am happy to announce that we have managed to find the root cause for this problem and we have a pending fix in #3032

filiphr avatar Sep 26 '22 16:09 filiphr