Hi all, can someone please tell me if I'm doing something wrong or point me to the right forum?
This seems like a very serious bug in HV 4.2.0.Beta2.
The code works as expected with HV 4.1.0.Final.If my custom class-level validator uses context.buildConstraintViolationWithTemplate(..).
addNode, then the node name becomes the prefix for all subsequent property paths.
Result from JUnit below:
Code:
junit.framework.AssertionFailedError: expected:<[field2, myNode, field1]> but was:<[myNode.field1, myNode, myNode.field2]>
(modulo HashSet randomization...another pet peeve of mine, why can't y'all use LinkedHashSet instead?)
Code:
public class CustomMessageTest extends Assert {
@Retention(RetentionPolicy.RUNTIME)
@Constraint(validatedBy=MyClassLevelValidation.Validator.class)
public @interface MyClassLevelValidation {
String message() default "failed";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
public static class Validator implements ConstraintValidator<MyClassLevelValidation,MyObject> {
@Override
public void initialize(MyClassLevelValidation constraintAnnotation) {}
@Override
public boolean isValid(MyObject value, ConstraintValidatorContext context) {
if (/* stuff */ true) {
context.buildConstraintViolationWithTemplate(
context.getDefaultConstraintMessageTemplate())
.addNode("myNode") // <<< problem here: field1 becomes prefix for all other validations
.addConstraintViolation()
.disableDefaultConstraintViolation();
return false;
}
return true;
}
}
}
@MyClassLevelValidation
public static class MyObject {
@NotNull // field level validation
String field1;
@NotNull
String field2;
}
@Test
public void testValidation() {
Set<ConstraintViolation<MyObject>> violations =
Validation.buildDefaultValidatorFactory()
.getValidator()
.validate(new MyObject());
assertEquals(3, violations.size());
Set<String> paths = new HashSet<String>();
for (ConstraintViolation<MyObject> violation : violations) {
paths.add(violation.getPropertyPath().toString());
}
assertEquals(new HashSet<String>(Arrays.asList("field1", "field2", "myNode")), paths);
}
}
The only recent BVAL issue I can find that seems related is this:
http://opensource.atlassian.com/project ... e/BVTCK-12Did that fix introduce this brokenness?