|
|
|
|
|
Updated with List<String> example.
|
|
|
Change By:
|
Jeff Maxwell
|
|
Summary:
|
Type parameter validations are not executed against all entries elements in a map collection if elements contain the value is the same object instance across entries .
|
|
Type parameter validations are not executed against all entries elements in a map collection if elements contain the value is the same object instance across entries .
{code:java} /** The Constant VALIDATOR_FACTORY. */ static final ValidatorFactory VALIDATOR_FACTORY = Validation.byDefaultProvider() .configure() .buildValidatorFactory();
/** The Constant VALIDATOR. */ static final Validator VALIDATOR = VALIDATOR_FACTORY.getValidator();
public static class WithMap {
@Valid final private Map<String, @Size(min = 1) List<String>> lists;
public WithMap(Map<String, List<String>> lists) { super(); this.lists = lists; }
public Map<String, List<String>> getLists() { return this.lists; }
}
/** * Test fails as it does not consider the property path when checking if already validated. */ @Test public void testSameInstance testMapSameInstance () {
List<String> emptyList = new ArrayList<>();
String[] stringArray = { "A" };
List<String> populatedList = Arrays.asList(stringArray);
HashMap<String, List<String>> map = new HashMap<>(); map.put("POPULATED", populatedList); map.put("EMPTY_LIST1", emptyList); map.put("EMPTY_LIST2", emptyList);
WithMap withMap = new WithMap(map);
Set<ConstraintViolation<WithMap>> constraintViolations = VALIDATOR.validate(withMap);
// java.lang.AssertionError: expected:<2> but was:<1> assertEquals(2, constraintViolations.size()); }
/** * Test passes since the lists are different instances. */ @Test public void testDifferentInstance testMapDifferentInstance () {
String[] stringArray = { "A" };
List<String> populatedList = Arrays.asList(stringArray);
HashMap<String, List<String>> map = new HashMap<>(); map.put("POPULATED", populatedList); map.put("EMPTY_LIST1", new ArrayList<>()); map.put("EMPTY_LIST2", new ArrayList<>());
WithMap withMap = new WithMap(map);
Set<ConstraintViolation<WithMap>> constraintViolations = VALIDATOR.validate(withMap);
// Passes assertEquals(2, constraintViolations.size()); }
public static class WithList {
@Valid final private List<@Size(min = 1) String> list;
public WithList(List<String> list) { super(); this.list = list; }
public List<String> getLists() { return this.list; }
}
/** * Test fails as it does not consider the property path when checking if already validated. */ @Test public void testListSameInstance() {
String[] stringArray = { "", "A", "" };
List<String> populatedList = Arrays.asList(stringArray);
WithList withList = new WithList(populatedList);
Set<ConstraintViolation<WithList>> constraintViolations = VALIDATOR.validate(withList);
// java.lang.AssertionError: expected:<2> but was:<1> assertEquals(2, constraintViolations.size()); }
/** * Test passes since the lists are different instances. */ @Test public void testListDifferentInstance() {
String[] stringArray = { new String(""), "A", new String("") };
List<String> populatedList = Arrays.asList(stringArray);
WithList withList = new WithList(populatedList);
Set<ConstraintViolation<WithList>> constraintViolations = VALIDATOR.validate(withList);
assertEquals(2, constraintViolations.size()); }
{ code}
|
|
|
|
|
|