Fix NPE when GraphQL argument is list with null

See gh-486
This commit is contained in:
Alexander Zhuravlev
2022-09-09 00:47:49 +03:00
committed by rstoyanchev
parent 17f6a7c103
commit 65facf41f3
2 changed files with 40 additions and 1 deletions

View File

@@ -213,7 +213,7 @@ public class GraphQlArgumentBinder {
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) {

View File

@@ -32,6 +32,7 @@ import com.fasterxml.jackson.core.JsonProcessingException;
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;
@@ -281,6 +282,44 @@ class GraphQlArgumentBinderTests {
assertThat(((ItemSetHolder) result).getItems()).hasSize(5);
}
@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);
new CollectionAssert<>((List<String>) 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);
new CollectionAssert<>((List<String>) 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);
new CollectionAssert<>((List<String>) result).isEmpty();
}
@SuppressWarnings("unchecked")
private DataFetchingEnvironment environment(String jsonPayload) throws JsonProcessingException {