| 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 of a nested index. 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-TBD")
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" )
);
}
|