-->
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: Hibernate Validator 4.2 bug: node paths duplicated
PostPosted: Thu Apr 14, 2011 3:57 pm 
Newbie

Joined: Sun Feb 27, 2011 11:40 am
Posts: 8
Hi all, can someone please tell me if I'm doing something wrong or point me to the right forum?

This seems like a very serious bug in HV 4.2.0.Beta2. The code works as expected with HV 4.1.0.Final.

If my custom class-level validator uses context.buildConstraintViolationWithTemplate(..).addNode, then the node name becomes the prefix for all subsequent property paths.

Result from JUnit below:
Code:
junit.framework.AssertionFailedError: expected:<[field2, myNode, field1]> but was:<[myNode.field1, myNode, myNode.field2]>

(modulo HashSet randomization...another pet peeve of mine, why can't y'all use LinkedHashSet instead?)

Code:
public class CustomMessageTest extends Assert {

   @Retention(RetentionPolicy.RUNTIME)
   @Constraint(validatedBy=MyClassLevelValidation.Validator.class)
   public @interface MyClassLevelValidation {
      String message() default "failed";
      Class<?>[] groups() default {};
      Class<? extends Payload>[] payload() default {};

      public static class Validator implements ConstraintValidator<MyClassLevelValidation,MyObject> {
         @Override
         public void initialize(MyClassLevelValidation constraintAnnotation) {}

         @Override
         public boolean isValid(MyObject value, ConstraintValidatorContext context) {
            if (/* stuff */ true) {
               context.buildConstraintViolationWithTemplate(
                     context.getDefaultConstraintMessageTemplate())
                  .addNode("myNode") // <<< problem here: field1 becomes prefix for all other validations
                  .addConstraintViolation()
                  .disableDefaultConstraintViolation();
               return false;
            }
            return true;
         }
      }
   }

   @MyClassLevelValidation
   public static class MyObject {
      @NotNull // field level validation
      String field1;
      @NotNull
      String field2;
   }

   @Test
   public void testValidation() {
      Set<ConstraintViolation<MyObject>> violations =
         Validation.buildDefaultValidatorFactory()
            .getValidator()
            .validate(new MyObject());

      assertEquals(3, violations.size());
      Set<String> paths = new HashSet<String>();
      for (ConstraintViolation<MyObject> violation : violations) {
         paths.add(violation.getPropertyPath().toString());
      }

      assertEquals(new HashSet<String>(Arrays.asList("field1", "field2", "myNode")), paths);
   }
}


The only recent BVAL issue I can find that seems related is this:
http://opensource.atlassian.com/project ... e/BVTCK-12
Did that fix introduce this brokenness?


Top
 Profile  
 
 Post subject: Re: Hibernate Validator 4.2 bug: node paths duplicated
PostPosted: Fri Apr 15, 2011 2:42 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

HV was actually not behaving according to the spec. This is was addressed w/ BVTCK-12. So the way the path behaves has changed slightly.

http://opensource.atlassian.com/project ... wse/HV-452 got introduced a bug as part of BVTCK-12. It will be fixed though for the final release. I think that might be the issue you are seeing.

--Hardy


Top
 Profile  
 
 Post subject: Re: Hibernate Validator 4.2 bug: node paths duplicated
PostPosted: Fri Apr 15, 2011 9:40 am 
Newbie

Joined: Sun Feb 27, 2011 11:40 am
Posts: 8
Thanks Hardy, that's it. I'll just hold my breath until CR1 :)


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.