[JIRA] (HV-1949) Nondeterministic behavior of HibernateValidator (in springboot?) when both annotation and xml-based definition are used
by Marko Bekhta (JIRA)
Marko Bekhta ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=557058%... ) *commented* on HV-1949 ( https://hibernate.atlassian.net/browse/HV-1949?atlOrigin=eyJpIjoiNDI2OWZi... )
Re: Nondeterministic behavior of HibernateValidator (in springboot?) when both annotation and xml-based definition are used ( https://hibernate.atlassian.net/browse/HV-1949?atlOrigin=eyJpIjoiNDI2OWZi... )
Hey martin mucha ( https://hibernate.atlassian.net/secure/ViewProfile.jspa?accountId=70121%3... ) ,
Thanks for providing a reproducer. I can confirm that the problem comes from the HV. What’s happening in this case right now is:
HV has multiple sources from where it is reading the metadata: annotations, XML, API(programmatic and various ServiceLoaders). Each next source would have higher priority and can be used to override the previous configuration.
Now what’s happening in your case:
* HV reads XML, which has a reference to your custom constraint, but as that constraint doesn’t have a provided validator either as part of the definition or in the XML – the XML metadata provider “thinks” that there are no validators to be applied to this constraint.
* HV “reads” programmatic definitions/ServiceLoadrs – finds a validator for your custom constraint and registers it. At this point, the constraint metadata from XML is not refreshed, and it still assumes that there are no validators.
* When you execute the validation for the first time, depending on which route it goes, it will cache the ConstraintValidator for the validated type… If it is going the XML route first – no validators, and you get the exceptions…. If it is an annotation route – annotation metadata in Spring would get built lazily, and as a result, at that point, it would already know about the validator and cache it. Future XML/annotation-based calls will be using the cached instance, and no errors will appear.
Until this is fixed, I can suggest that you add some quick validation call that will take an annotation route as part of the initialization process. Maybe if you have a configuration for the validator – add some post-construct action that would request a validator and make a validation call. Note this would only work if the constraint has the same definition (same annotation attributes) and the validated type is also the same + no changes to HibernateConstraintValidatorInitializationContext
( https://hibernate.atlassian.net/browse/HV-1949#add-comment?atlOrigin=eyJp... ) Add Comment ( https://hibernate.atlassian.net/browse/HV-1949#add-comment?atlOrigin=eyJp... )
Get Jira notifications on your phone! Download the Jira Cloud app for Android ( https://play.google.com/store/apps/details?id=com.atlassian.android.jira.... ) or iOS ( https://itunes.apple.com/app/apple-store/id1006972087?pt=696495&ct=EmailN... ) This message was sent by Atlassian Jira (v1001.0.0-SNAPSHOT#100225- sha1:bbd69a1 )
1 year, 4 months