We're updating the issue view to help you get more done. 

Type parameter validations are not executed against all elements in a collection if elements contain the same object instance.

Description

Type parameter validations are not executed against all elements in a collection if elements contain the same object instance.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 /** 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 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 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()); }

Environment

java 8 66

Status

Assignee

Gunnar Morling

Reporter

Jeff Maxwell

Labels

None

Worked in

None

Feedback Requested

None

Feedback Requested By

None

backPortable

None

Community Help Wanted

None

Suitable for new contributors

None

Requires Release Note

None

backportDecision

None

backportReEvaluate

None

Components

Fix versions

Affects versions

5.2.2.Final

Priority

Major