Search in sources :

Example 1 with ConstraintAnnotationDescriptor

use of org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor in project graphql-java-extended-validation by graphql-java.

the class ResourceBundleMessageInterpolator method buildHibernateContext.

@SuppressWarnings("ConstantConditions")
private MessageInterpolatorContext buildHibernateContext(Map<String, Object> messageParams, ValidationEnvironment validationEnvironment) {
    Object validatedValue = validationEnvironment.getValidatedValue();
    ConstraintAnnotationDescriptor<BridgeAnnotation> annotationDescriptor = new ConstraintAnnotationDescriptor.Builder<>(BridgeAnnotation.class).build();
    ConstraintDescriptorImpl<BridgeAnnotation> constraintDescriptor = new ConstraintDescriptorImpl<>(ConstraintHelper.forAllBuiltinConstraints(), null, annotationDescriptor, ConstraintLocationKind.FIELD, ConstraintType.GENERIC);
    Map<String, Object> expressionVariables = StandardELVariables.standardELVars(validationEnvironment);
    Class<?> rootBeanType = null;
    Path propertyPath = null;
    return new MessageInterpolatorContext(constraintDescriptor, validatedValue, rootBeanType, propertyPath, messageParams, expressionVariables, ExpressionLanguageFeatureLevel.BEAN_PROPERTIES, true);
}
Also used : Path(jakarta.validation.Path) ResultPath(graphql.execution.ResultPath) MessageInterpolatorContext(org.hibernate.validator.internal.engine.MessageInterpolatorContext) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)

Example 2 with ConstraintAnnotationDescriptor

use of org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor in project hibernate-validator by hibernate.

the class ResourceBundleMessageInterpolatorTest method testCorrectMessageInterpolationIfParameterCannotBeReplaced.

@Test
@TestForIssue(jiraKey = "HV-182")
public void testCorrectMessageInterpolationIfParameterCannotBeReplaced() {
    ConstraintAnnotationDescriptor.Builder<Max> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Max.class);
    String message = "Message should stay unchanged since {fubar} is not replaceable";
    descriptorBuilder.setMessage(message);
    descriptorBuilder.setAttribute("value", 10L);
    ConstraintAnnotationDescriptor<Max> maxDescriptor = descriptorBuilder.build();
    ConstraintDescriptorImpl<Max> constraintDescriptor = new ConstraintDescriptorImpl<Max>(ConstraintHelper.forAllBuiltinConstraints(), null, maxDescriptor, ConstraintLocationKind.FIELD);
    interpolator = new ResourceBundleMessageInterpolator(new TestResourceBundleLocator());
    MessageInterpolator.Context messageInterpolatorContext = createMessageInterpolatorContext(constraintDescriptor);
    String actual = interpolator.interpolate(maxDescriptor.getMessage(), messageInterpolatorContext);
    assertEquals(actual, message, "The message should not have changed.");
}
Also used : ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) Max(jakarta.validation.constraints.Max) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Example 3 with ConstraintAnnotationDescriptor

use of org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor in project hibernate-validator by hibernate.

the class ResourceBundleMessageInterpolatorTest method testRecursiveMessageInterpolation.

@Test
@TestForIssue(jiraKey = "HV-102")
public void testRecursiveMessageInterpolation() {
    ConstraintAnnotationDescriptor.Builder<Max> descriptorBuilder = new ConstraintAnnotationDescriptor.Builder<>(Max.class);
    descriptorBuilder.setMessage("{replace.in.user.bundle1}");
    descriptorBuilder.setAttribute("value", 10L);
    ConstraintAnnotationDescriptor<Max> descriptor = descriptorBuilder.build();
    ConstraintDescriptorImpl<Max> constraintDescriptor = new ConstraintDescriptorImpl<Max>(ConstraintHelper.forAllBuiltinConstraints(), null, descriptorBuilder.build(), ConstraintLocationKind.FIELD);
    interpolator = new ResourceBundleMessageInterpolator(new TestResourceBundleLocator());
    MessageInterpolator.Context messageInterpolatorContext = createMessageInterpolatorContext(constraintDescriptor);
    String expected = "{replace.in.default.bundle2}";
    String actual = interpolator.interpolate(descriptor.getAnnotation().message(), messageInterpolatorContext);
    assertEquals(actual, expected, "Within default bundle replacement parameter evaluation should not be recursive!");
}
Also used : ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) ConstraintAnnotationDescriptor(org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor) Max(jakarta.validation.constraints.Max) ConstraintDescriptorImpl(org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl) ResourceBundleMessageInterpolator(org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator) MessageInterpolator(jakarta.validation.MessageInterpolator) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) TestForIssue(org.hibernate.validator.testutil.TestForIssue)

Aggregations

ConstraintDescriptorImpl (org.hibernate.validator.internal.metadata.descriptor.ConstraintDescriptorImpl)3 ConstraintAnnotationDescriptor (org.hibernate.validator.internal.util.annotation.ConstraintAnnotationDescriptor)3 MessageInterpolator (jakarta.validation.MessageInterpolator)2 Max (jakarta.validation.constraints.Max)2 ResourceBundleMessageInterpolator (org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator)2 TestForIssue (org.hibernate.validator.testutil.TestForIssue)2 BeforeTest (org.testng.annotations.BeforeTest)2 Test (org.testng.annotations.Test)2 ResultPath (graphql.execution.ResultPath)1 Path (jakarta.validation.Path)1 MessageInterpolatorContext (org.hibernate.validator.internal.engine.MessageInterpolatorContext)1