如何在hibernate验证器消息中转义花括号(提供测试用例)

时间:2013-05-21 08:43:40

标签: escaping bean-validation hibernate-validator

我希望将类似{this}的内容作为hibernate验证器验证消息的消息值。

所以我在属性文件中转义了这条给定消息的值:\ {this \}

然而,Hibrnate Validator会尝试检查是否有给定密钥的消息。 我认为这是一个错误,但也许我做错了什么? (使用hibernate-validator 4.3.1和5.0.1进行测试 - 结果相同 - 堆栈溢出

测试用例:

(为了简化它没有属性文件,我只是从java类返回值。实际上handleGetObject会从我自定义提供的属性中读取值):

import org.hibernate.validator.HibernateValidator;
import org.hibernate.validator.HibernateValidatorConfiguration;
import org.hibernate.validator.internal.engine.constraintvalidation.ConstraintValidatorFactoryImpl;
import org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator;
import org.hibernate.validator.spi.resourceloading.ResourceBundleLocator;
import org.junit.Test;

import javax.validation.*;
import javax.validation.constraints.Max;
import java.util.Enumeration;
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.Set;


public class HibValTest {

@Test
public void shouldNotSubstituteEscaped() throws Exception {

    MessageInterpolator interpolator = new ResourceBundleMessageInterpolator( new ResourceBundleLocator() {

        @Override
        public ResourceBundle getResourceBundle(Locale locale) {
            return new ResourceBundle() {

                @Override
                protected Object handleGetObject(String key) {
                    return "\\{escapedKey\\}";
                }

                @Override
                public Enumeration<String> getKeys() {
                    throw new UnsupportedOperationException();
                }
            };
        }
    }, false);

    HibernateValidatorConfiguration config = Validation.byProvider( HibernateValidator.class ).configure();
    config = config.messageInterpolator(interpolator).constraintValidatorFactory(new ConstraintValidatorFactoryImpl());

    ValidatorFactory factory = config.buildValidatorFactory();
    Validator validator = factory.getValidator();

    Set<ConstraintViolation<A>> validate = validator.validate(new A());
}

private class A {
    @Max(value=1, message = "{key}")
    private int a=2;
}
}

结果:

at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.resolveParameter(ResourceBundleMessageInterpolator.java:244)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateBundleMessage(ResourceBundleMessageInterpolator.java:211)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.resolveParameter(ResourceBundleMessageInterpolator.java:244)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateBundleMessage(ResourceBundleMessageInterpolator.java:211)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.resolveParameter(ResourceBundleMessageInterpolator.java:244)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateBundleMessage(ResourceBundleMessageInterpolator.java:211)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.resolveParameter(ResourceBundleMessageInterpolator.java:244)
at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.interpolateBundleMessage(ResourceBundleMessageInterpolator.java:211)

1 个答案:

答案 0 :(得分:0)

这看起来确实是一个错误。你能在我们的issue tracker创建一张票吗?

目前,您可以通过不为转义密钥提供值来解决此问题(如果在您的情况下可行),例如像这样:

protected Object handleGetObject(String key) {
    return key.equals("key") ? "\\{escapedKey\\}" : null;
}