We're updating the issue view to help you get more done. 

Rounding error when having a BigDecimal at runtime with @Max/@Min annotation set on a Number field

Description

I think I might have found a oddity in hibernate-validator 6.0.15.Final. It used to work with the version 5.4.2.Final.

Here is a test example:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 import lombok.Data; import org.junit.Test; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import javax.validation.constraints.Max; import java.math.BigDecimal; import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; public class ValidTest { @Data static class ClassToValidate{ public ClassToValidate() { failingNumber = new BigDecimal("1.001"); failingBigDecimal = new BigDecimal("1.001"); passingNumber = new BigDecimal("0.001"); passingBigDecimal = new BigDecimal("0.001"); } @Max(1) private Number failingNumber; @Max(1) private BigDecimal failingBigDecimal; @Max(1) private Number passingNumber; @Max(1) private BigDecimal passingBigDecimal; } @Test public void test(){ ValidatorFactory factory = Validation.buildDefaultValidatorFactory(); Validator validator = factory.getValidator(); Set<ConstraintViolation<ClassToValidate>> violations = validator .validate(new ClassToValidate()); for (ConstraintViolation<ClassToValidate> violation : violations) { System.out.println(violation); } assertThat(violations).hasSize(2); } }

The BigDecimal stored in a Number field will not trigger a constraint exception even though it is bigger than 1. And a bigdecimal such as 2.xxx would.

It feels like the validator does not (anymore) take into account the numbers after the comma in BigDecimals objects stored in a Number.

Environment

java version "1.8.0_201"
Windows 10

Status

Assignee

Marko Bekhta

Reporter

Arnaud Villevieille

Labels

None

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

backportReEvaluate

None

Components

Fix versions

Affects versions

6.0.15.Final

Priority

Major