Skip to content

Commit

Permalink
Fix NPE when GraphQL argument is list with null
Browse files Browse the repository at this point in the history
  • Loading branch information
turboezh authored and rstoyanchev committed Sep 9, 2022
1 parent 17f6a7c commit 65facf4
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ private <T> Collection<T> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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;
Expand Down Expand Up @@ -281,6 +282,44 @@ void shouldUseTargetCollectionType() throws Exception {
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 {
Expand Down

0 comments on commit 65facf4

Please sign in to comment.