Skip to content

Commit

Permalink
Merge pull request #11598 from sdaschner/sdaschner-6094-validator-inject
Browse files Browse the repository at this point in the history
Added constraint validators as singleton beans
  • Loading branch information
gsmet authored Aug 25, 2020
2 parents 5afaaa8 + e0bbcdb commit c87742d
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,12 @@

import io.quarkus.arc.deployment.AdditionalBeanBuildItem;
import io.quarkus.arc.deployment.AnnotationsTransformerBuildItem;
import io.quarkus.arc.deployment.AutoAddScopeBuildItem;
import io.quarkus.arc.deployment.BeanArchiveIndexBuildItem;
import io.quarkus.arc.deployment.BeanContainerListenerBuildItem;
import io.quarkus.arc.deployment.UnremovableBeanBuildItem;
import io.quarkus.arc.processor.BeanInfo;
import io.quarkus.arc.processor.BuiltinScope;
import io.quarkus.deployment.Capabilities;
import io.quarkus.deployment.Capability;
import io.quarkus.deployment.Feature;
Expand Down Expand Up @@ -114,6 +116,7 @@ LogCleanupFilterBuildItem logCleanup() {
@BuildStep
void registerAdditionalBeans(BuildProducer<AdditionalBeanBuildItem> additionalBeans,
BuildProducer<UnremovableBeanBuildItem> unremovableBean,
BuildProducer<AutoAddScopeBuildItem> autoScopes,
Capabilities capabilities) {
// The bean encapsulating the Validator and ValidatorFactory
additionalBeans.produce(new AdditionalBeanBuildItem(ValidatorProvider.class));
Expand All @@ -129,6 +132,10 @@ void registerAdditionalBeans(BuildProducer<AdditionalBeanBuildItem> additionalBe
"io.quarkus.hibernate.validator.runtime.jaxrs.ResteasyContextLocaleResolver"));
}

// A constraint validator with an injection point but no scope is added as @Singleton
autoScopes.produce(AutoAddScopeBuildItem.builder().implementsInterface(CONSTRAINT_VALIDATOR).requiresContainerServices()
.defaultScope(BuiltinScope.SINGLETON).build());

// Do not remove the Bean Validation beans
unremovableBean.produce(new UnremovableBeanBuildItem(new Predicate<BeanInfo>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package io.quarkus.hibernate.validator.test;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Set;

import javax.inject.Inject;
import javax.validation.ConstraintViolation;
import javax.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.hibernate.validator.test.injection.TestBean;
import io.quarkus.hibernate.validator.test.injection.TestConstraint;
import io.quarkus.hibernate.validator.test.injection.TestInjectedBean;
import io.quarkus.hibernate.validator.test.injection.TestInjectionValidator;
import io.quarkus.test.QuarkusUnitTest;

public class ValidatorBeanInjectionTest {

@RegisterExtension
static final QuarkusUnitTest test = new QuarkusUnitTest().setArchiveProducer(() -> ShrinkWrap
.create(JavaArchive.class)
.addClasses(TestBean.class, TestConstraint.class, TestInjectedBean.class, TestInjectionValidator.class));

@Inject
Validator validator;

@Test
public void testValidationWithInjection() {
Set<ConstraintViolation<TestBean>> constraintViolations = validator.validate(new TestBean());

assertThat(constraintViolations).isNotEmpty();

TestBean bean = new TestBean();
bean.name = "Alpha";
constraintViolations = validator.validate(bean);
assertThat(constraintViolations).isEmpty();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package io.quarkus.hibernate.validator.test.injection;

public class TestBean {

@TestConstraint
public String name;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.hibernate.validator.test.injection;

import static java.lang.annotation.ElementType.ANNOTATION_TYPE;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import javax.validation.Constraint;
import javax.validation.Payload;
import javax.validation.constraints.NotNull;

@Retention(RUNTIME)
@Target({ FIELD, METHOD, ANNOTATION_TYPE, PARAMETER })
@NotNull
@Constraint(validatedBy = TestInjectionValidator.class)
@Documented
public @interface TestConstraint {

String message() default "";

Class<?>[] groups() default {};

Class<? extends Payload>[] payload() default {};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package io.quarkus.hibernate.validator.test.injection;

import java.util.Collections;
import java.util.List;

import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class TestInjectedBean {

public List<String> allowedStrings() {
return Collections.singletonList("Alpha");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.quarkus.hibernate.validator.test.injection;

import javax.inject.Inject;
import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class TestInjectionValidator implements ConstraintValidator<TestConstraint, String> {

@Inject
TestInjectedBean testInjectedBean;

@Override
public boolean isValid(String string, ConstraintValidatorContext context) {
return testInjectedBean.allowedStrings().contains(string);
}

}

0 comments on commit c87742d

Please sign in to comment.