Skip to content

Commit

Permalink
HV-1774 Do not interpret '$\A{1+1}' in message templates
Browse files Browse the repository at this point in the history
  • Loading branch information
yrodiere authored and gsmet committed May 6, 2020
1 parent df2fe2c commit 29bd0f4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,11 @@ public void handleEndTerm(char character, TokenCollector tokenCollector) throws
@Override
public void handleEscapeCharacter(char character, TokenCollector tokenCollector)
throws MessageDescriptorFormatException {
tokenCollector.transitionState( new EscapedState( this ) );
tokenCollector.appendToToken( EL_DESIGNATOR );
tokenCollector.appendToToken( character );
// Do not go back to this state after the escape: $\ is not the start of an EL expression
ParserState stateAfterEscape = new MessageState();
tokenCollector.transitionState( new EscapedState( stateAfterEscape ) );
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@

import org.hibernate.validator.internal.engine.messageinterpolation.InterpolationTermType;
import org.hibernate.validator.internal.engine.messageinterpolation.parser.MessageDescriptorFormatException;
import org.hibernate.validator.internal.engine.messageinterpolation.parser.Token;
import org.hibernate.validator.internal.engine.messageinterpolation.parser.TokenCollector;

import org.assertj.core.api.Assertions;
import org.assertj.core.api.ListAssert;
import org.testng.annotations.Test;

/**
Expand All @@ -29,10 +33,25 @@ public void testParameterWithoutOpeningBraceThrowsException() throws Exception {
}

@Test(expectedExceptions = MessageDescriptorFormatException.class, expectedExceptionsMessageRegExp = "HV000168.*")
public void testELExpressionWithoutOpeningBraceThrowsException() throws Exception {
public void testELExpressionDollarThenClosingBraceThrowsException() throws Exception {
new TokenCollector( "$}", InterpolationTermType.EL );
}

@Test
public void testELExpressionDollarThenEscapeInterpretedAsLiterals() {
ListAssert<Token> assertion = Assertions.assertThat(
new TokenCollector( "$\\A{1+1}", InterpolationTermType.EL )
.getTokenList()
)
.hasSize( 2 );
assertion.element( 0 )
.returns( "$\\A", Token::getTokenValue )
.returns( false, Token::isParameter );
assertion.element( 1 )
.returns( "{1+1}", Token::getTokenValue )
.returns( false, Token::isParameter );
}

@Test(expectedExceptions = MessageDescriptorFormatException.class, expectedExceptionsMessageRegExp = "HV000168.*")
public void testTermWithoutClosingBraceThrowsException() throws Exception {
new TokenCollector( "{foo", InterpolationTermType.PARAMETER );
Expand Down

0 comments on commit 29bd0f4

Please sign in to comment.