Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with message expressions in hibernate validator
    text
    copied!<p>I'm working on a project and I've been using <em>Hibernate Validator</em> for a while. Recently I tried to make use of <a href="http://docs.jboss.org/hibernate/validator/5.0/reference/en-US/html/chapter-message-interpolation.html#section-interpolation-with-message-expressions" rel="nofollow">Interpolation with message expressions</a> (with EL features) in my validation messages, but I get inconsistent results (to my knowledge).</p> <p>According to the documentation on message <em>interpolation steps</em>, message parameters (those enclosed in "<code>{..}</code>") are resolved prior to evaluation of message expressions (those enclosed in "<code>${..}</code>"). Hence, I conclude that it is allowed to write a message like this:</p> <pre><code> ${validatedValue &gt; someValue ? '{x.y.z.message}' : '{x.y.w.message}'} </code></pre> <p>and expect that a message literal whose key is either <code>x.y.z.message</code> or <code>x.y.w.message</code> be the result, where <code>x.y.z.message</code> and <code>x.y.w.message</code> are assumed to be valid and already defined message keys in a <em>properly bootstrapped resource bundle</em>.</p> <p>To clarify things out, I've written a test case using TestNG and ran it against <strong>hibernate-validator-5.0.1.Final</strong> and <strong>hibernate-validator-5.1.0-Alpha</strong>. As is pointed out in test comments, the results obtained by these releases vary themselves on this specific issue which hints me towards an implementation bug. I first thought this might have to do with <a href="https://hibernate.atlassian.net/browse/HV-798" rel="nofollow">[HV-798]</a> but it seems deeper.</p> <p>Here is the test case:</p> <pre><code>import java.util.Set; import javax.validation.ConstraintViolation; import javax.validation.Validator; import javax.validation.constraints.Size; import org.testng.annotations.Test; import static org.testng.Assert.assertEquals; public class HibernateValidationTest { public class Entity { public int caseIndex; @Size(min = 4, max = 10, message = "${validatedValue.length() &lt; min ? 'Error msg.#1' : 'Error msg.#2'}") public String getMessage1() { if (caseIndex == 0) return "abc"; return "abcd"; } @Size(min = 4, max = 10, message = "{javax.validation.constraints.Size.message}") public String getMessage2() { if (caseIndex == 1) return "abc"; return "abcd"; } @Size(min = 4, max = 10, message = "{javax.validation.constraints.Size.message} (${validatedValue.length() &lt; min ? 'Error msg.#3' : '{javax.validation.constraints.Size.message}'})") public String getMessage3() { if (caseIndex == 2) return "abc"; return "abcd"; } } @Test public void testValidatorBug() { Validator validator = javax.validation.Validation.buildDefaultValidatorFactory().getValidator(); Set&lt;ConstraintViolation&lt;Entity&gt;&gt; violations; ConstraintViolation&lt;Entity&gt; violation; Entity entity = new Entity(); /** * PASS */ entity.caseIndex = 0; violations = validator.validate(entity); assertEquals(violations.size(), 1); violation = violations.iterator().next(); assertEquals(violation.getMessage(), "Error msg.#1"); /** * PASS */ entity.caseIndex = 1; violations = validator.validate(entity); assertEquals(violations.size(), 1); violation = violations.iterator().next(); assertEquals(violation.getMessage(), "size must be between 4 and 10"); /** * FAIL * * Violation message: * * - on version hibernate-validator-5.0.1.Final: * size must be between 4 and 10 (${validatedValue.length() &lt; min ? 'Error msg.#3' : '{javax.validation.constraints.Size.message}'}) * * - on hibernate-validator-5.1.0-20131114.015215-37: * {javax.validation.constraints.Size.message} (${validatedValue.length() &lt; min ? 'Error msg.#3' : '{javax.validation.constraints.Size.message}'}) */ entity.caseIndex = 2; violations = validator.validate(entity); assertEquals(violations.size(), 1); violation = violations.iterator().next(); assertEquals(violation.getMessage(), "size must be between 4 and 10 (Error msg.#3)"); } } </code></pre> <p>In this test <code>Entity.getMessage3()</code> demonstrates the issue which is caught by last part of the test. Based on my understanding, this message must be <code>size must be between 4 and 10 (Error msg.#3)</code>, but it is <code>size must be between 4 and 10 (${validatedValue.length() &lt; min ? 'Error msg.#3' : '{javax.validation.constraints.Size.message}'})</code> on version <strong>5.0.1</strong> and <code>{javax.validation.constraints.Size.message} (${validatedValue.length() &lt; min ? 'Error msg.#3' : '{javax.validation.constraints.Size.message}'})</code> on <strong>5.1.0.Alpha</strong>.</p> <p>Can anyone kindly evaluate this and assert the issue or help me find my own mistake.</p> <p>Thank you for your time.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload