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-sequence {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
Looking at the code in org.hibernate.validator.internal.engine.ValidatorImpl.java When the groupSequence of a bean is no redefined this lines are executed
{code:java} else { Set<MetaConstraint<?>> metaConstraints = hostingBeanMetaData.getDirectMetaConstraints(); validateConstraintsForSingleDefaultGroupElement( validationContext, valueContext, validatedInterfaces, clazz, metaConstraints, Group.DEFAULT_GROUP ); } {code}
If I change the metaConstaints to be :
{code:java} else { Set<MetaConstraint<?>> metaConstraints = hostingBeanMetaData.getMetaConstraints(); validateConstraintsForSingleDefaultGroupElement( validationContext, valueContext, validatedInterfaces, clazz, metaConstraints, Group.DEFAULT_GROUP ); } {code:java}
Then the < getter > constraint is executed.
The only way to force the current code to call hostingBeanMetaData.getMetaConstraints() is to force the hostingBeanMetaData.defaultGroupSequenceIsRedefined() to be true which i could do by adding two times the same group to the <group-sequence> element
Why is hostingBeanMetaData.getMetaConstraints() used in one case and hostingBeanMetaData.getDirectMetaConstraints() in the other?. Using hostingBeanMetaData.getMetaConstraints() makes the < getter > constraint work
Not sure is a bug, but definitely a very strange behaviour, and a strange workaround to make the getter constraint to work. |
|