-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: Issue with message expressions in hibernate validator
PostPosted: Sun Nov 17, 2013 10:41 am 
Newbie

Joined: Sun Nov 17, 2013 10:34 am
Posts: 2
Hello every one, I've already asked this question on StackOverflow
Code:
http://stackoverflow.com/questions/20029058/issue-with-message-expressions-in-hibernate-validator

I repeated it here hopefully for some answers.

I'm working on a project and I've been using Hibernate Validator for a while. Recently I tried to make use of Interpolation with message expressions (with EL features) in my validation messages, but I get inconsistent results (to my knowledge).

According to the documentation on message interpolation steps, message parameters (those enclosed in "{..}") are resolved prior to evaluation of message expressions (those enclosed in "${..}"). Hence, I conclude that it is allowed to write a message like this:
Code:
    ${validatedValue > someValue ? '{x.y.z.message}' : '{x.y.w.message}'}

and expect that a message literal whose key is either x.y.z.message or x.y.w.message be the result, where x.y.z.message and x.y.w.message are assumed to be valid and already defined message keys in a properly bootstrapped resource bundle.

To clarify things out, I've written a test case using TestNG and ran it against hibernate-validator-5.0.1.Final and hibernate-validator-5.1.0-Alpha. 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 [HV-798] but it seems deeper.

Here is the test case:

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() < 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() < 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<ConstraintViolation<Entity>> violations;
        ConstraintViolation<Entity> 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() < 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() < 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)");
    }
}


In this test Entity.getMessage3() demonstrates the issue which is caught by last part of the test. Based on my understanding, this message must be size must be between 4 and 10 (Error msg.#3), but it is size must be between 4 and 10 (${validatedValue.length() < min ? 'Error msg.#3' : '{javax.validation.constraints.Size.message}'}) on version 5.0.1 and {javax.validation.constraints.Size.message} (${validatedValue.length() < min ? 'Error msg.#3' : '{javax.validation.constraints.Size.message}'}) on 5.1.0.Alpha.

Can anyone kindly evaluate this and assert the issue or help me find my own mistake.

Thank you for your time.


Top
 Profile  
 
 Post subject: Re: Issue with message expressions in hibernate validator
PostPosted: Mon Nov 18, 2013 4:47 am 
Hibernate Team
Hibernate Team

Joined: Sat Jan 24, 2009 12:46 pm
Posts: 388
I've added an answer to your original SO question.

--Gunnar

_________________
Visit my blog at http://musingsofaprogrammingaddict.blogspot.com/


Top
 Profile  
 
 Post subject: Re: Issue with message expressions in hibernate validator
PostPosted: Mon Nov 18, 2013 9:12 am 
Newbie

Joined: Sun Nov 17, 2013 10:34 am
Posts: 2
Thanks, I think I pinpointed the issue.
I added a comment under your SO answer.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.