Uploaded image for project: 'Hibernate Validator'
  1. HV-798

Escaping validator messages doesn't work

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 4.3.1.Final, 5.0.1.Final
    • Fix Version/s: 5.1.0.Alpha1
    • Component/s: None
    • Labels:
      None
    • Bug Testcase Reminder (view):

      Bug reports should generally be accompanied by a test case!

    • Last commented by a user?:
      true
    • Sprint:

      Description

      as described in http://stackoverflow.com/questions/16665649/how-to-escape-curly-braces-in-hibernate-validator-messages-test-case-provided.

      HV should allow to escape validation messages from user provided bundles.

      a test case:

      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;
      }
      }
      

      that results in stack overflow as it tries to constantly substitute escaped variable

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: