Uploaded image for project: 'Hibernate Validator'
  1. HV-1614

Unable to specify constraints at more than 1 nested parameter of a typed container

    Details

    • Type: Bug
    • Status: Closed
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: 6.0.9.Final
    • Fix Version/s: 6.0.10.Final
    • Component/s: engine
    • Labels:
      None
    • Environment:
      java 8 66
      eclipse 4.6.1
      windows 7

      Description

      I am attempting to build a nested constraint mapping for a container using the containerElementType method and I noticed that I am only able to create constraints at 1 nested parameter of a typed container.

      Tracing through the code I think the hashmap put at line 137 of CascadableConstraintMappingContextImplBase is allowing only 1 nested constraint value to be kept because it is keying on index which is 1 in both cases.

         containerElementContexts.put( index, containerElementContext ); 
      

      I was able to reproduce the issue with a small modification to the canDeclareDeeplyNestedContainerElementConstraintsForFieldProgrammatically method of ProgrammaticContainerElementConstraintsForFieldTest that adds a size constraint to the list of fish. I noticed that if i flip the order i declare the constraints it affects which violation makes it through and which is thrown out.

      Here is a test case to reproduce this issue. It leverages the FishTank type from ProgrammaticContainerElementConstraintsForFieldTest

          @Test
          @TestForIssue(jiraKey = "HV-1614")
          public void canDeclareMultipleDeeplyNestedContainerElementConstraintsForFieldProgrammatically() {
              ConstraintMapping newMapping = config.createConstraintMapping();
              newMapping
                  .type( FishTank.class )
                      .property( "fishOfTheMonth", FIELD )
                      .containerElementType( 1 )
                         .constraint(new SizeDef().min( 3 ).message( "there must be at least {min} fish of the month" ))
                      .containerElementType( 1, 0 )
                         .constraint( new NotNullDef() );
      
              config.addMapping( newMapping );
              Validator validator = config.buildValidatorFactory().getValidator();
      
              Set<ConstraintViolation<FishTank>> violations = validator.validate( new FishTank() );
             
              assertThat( violations ).containsOnlyViolations(
                      violationOf( NotNull.class ).withMessage( "must not be null" ),
                      violationOf( Size.class ).withMessage( "there must be at least 3 fish of the month" )
              );
          }	
      

        Attachments

          Activity

            People

            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Stride room