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 level nested parameter of a nested index typed container .
Tracing through the code I think the hashmap put at line 137 of [CascadableConstraintMappingContextImplBase | https://github.com/hibernate/hibernate-validator/blob/master/engine/src/main/java/org/hibernate/validator/internal/cfg/context/CascadableConstraintMappingContextImplBase.java] is allowing only 1 nested constraint value to be kept because it is keying on index which is 1 in both cases. {code} containerElementContexts.put( index, containerElementContext ); {code}
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 |https://github.com/hibernate/hibernate-validator/blob/master/engine/src/test/java/org/hibernate/validator/test/cfg/ProgrammaticContainerElementConstraintsForFieldTest.java] {code} @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" ) ); } {code} |
|