A single opening or closing brace inside a character class in a {{regexp}} of a {{@Pattern}} leads to a {{MessageDescriptorFormatException}}, which is caught and swallowed with a warning in {{ResourceBundleMessageInterpolator}}. Since the interpolation is aborted there, instead of a message the message template is returned, e.g. {{"\{javax.validation.constraints.Pattern.message\}"}}.
For an opening brace, the warning is:
{code}WARN: HV000169: The message descriptor 'must match "[{0-9]{1,160}"' has nested parameters. {code}
For a closing brace:
{code}WARN: HV000168: The message descriptor 'must match "[}0-9]{1,160}"' contains an unbalanced meta character '}' parameter. {code}
This was still working in 5.0.0.Final, as bundled with GlassFish 4.1. I ran into the bug with Payara 4.1.1.171.0.1, that's why I put 5.1.2.Final as affected version, but I also verified it still happens with 5.4.0.Final. I suspect it may have to do with https://hibernate.atlassian.net/browse/ HV-798.
A test case:
{code} import static org.junit.Assert.assertNotEquals;
import java.util.Set;
import javax.validation.Configuration; import javax.validation.ConstraintViolation; import javax.validation.Validation; import javax.validation.Validator; import javax.validation.ValidatorFactory; import javax.validation.constraints.Pattern;
import org.junit.Test;
public class PatternInterpolationTest { @Test public void shouldInterpolateRegexWithSingleBraceInCharacterClass() throws Exception { Configuration<?> config = Validation.byDefaultProvider().configure();
ValidatorFactory factory = config.buildValidatorFactory(); Validator validator = factory.getValidator();
Entity invalidContent = new Entity(); invalidContent.field = "ABC";
Set<ConstraintViolation<Entity>> violations = validator.validate( invalidContent );
for ( ConstraintViolation<Entity> violation : violations ) { assertNotEquals( "{javax.validation.constraints.Pattern.message}", violation.getMessage() ); } }
private class Entity { @Pattern( regexp = "[{0-9]{1,160}" ) private String field; } } {code} |
|