Skip to content

Commit

Permalink
Merge pull request #32677 from gsmet/config-mapping-hv
Browse files Browse the repository at this point in the history
Switch Hibernate Validator extension to @ConfigMapping
  • Loading branch information
geoand authored Apr 18, 2023
2 parents c205249 + 37eacbd commit ea7b14d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,39 @@

import org.hibernate.validator.messageinterpolation.ExpressionLanguageFeatureLevel;

import io.quarkus.runtime.annotations.ConfigDocDefault;
import io.quarkus.runtime.annotations.ConfigDocSection;
import io.quarkus.runtime.annotations.ConfigGroup;
import io.quarkus.runtime.annotations.ConfigItem;
import io.quarkus.runtime.annotations.ConfigPhase;
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;

@ConfigMapping(prefix = "quarkus.hibernate-validator")
@ConfigRoot(phase = ConfigPhase.BUILD_AND_RUN_TIME_FIXED)
public class HibernateValidatorBuildTimeConfig {
public interface HibernateValidatorBuildTimeConfig {

/**
* Enable the fail fast mode. When fail fast is enabled the validation
* will stop on the first constraint violation detected.
*/
@ConfigItem(defaultValue = "false")
public boolean failFast;
@WithDefault("false")
boolean failFast();

/**
* Method validation.
*/
@ConfigDocSection
public HibernateValidatorMethodBuildTimeConfig methodValidation;
HibernateValidatorMethodBuildTimeConfig methodValidation();

/**
* Expression Language.
*/
@ConfigDocSection
public HibernateValidatorExpressionLanguageBuildTimeConfig expressionLanguage;
HibernateValidatorExpressionLanguageBuildTimeConfig expressionLanguage();

@ConfigGroup
public static class HibernateValidatorMethodBuildTimeConfig {
public interface HibernateValidatorMethodBuildTimeConfig {

/**
* Define whether overriding methods that override constraints should throw a {@code ConstraintDefinitionException}.
Expand All @@ -47,8 +50,8 @@ public static class HibernateValidatorMethodBuildTimeConfig {
* This would pose a strengthening of preconditions to be fulfilled by the caller."
* </pre>
*/
@ConfigItem(defaultValue = "false")
public boolean allowOverridingParameterConstraints;
@WithDefault("false")
boolean allowOverridingParameterConstraints();

/**
* Define whether parallel methods that define constraints should throw a {@code ConstraintDefinitionException}. The
Expand All @@ -63,8 +66,8 @@ public static class HibernateValidatorMethodBuildTimeConfig {
* This again is to avoid an unexpected strengthening of preconditions to be fulfilled by the caller."
* </pre>
*/
@ConfigItem(defaultValue = "false")
public boolean allowParameterConstraintsOnParallelMethods;
@WithDefault("false")
boolean allowParameterConstraintsOnParallelMethods();

/**
* Define whether more than one constraint on a return value may be marked for cascading validation are allowed.
Expand All @@ -79,12 +82,12 @@ public static class HibernateValidatorMethodBuildTimeConfig {
* overridden method of the super type or interface."
* </pre>
*/
@ConfigItem(defaultValue = "false")
public boolean allowMultipleCascadedValidationOnReturnValues;
@WithDefault("false")
boolean allowMultipleCascadedValidationOnReturnValues();
}

@ConfigGroup
public static class HibernateValidatorExpressionLanguageBuildTimeConfig {
public interface HibernateValidatorExpressionLanguageBuildTimeConfig {

/**
* Configure the Expression Language feature level for constraints, allowing the selection of
Expand All @@ -97,7 +100,7 @@ public static class HibernateValidatorExpressionLanguageBuildTimeConfig {
* created programmatically in validator implementations.
* The feature level for those can only be configured directly in the validator implementation.
*/
@ConfigItem(defaultValueDocumentation = "bean-properties")
public Optional<ExpressionLanguageFeatureLevel> constraintExpressionFeatureLevel;
@ConfigDocDefault("bean-properties")
Optional<ExpressionLanguageFeatureLevel> constraintExpressionFeatureLevel();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,9 @@ public void created(BeanContainer container) {
.defaultLocale(localesBuildTimeConfig.defaultLocale)
.beanMetaDataClassNormalizer(new ArcProxyBeanMetaDataClassNormalizer());

if (hibernateValidatorBuildTimeConfig.expressionLanguage.constraintExpressionFeatureLevel.isPresent()) {
if (hibernateValidatorBuildTimeConfig.expressionLanguage().constraintExpressionFeatureLevel().isPresent()) {
configuration.constraintExpressionLanguageFeatureLevel(
hibernateValidatorBuildTimeConfig.expressionLanguage.constraintExpressionFeatureLevel.get());
hibernateValidatorBuildTimeConfig.expressionLanguage().constraintExpressionFeatureLevel().get());
}

InstanceHandle<ConstraintValidatorFactory> configuredConstraintValidatorFactory = Arc.container()
Expand Down Expand Up @@ -127,13 +127,13 @@ public void created(BeanContainer container) {

// Hibernate Validator-specific configuration

configuration.failFast(hibernateValidatorBuildTimeConfig.failFast);
configuration.failFast(hibernateValidatorBuildTimeConfig.failFast());
configuration.allowOverridingMethodAlterParameterConstraint(
hibernateValidatorBuildTimeConfig.methodValidation.allowOverridingParameterConstraints);
hibernateValidatorBuildTimeConfig.methodValidation().allowOverridingParameterConstraints());
configuration.allowParallelMethodsDefineParameterConstraints(
hibernateValidatorBuildTimeConfig.methodValidation.allowParameterConstraintsOnParallelMethods);
hibernateValidatorBuildTimeConfig.methodValidation().allowParameterConstraintsOnParallelMethods());
configuration.allowMultipleCascadedValidationOnReturnValues(
hibernateValidatorBuildTimeConfig.methodValidation.allowMultipleCascadedValidationOnReturnValues);
hibernateValidatorBuildTimeConfig.methodValidation().allowMultipleCascadedValidationOnReturnValues());

InstanceHandle<ScriptEvaluatorFactory> configuredScriptEvaluatorFactory = Arc.container()
.instance(ScriptEvaluatorFactory.class);
Expand Down

0 comments on commit ea7b14d

Please sign in to comment.