Skip to content

Commit 4718347

Browse files
committed
Use target collection type for binding
This commit ensures that the `GraphQlArgumentBinder` is using the target collection type (on the type we're binding to) to create a new collection instance when using constructor binding. Fixes gh-485
1 parent c12b46f commit 4718347

File tree

2 files changed

+44
-1
lines changed

2 files changed

+44
-1
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ private <T> Collection<T> createCollection(
209209
return Collections.emptyList();
210210
}
211211

212-
Collection<T> collection = CollectionFactory.createApproximateCollection(rawCollection, rawCollection.size());
212+
Collection<T> collection = CollectionFactory.createCollection(collectionType.getRawClass(), elementClass, rawCollection.size());
213213
int i = 0;
214214
for (Object rawValue : rawCollection) {
215215
segments.push("[" + i++ + "]");

spring-graphql/src/test/java/org/springframework/graphql/data/GraphQlArgumentBinderTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
import java.util.ArrayList;
2020
import java.util.Collections;
2121
import java.util.HashMap;
22+
import java.util.HashSet;
2223
import java.util.List;
2324
import java.util.Map;
25+
import java.util.Objects;
26+
import java.util.Set;
2427
import java.util.stream.Collectors;
2528
import java.util.stream.IntStream;
2629
import java.util.stream.Stream;
@@ -268,6 +271,16 @@ void shouldHaveHigherDefaultAutoGrowLimit() throws Exception {
268271
assertThat(((ItemListHolder) result).getItems()).hasSize(260);
269272
}
270273

274+
@Test
275+
void shouldUseTargetCollectionType() throws Exception {
276+
String items = IntStream.range(0, 5).mapToObj(value -> "{\"name\":\"test" + value + "\"}").collect(Collectors.joining(","));
277+
Object result = this.binder.bind(
278+
environment("{\"key\":{\"items\":[" + items + "]}}"), "key",
279+
ResolvableType.forClass(ItemSetHolder.class));
280+
assertThat(result).isNotNull().isInstanceOf(ItemSetHolder.class);
281+
assertThat(((ItemSetHolder) result).getItems()).hasSize(5);
282+
}
283+
271284

272285
@SuppressWarnings("unchecked")
273286
private DataFetchingEnvironment environment(String jsonPayload) throws JsonProcessingException {
@@ -401,6 +414,36 @@ public int getAge() {
401414
public void setAge(int age) {
402415
this.age = age;
403416
}
417+
418+
@Override
419+
public boolean equals(Object o) {
420+
if (this == o) return true;
421+
if (o == null || getClass() != o.getClass()) return false;
422+
Item item = (Item) o;
423+
return name.equals(item.name);
424+
}
425+
426+
@Override
427+
public int hashCode() {
428+
return Objects.hash(name);
429+
}
430+
}
431+
432+
static class ItemSetHolder {
433+
434+
private Set<Item> items;
435+
436+
public ItemSetHolder(Set<Item> items) {
437+
this.items = items;
438+
}
439+
440+
public Set<Item> getItems() {
441+
return items;
442+
}
443+
444+
public void setItems(Set<Item> items) {
445+
this.items = items;
446+
}
404447
}
405448

406449
}

0 commit comments

Comments
 (0)