Uploaded image for project: 'Hibernate Validator'
  1. HV-1699

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

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 6.0.15.Final
    • Fix Version/s: 6.1.0.Alpha4, 6.0.16.Final
    • Component/s: validators
    • Labels:
      None
    • Environment:
      java version "1.8.0_201"
      Windows 10

      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:

      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.

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: