When using @AssertTrue in methods, the method is invoked 4 times during validation (Bean Validation)

3.7k views Asked by At

When using bean validation to validate the state of an object, the method annotated with @AssertTrue is called 4 times whenever the validation is invoked. It should only be called once per invocation.

Hibernate-validator version: 5.1.3.Final

Here is an example:

For the following class Motorcycle:

import javax.validation.constraints.AssertTrue;
class Motorcycle{
    private int fuel;
    private int tireDurability;

    @AssertTrue(message = "motorcycle.not.available.to.use")
    public boolean isAvailable(){
        return fuel > 0 && tireDurability > 0;
    }

    public void toUse(){...}
}

And the main:

import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;
public class Main{
    public static void main(String []args){
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();

        Set<ConstraintViolation<Motorcycle>> violations = validator.validate(new Motorcycle());

    }
}

When validator.validate(new Motorcycle()) is called, the method isAvailable() is invoked 4 times.

Can anyone help me with this situation? Is this a bug? How do I work around it?

1

There are 1 answers

0
mark_o On

You should try using Hibernate Validator 6.0.2.Final (it's currently the latest available version). Next test does pass with this (latest) version of validator:

@Test
public void testMotorcycle() throws Exception {
    AtomicInteger count = new AtomicInteger( 0 );

    Motorcycle motorcycle = new Motorcycle( count );
    validator.validate( motorcycle );
    assertEquals( motorcycle.count.get(), 1 );
}

class Motorcycle {
    final AtomicInteger count;
    private int fuel;
    private int tireDurability;

    Motorcycle(AtomicInteger count) {
        this.count = count;
    }

    @AssertTrue(message = "motorcycle.not.available.to.use")
    public boolean isAvailable() {
        count.incrementAndGet();
        return fuel > 0 && tireDurability > 0;
    }

}

I did try to run it with 5.1 as well - and it also passes there. How did you determined that the method is called 4 times ?