diff --git a/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java b/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java index a5b9686c..5adb3512 100644 --- a/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java +++ b/spring-graphql/src/main/java/org/springframework/graphql/data/GraphQlArgumentBinder.java @@ -209,7 +209,7 @@ public class GraphQlArgumentBinder { return Collections.emptyList(); } - Collection collection = CollectionFactory.createApproximateCollection(rawCollection, rawCollection.size()); + Collection collection = CollectionFactory.createCollection(collectionType.getRawClass(), elementClass, rawCollection.size()); int i = 0; for (Object rawValue : rawCollection) { segments.push("[" + i++ + "]"); diff --git a/spring-graphql/src/test/java/org/springframework/graphql/data/GraphQlArgumentBinderTests.java b/spring-graphql/src/test/java/org/springframework/graphql/data/GraphQlArgumentBinderTests.java index 394920ff..b9862ff8 100644 --- a/spring-graphql/src/test/java/org/springframework/graphql/data/GraphQlArgumentBinderTests.java +++ b/spring-graphql/src/test/java/org/springframework/graphql/data/GraphQlArgumentBinderTests.java @@ -19,8 +19,11 @@ package org.springframework.graphql.data; import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Objects; +import java.util.Set; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -268,6 +271,16 @@ class GraphQlArgumentBinderTests { assertThat(((ItemListHolder) result).getItems()).hasSize(260); } + @Test + void shouldUseTargetCollectionType() throws Exception { + String items = IntStream.range(0, 5).mapToObj(value -> "{\"name\":\"test" + value + "\"}").collect(Collectors.joining(",")); + Object result = this.binder.bind( + environment("{\"key\":{\"items\":[" + items + "]}}"), "key", + ResolvableType.forClass(ItemSetHolder.class)); + assertThat(result).isNotNull().isInstanceOf(ItemSetHolder.class); + assertThat(((ItemSetHolder) result).getItems()).hasSize(5); + } + @SuppressWarnings("unchecked") private DataFetchingEnvironment environment(String jsonPayload) throws JsonProcessingException { @@ -401,6 +414,36 @@ class GraphQlArgumentBinderTests { public void setAge(int age) { this.age = age; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Item item = (Item) o; + return name.equals(item.name); + } + + @Override + public int hashCode() { + return Objects.hash(name); + } + } + + static class ItemSetHolder { + + private Set items; + + public ItemSetHolder(Set items) { + this.items = items; + } + + public Set getItems() { + return items; + } + + public void setItems(Set items) { + this.items = items; + } } } \ No newline at end of file