Skip to content

Commit

Permalink
HV-2031 Add a config knob to predefined configuration
Browse files Browse the repository at this point in the history
so that we can get the "old-default" behavior by-default and enable the additional collection of information from the XML metadata
  • Loading branch information
marko-bekhta committed Sep 3, 2024
1 parent faa7cc0 commit b4808e7
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,17 @@ public interface PredefinedScopeHibernateValidatorConfiguration extends BaseHibe
@Incubating
@Deprecated
PredefinedScopeHibernateValidatorConfiguration initializeLocales(Set<Locale> 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.
* <p>
* 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public class PredefinedScopeConfigurationImpl extends AbstractConfigurationImpl<

private Set<Class<?>> beanClassesToInitialize;

private boolean includeBeansAndConstraintsDefinedOnlyInXml = true;

public PredefinedScopeConfigurationImpl(BootstrapState state) {
super( state );
}
Expand Down Expand Up @@ -58,6 +60,10 @@ public Set<Class<?>> getBeanClassesToInitialize() {
return beanClassesToInitialize;
}

public boolean isIncludeBeansAndConstraintsDefinedOnlyInXml() {
return includeBeansAndConstraintsDefinedOnlyInXml;
}

@Override
@Deprecated
public PredefinedScopeHibernateValidatorConfiguration initializeLocales(Set<Locale> localesToInitialize) {
Expand All @@ -66,6 +72,12 @@ public PredefinedScopeHibernateValidatorConfiguration initializeLocales(Set<Loca
return thisAsT();
}

@Override
public PredefinedScopeHibernateValidatorConfiguration includeBeansAndConstraintsDefinedOnlyInXml(boolean include) {
this.includeBeansAndConstraintsDefinedOnlyInXml = include;
return thisAsT();
}

@Override
protected boolean preloadResourceBundles() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ public PredefinedScopeValidatorFactoryImpl(ConfigurationState configurationState
this.propertyNodeNameProvider = ValidatorFactoryConfigurationHelper.determinePropertyNodeNameProvider( hibernateSpecificConfig, properties, externalClassLoader );

this.valueExtractorManager = new ValueExtractorManager( configurationState.getValueExtractors() );
ConstraintHelper constraintHelper = ConstraintHelper.forBuiltinConstraints( hibernateSpecificConfig.getBuiltinConstraints() );
ConstraintHelper constraintHelper = ConstraintHelper.forBuiltinConstraints(
hibernateSpecificConfig.getBuiltinConstraints(),
hibernateSpecificConfig.isIncludeBeansAndConstraintsDefinedOnlyInXml() );
TypeResolutionHelper typeResolutionHelper = new TypeResolutionHelper();

ConstraintCreationContext constraintCreationContext = new ConstraintCreationContext( constraintHelper,
Expand Down Expand Up @@ -194,7 +196,9 @@ public PredefinedScopeValidatorFactoryImpl(ConfigurationState configurationState
XmlMetaDataProvider xmlMetaDataProvider;
if ( mappingParser != null && mappingParser.createConstrainedElements() ) {
xmlMetaDataProvider = new XmlMetaDataProvider( mappingParser );
beanClassesToInitialize.addAll( xmlMetaDataProvider.configuredBeanClasses() );
if ( hibernateSpecificConfig.isIncludeBeansAndConstraintsDefinedOnlyInXml() ) {
beanClassesToInitialize.addAll( xmlMetaDataProvider.configuredBeanClasses() );
}
}
else {
xmlMetaDataProvider = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -400,8 +400,14 @@ public static ConstraintHelper forAllBuiltinConstraints() {
return new StaticConstraintHelper();
}

public static ConstraintHelper forBuiltinConstraints(Set<String> enabledConstraints) {
return new DynamicConstraintHelper( BuiltinConstraint.resolve( enabledConstraints ) );
public static ConstraintHelper forBuiltinConstraints(Set<String> enabledConstraints, boolean attemptToResolveMissingBuiltInConstraintsOnTheFly) {
Set<BuiltinConstraint> builtinConstraints = BuiltinConstraint.resolve( enabledConstraints );
if ( attemptToResolveMissingBuiltInConstraintsOnTheFly ) {
return new DynamicConstraintHelper( builtinConstraints );
}
else {
return new StaticConstraintHelper( builtinConstraints );
}
}

@SuppressWarnings("deprecation")
Expand Down Expand Up @@ -1154,7 +1160,11 @@ private static class StaticConstraintHelper extends ConstraintHelper {
private final Map<Class<? extends Annotation>, List<? extends ConstraintValidatorDescriptor<?>>> enabledBuiltinConstraints;

private StaticConstraintHelper() {
this.enabledBuiltinConstraints = Collections.unmodifiableMap( resolve( new HashSet<>( Arrays.asList( BuiltinConstraint.values() ) ) ) );
this( new HashSet<>( Arrays.asList( BuiltinConstraint.values() ) ) );
}

private StaticConstraintHelper(Set<BuiltinConstraint> builtinConstraints) {
this.enabledBuiltinConstraints = Collections.unmodifiableMap( resolve( builtinConstraints ) );
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -349,7 +369,6 @@ public void testXmlDefinedConstraints() {
violationOf( NotNull.class ).withMessage( "must not be null" )
);
}

}

private static ValidatorFactory getValidatorFactory() {
Expand Down

0 comments on commit b4808e7

Please sign in to comment.