Message interpolation fails with single opening or closing brace in regex character class

Description

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:

1 WARN: HV000169: The message descriptor 'must match "[{0-9]{1,160}"' has nested parameters.

For a closing brace:

1 WARN: HV000168: The message descriptor 'must match "[}0-9]{1,160}"' contains an unbalanced meta character '}' parameter.

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 HV-798.

A test case:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 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; } }

Environment

None

Status

Assignee

Guillaume Smet

Reporter

Joachim Kanbach

Labels

None

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

backportReEvaluate

None

Components

Fix versions

Affects versions

5.1.2.Final
5.4.0.Final

Priority

Minor