Skip to content

Commit

Permalink
🎨 List constructor arg initialized correctly
Browse files Browse the repository at this point in the history
DataBinder now uses the calculated List size rather than
the number of indexes to initialize the list.
  • Loading branch information
TAKETODAY committed Dec 31, 2024
1 parent 9b38234 commit 8c76e9a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
4 changes: 3 additions & 1 deletion today-context/src/main/java/infra/validation/DataBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,9 @@ private <V> List<V> createList(String paramPath, Class<?> paramType, ResolvableT
}
int size = (indexes.last() < this.autoGrowCollectionLimit ? indexes.last() + 1 : 0);
List<V> list = (List<V>) CollectionUtils.createCollection(paramType, size);
indexes.forEach(i -> list.add(null));
for (int i = 0; i < size; i++) {
list.add(null);
}
for (int index : indexes) {
list.set(index, (V) createObject(elementType, paramPath + "[" + index + "].", valueResolver));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,24 @@ void listBinding() {
assertThat(list.get(2).param1()).isEqualTo("value3");
}

@Test
void listBindingWithNonconsecutiveIndices() {
MapValueResolver valueResolver = new MapValueResolver(Map.of(
"dataClassList[0].param1", "value1", "dataClassList[0].param2", "true",
"dataClassList[1].param1", "value2", "dataClassList[1].param2", "true",
"dataClassList[3].param1", "value3", "dataClassList[3].param2", "true"));

DataBinder binder = initDataBinder(ListDataClass.class);
binder.construct(valueResolver);

ListDataClass dataClass = getTarget(binder);
List<DataClass> list = dataClass.dataClassList();

assertThat(list.get(0).param1()).isEqualTo("value1");
assertThat(list.get(1).param1()).isEqualTo("value2");
assertThat(list.get(3).param1()).isEqualTo("value3");
}

@Test
void mapBinding() {
MapValueResolver valueResolver = new MapValueResolver(Map.of(
Expand Down

0 comments on commit 8c76e9a

Please sign in to comment.