Allow getter constraints to be specified for subclasses in XML configuration
Description
Environment
Attachments
Activity
Rob DickinsonMay 9, 2018 at 9:08 AM
Hi @gsmet,
I'm a colleague of @alanbos and have to agree with his view, your suggestion will cause a lot of pain to initially implement in our product and also for ongoing work. The ability to specify constraints directly onto sub-classes was a very useful feature, and it seemed to be a feature you could only achieve through an XML mapping configuration.
I hope you can reconsider your view that it was a misfeature and should be treated as an exception during the bootstrap of the validator configuration.
I have this morning raised this PR https://github.com/hibernate/hibernate-validator/pull/958 which fixes this issue if you were to accept it as a first class feature of the XML mapping configuration.
Kind regards,
Rob
Alan BoshierMay 8, 2018 at 3:50 PM
Thanks Guillaume for your understanding; I hope it will be possible to work something out.

Guillaume SmetMay 8, 2018 at 2:23 PM
I can see how it's unpractical for you. The thing is that the HV engine is not ready to accept "XML annotations" on something other than a concrete method. That's why you can have this weird workaround working.
I'll try to see if we can somehow support this use case without too many risks and come back to you.
Alan BoshierMay 8, 2018 at 12:50 PM
Thanks for your reply Guillaume; that's going to cause us considerable pain to live with unfortunately, because the facility to use XML constraints to provide deploy-specific validation, without needing to artificially override superclass properties in code, is something we rely on. This was actually a really useful feature which encouraged us to adopt the hibernate validator originally.

Guillaume SmetMay 8, 2018 at 12:17 PM
Hi ,
Thanks for the test case.
So we discussed it with Marko and we think it was an unsupported "misfeature" of 4.3.x.
The XML configuration should allow you to annotate your classes in the same way as the annotations so you shouldn't be able to add XML constraints where you wouldn't be able to add annotation constraints.
If you want to add constraints to a specific subclass, you need to override the method as you would do with annotations.
That being said, we shouldn't ignore this and we should throw an exception at bootstrap. That's what we will do starting from 6.1.
I know it's not the outcome you hoped for but at least it's going to be consistent.
Details
Assignee
UnassignedUnassignedReporter
Jesus Menendez CastellanoJesus Menendez Castellano(Deactivated)Participants
Alan Boshier
Guillaume Smet
Gunnar Morling
Jesus Menendez Castellano
Rob DickinsonPull Request
Components
Fix versions
Affects versions
Priority
Major
Details
Details
Assignee
Reporter

Participants



Given the contraint mapping constraints-config.xml:
Where MyClass extends from MyObject
The Validator built using the constraint mappings xml, ignores the getter Constraint
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
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
If I change the metaConstaints to be :
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.