Skip to content

Commit 89da0ca

Browse files
committed
fix(core): ListNode.addAll(ListNode.value()) generates a ConcurrentModificationException
1 parent 4bb6082 commit 89da0ca

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

core/src/main/java/com/github/siroshun09/configapi/core/node/ListNodeImpl.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,11 @@ public void add(@Nullable Object value) {
9494

9595
@Override
9696
public void addAll(@NotNull Collection<?> collection) {
97-
for (var element : collection) {
97+
if (collection.isEmpty()) {
98+
return;
99+
}
100+
101+
for (var element : collection.toArray()) { // We need to create an array to prevent ConcurrentModificationException
98102
this.backing.add(Node.fromObject(element));
99103
}
100104
}

core/src/test/java/com/github/siroshun09/configapi/core/node/ListNodeTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,18 @@ protected Stream<NodeTestCase<ListNode>> nodeTestCases() {
107107
},
108108
(initial, modified) -> assertEquals(List.of(StringValue.fromString("a"), new IntValue(1), BooleanValue.TRUE), modified.value())
109109
),
110+
nodeTest(
111+
"ListNode#addAll(Collection) by ListNode#value",
112+
ListNode.create(List.of("a", "b", "c")),
113+
node -> {
114+
node.addAll(node.value());
115+
return node;
116+
},
117+
(initial, modified) -> {
118+
assertEquals(6, modified.size());
119+
assertEquals(List.of("a", "b", "c", "a", "b", "c"), modified.asList(String.class));
120+
}
121+
),
110122
nodeTest(
111123
"ListNode#addAll(ListNode)",
112124
ListNode.create(List.of("a", "b", "c")),

0 commit comments

Comments
 (0)