Given the contraint mapping constraints-config.xml:
{code:xml} <constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping/validation-mapping-1.1.xsd" xmlns="http://jboss.org/xml/ns/javax/validation/mapping" version="1.1">
<bean class="mypackage.MyClass"> <getter name="id"> <constraint annotation="org.hibernate.validator.constraints.NotBlank"/> </getter> </bean> </constraint-mappings> {code}
Where MyClass extends from MyObject
{code:java} public class MyObject { private final String id;
public MyObject(String id) { this.id = id; }
public String getId() { return id; } } public class MyClass extends MyObject { public MyClass(String id) { super(id); } } {code}
The Validator built using the constraint mappings xml, ignores the getter Constraint
{code:java}
class Main { static void main(String[] args) {
MyClass myinstance = new MyClass(""); Set<ConstraintViolation<MyClass>> constraintViolations = getValidator().validate(myinstance); }
static final Validator getValidator() { final Configuration<?> config = Validation.byDefaultProvider().configure();
try (InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("contraints-config.xml")) { config.addMapping(inputStream); return config.buildValidatorFactory().getValidator();
} catch (IOException ex) { throw new UncheckedIOException(ex); }
} } {code}
No constraint validations are reported even if the id is blank
But if I add to the constraints-config.xml, a group-sequence element with two entries with the same group value using the class name, then it works. And I think the reason is in the org.hibernate.validator.internal.engine.ValidatorImpl
So if I add the group-s {code:xml} <constraint-mappings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://jboss.org/xml/ns/javax/validation/mapping/validation-mapping-1.1.xsd" xmlns="http://jboss.org/xml/ns/javax/validation/mapping" version="1.1">
<bean class="mypackage.MyClass"> <!-- A group sequence with two entries needs to be created because a xtrange bug that make getter properties not to be validated --> <group-sequence> <value>mypackage.MyClass</value> <value>mypackage.MyClass</value> </group-sequence> <getter name="id"> <constraint annotation="org.hibernate.validator.constraints.NotBlank"/> </getter> </bean> </constraint-mappings> {code}
Now the validation works, and reports a ConstraintValidation
|
|