-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32586 from radcortez/fix-24540
Validator for Config only
- Loading branch information
Showing
8 changed files
with
373 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
228 changes: 184 additions & 44 deletions
228
.../src/main/java/io/quarkus/hibernate/validator/deployment/HibernateValidatorProcessor.java
Large diffs are not rendered by default.
Oops, something went wrong.
75 changes: 75 additions & 0 deletions
75
...ava/io/quarkus/hibernate/validator/test/config/ConfigMappingInjectionInValidatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package io.quarkus.hibernate.validator.test.config; | ||
|
||
import static java.lang.annotation.ElementType.ANNOTATION_TYPE; | ||
import static java.lang.annotation.ElementType.TYPE; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
import jakarta.inject.Inject; | ||
import jakarta.validation.Constraint; | ||
import jakarta.validation.ConstraintValidator; | ||
import jakarta.validation.ConstraintValidatorContext; | ||
import jakarta.validation.Payload; | ||
import jakarta.validation.Validator; | ||
|
||
import org.jboss.shrinkwrap.api.ShrinkWrap; | ||
import org.jboss.shrinkwrap.api.spec.JavaArchive; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
import io.quarkus.runtime.annotations.StaticInitSafe; | ||
import io.quarkus.test.QuarkusUnitTest; | ||
import io.smallrye.config.ConfigMapping; | ||
import io.smallrye.config.WithDefault; | ||
|
||
class ConfigMappingInjectionInValidatorTest { | ||
@RegisterExtension | ||
private static final QuarkusUnitTest TEST = new QuarkusUnitTest() | ||
.setArchiveProducer(() -> ShrinkWrap.create(JavaArchive.class)); | ||
|
||
@Inject | ||
Validator validator; | ||
|
||
@Test | ||
void valid() { | ||
assertTrue(validator.validate(new Entity()).isEmpty()); | ||
} | ||
|
||
@StaticInitSafe | ||
@ConfigMapping(prefix = "valid.config") | ||
public interface ValidConfig { | ||
@WithDefault("true") | ||
boolean isValid(); | ||
} | ||
|
||
@Target({ TYPE, ANNOTATION_TYPE }) | ||
@Retention(RUNTIME) | ||
@Constraint(validatedBy = { ValidEntityValidator.class }) | ||
@Documented | ||
public @interface ValidEntity { | ||
String message() default ""; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
} | ||
|
||
public static class ValidEntityValidator implements ConstraintValidator<ValidEntity, Entity> { | ||
@Inject | ||
ValidConfig validConfig; | ||
|
||
@Override | ||
public boolean isValid(Entity value, ConstraintValidatorContext context) { | ||
return validConfig.isValid(); | ||
} | ||
} | ||
|
||
@ValidEntity | ||
public static class Entity { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 44 additions & 1 deletion
45
...n/java/io/quarkus/hibernate/validator/runtime/HibernateBeanValidationConfigValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,55 @@ | ||
package io.quarkus.hibernate.validator.runtime; | ||
|
||
import java.util.Set; | ||
|
||
import jakarta.validation.Validation; | ||
import jakarta.validation.Validator; | ||
import jakarta.validation.ValidatorFactory; | ||
|
||
import org.hibernate.validator.PredefinedScopeHibernateValidator; | ||
import org.hibernate.validator.PredefinedScopeHibernateValidatorConfiguration; | ||
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorFactoryImpl; | ||
|
||
import io.smallrye.config.validator.BeanValidationConfigValidator; | ||
|
||
public class HibernateBeanValidationConfigValidator implements BeanValidationConfigValidator { | ||
|
||
public HibernateBeanValidationConfigValidator(Set<String> constraints, Set<Class<?>> classesToBeValidated) { | ||
PredefinedScopeHibernateValidatorConfiguration configuration = Validation | ||
.byProvider(PredefinedScopeHibernateValidator.class) | ||
.configure(); | ||
|
||
// TODO - There is no way to retrieve locales from configuration here (even manually). We need to add a way to configure the validator from SmallRye Config. | ||
configuration | ||
.ignoreXmlConfiguration() | ||
.builtinConstraints(constraints) | ||
.initializeBeanMetaData(classesToBeValidated) | ||
.constraintValidatorFactory(new ConstraintValidatorFactoryImpl()); | ||
|
||
ConfigValidatorHolder.initialize(configuration.buildValidatorFactory()); | ||
} | ||
|
||
@Override | ||
public Validator getValidator() { | ||
return ValidatorHolder.getValidator(); | ||
return ConfigValidatorHolder.getValidator(); | ||
} | ||
|
||
// Store in a holder, so we can easily reference it and shutdown the validator | ||
public static class ConfigValidatorHolder { | ||
private static ValidatorFactory validatorFactory; | ||
private static Validator validator; | ||
|
||
static void initialize(ValidatorFactory validatorFactory) { | ||
ConfigValidatorHolder.validatorFactory = validatorFactory; | ||
ConfigValidatorHolder.validator = validatorFactory.getValidator(); | ||
} | ||
|
||
static ValidatorFactory getValidatorFactory() { | ||
return validatorFactory; | ||
} | ||
|
||
static Validator getValidator() { | ||
return validator; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1 change: 0 additions & 1 deletion
1
...validator/runtime/src/main/resources/META-INF/services/io.smallrye.config.ConfigValidator
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters