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 a5b9686c6..63ef26e3a 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 @@ -213,7 +213,7 @@ private Collection createCollection( int i = 0; for (Object rawValue : rawCollection) { segments.push("[" + i++ + "]"); - if (elementClass.isAssignableFrom(rawValue.getClass())) { + if (rawValue == null || elementClass.isAssignableFrom(rawValue.getClass())) { collection.add((T) rawValue); } else if (rawValue instanceof Map) { 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 394920ffb..23fe3e78f 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 @@ -16,11 +16,7 @@ package org.springframework.graphql.data; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.stream.Stream; @@ -29,6 +25,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import graphql.schema.DataFetchingEnvironment; import graphql.schema.DataFetchingEnvironmentImpl; +import org.assertj.core.api.CollectionAssert; import org.junit.jupiter.api.Test; import org.springframework.core.ResolvableType; @@ -268,6 +265,44 @@ void shouldHaveHigherDefaultAutoGrowLimit() throws Exception { assertThat(((ItemListHolder) result).getItems()).hasSize(260); } + @Test + @SuppressWarnings("unchecked") + void list() throws Exception { + Object result = this.binder.bind( + environment("{\"key\": [\"1\", \"2\", \"3\"]}"), + "key", + ResolvableType.forClassWithGenerics(List.class, String.class) + ); + + assertThat(result).isNotNull().isInstanceOf(List.class); + CollectionAssert.assertThatCollection((List) result).containsExactly("1", "2", "3"); + } + + @Test + @SuppressWarnings("unchecked") + void listWithNullItem() throws Exception { + Object result = this.binder.bind( + environment("{\"key\": [\"1\", null, \"3\"]}"), + "key", + ResolvableType.forClassWithGenerics(List.class, String.class) + ); + + assertThat(result).isNotNull().isInstanceOf(List.class); + CollectionAssert.assertThatCollection((List) result).containsExactly("1", null, "3"); + } + + @Test + @SuppressWarnings("unchecked") + void emptyList() throws Exception { + Object result = this.binder.bind( + environment("{\"key\": []}"), + "key", + ResolvableType.forClassWithGenerics(List.class, String.class) + ); + + assertThat(result).isNotNull().isInstanceOf(List.class); + CollectionAssert.assertThatCollection((List) result).isEmpty(); + } @SuppressWarnings("unchecked") private DataFetchingEnvironment environment(String jsonPayload) throws JsonProcessingException {