From b4808e7c1ed7e71e806dd6a3bf4fb54b3d1194ab Mon Sep 17 00:00:00 2001 From: marko-bekhta Date: Fri, 30 Aug 2024 11:25:07 +0200 Subject: [PATCH] HV-2031 Add a config knob to predefined configuration so that we can get the "old-default" behavior by-default and enable the additional collection of information from the XML metadata --- ...dScopeHibernateValidatorConfiguration.java | 13 +++++++++++ .../PredefinedScopeConfigurationImpl.java | 12 ++++++++++ .../PredefinedScopeValidatorFactoryImpl.java | 8 +++++-- .../metadata/core/ConstraintHelper.java | 16 ++++++++++--- .../PredefinedScopeValidatorFactoryTest.java | 23 +++++++++++++++++-- 5 files changed, 65 insertions(+), 7 deletions(-) diff --git a/engine/src/main/java/org/hibernate/validator/PredefinedScopeHibernateValidatorConfiguration.java b/engine/src/main/java/org/hibernate/validator/PredefinedScopeHibernateValidatorConfiguration.java index a295e88430..5426f6c2e0 100644 --- a/engine/src/main/java/org/hibernate/validator/PredefinedScopeHibernateValidatorConfiguration.java +++ b/engine/src/main/java/org/hibernate/validator/PredefinedScopeHibernateValidatorConfiguration.java @@ -32,4 +32,17 @@ public interface PredefinedScopeHibernateValidatorConfiguration extends BaseHibe @Incubating @Deprecated PredefinedScopeHibernateValidatorConfiguration initializeLocales(Set locales); + + /** + * Specify whether to append the {@link #builtinConstraints(Set) built-in constraints} and {@link #initializeBeanMetaData(Set) beans to initialize} + * with constraints and beans provided only through XML mapping. + *

+ * This option is enabled by default. + * + * @param include Whether to include the beans defined only in xml as part of the {@link #initializeBeanMetaData(Set) set of beans to initialize} + * and also add built-in constraints used only in xml definitions as part of the {@link #builtinConstraints(Set) set of built-in constraints}. + * @return {@code this} for chaining configuration method calls. + */ + @Incubating + PredefinedScopeHibernateValidatorConfiguration includeBeansAndConstraintsDefinedOnlyInXml(boolean include); } diff --git a/engine/src/main/java/org/hibernate/validator/internal/engine/PredefinedScopeConfigurationImpl.java b/engine/src/main/java/org/hibernate/validator/internal/engine/PredefinedScopeConfigurationImpl.java index 5a0d92383d..bfeae2c8a2 100644 --- a/engine/src/main/java/org/hibernate/validator/internal/engine/PredefinedScopeConfigurationImpl.java +++ b/engine/src/main/java/org/hibernate/validator/internal/engine/PredefinedScopeConfigurationImpl.java @@ -30,6 +30,8 @@ public class PredefinedScopeConfigurationImpl extends AbstractConfigurationImpl< private Set> beanClassesToInitialize; + private boolean includeBeansAndConstraintsDefinedOnlyInXml = true; + public PredefinedScopeConfigurationImpl(BootstrapState state) { super( state ); } @@ -58,6 +60,10 @@ public Set> getBeanClassesToInitialize() { return beanClassesToInitialize; } + public boolean isIncludeBeansAndConstraintsDefinedOnlyInXml() { + return includeBeansAndConstraintsDefinedOnlyInXml; + } + @Override @Deprecated public PredefinedScopeHibernateValidatorConfiguration initializeLocales(Set localesToInitialize) { @@ -66,6 +72,12 @@ public PredefinedScopeHibernateValidatorConfiguration initializeLocales(Set enabledConstraints) { - return new DynamicConstraintHelper( BuiltinConstraint.resolve( enabledConstraints ) ); + public static ConstraintHelper forBuiltinConstraints(Set enabledConstraints, boolean attemptToResolveMissingBuiltInConstraintsOnTheFly) { + Set builtinConstraints = BuiltinConstraint.resolve( enabledConstraints ); + if ( attemptToResolveMissingBuiltInConstraintsOnTheFly ) { + return new DynamicConstraintHelper( builtinConstraints ); + } + else { + return new StaticConstraintHelper( builtinConstraints ); + } } @SuppressWarnings("deprecation") @@ -1154,7 +1160,11 @@ private static class StaticConstraintHelper extends ConstraintHelper { private final Map, List>> enabledBuiltinConstraints; private StaticConstraintHelper() { - this.enabledBuiltinConstraints = Collections.unmodifiableMap( resolve( new HashSet<>( Arrays.asList( BuiltinConstraint.values() ) ) ) ); + this( new HashSet<>( Arrays.asList( BuiltinConstraint.values() ) ) ); + } + + private StaticConstraintHelper(Set builtinConstraints) { + this.enabledBuiltinConstraints = Collections.unmodifiableMap( resolve( builtinConstraints ) ); } @SuppressWarnings("unchecked") diff --git a/engine/src/test/java/org/hibernate/validator/test/predefinedscope/PredefinedScopeValidatorFactoryTest.java b/engine/src/test/java/org/hibernate/validator/test/predefinedscope/PredefinedScopeValidatorFactoryTest.java index 7de772698e..636de71cd8 100644 --- a/engine/src/test/java/org/hibernate/validator/test/predefinedscope/PredefinedScopeValidatorFactoryTest.java +++ b/engine/src/test/java/org/hibernate/validator/test/predefinedscope/PredefinedScopeValidatorFactoryTest.java @@ -330,7 +330,27 @@ public String interpolate(String messageTemplate, Context context) { } @Test - public void testXmlDefinedConstraints() { + public void testXmlDefinedConstraintsDiscoveryDisabled() { + // we assume that all the metadata is defined in the xml, + // hence there is no built-in constraints nor beans to init, + // But we also do not ask HV to append the sets with the beans from XMLs or built-in constraints: + try ( + ValidatorFactory factory = Validation.byProvider( PredefinedScopeHibernateValidator.class ) + .configure() + .builtinConstraints( Collections.emptySet() ) + .initializeBeanMetaData( Collections.emptySet() ) + .includeBeansAndConstraintsDefinedOnlyInXml( false ) + .addMapping( PredefinedScopeValidatorFactoryTest.class.getResourceAsStream( "constraints-simplexmlbean.xml" ) ) + .buildValidatorFactory() + ) { + Validator validator = factory.getValidator(); + // Because all the metadata for this bean was in the XML, and we ignore it: + assertNoViolations( validator.validate( new SimpleXmlBean() ) ); + } + } + + @Test + public void testXmlDefinedConstraintsDiscoveryEnabled() { // we assume that all the metadata is defined in the xml, // hence there is no built-in constraints nor beans to init: try ( @@ -349,7 +369,6 @@ public void testXmlDefinedConstraints() { violationOf( NotNull.class ).withMessage( "must not be null" ) ); } - } private static ValidatorFactory getValidatorFactory() {