Skip to content

Commit 65facf4

Browse files
turboezhrstoyanchev
authored andcommitted
Fix NPE when GraphQL argument is list with null
See gh-486
1 parent 17f6a7c commit 65facf4

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-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
@@ -213,7 +213,7 @@ private <T> Collection<T> createCollection(
213213
int i = 0;
214214
for (Object rawValue : rawCollection) {
215215
segments.push("[" + i++ + "]");
216-
if (elementClass.isAssignableFrom(rawValue.getClass())) {
216+
if (rawValue == null || elementClass.isAssignableFrom(rawValue.getClass())) {
217217
collection.add((T) rawValue);
218218
}
219219
else if (rawValue instanceof Map) {

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

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.fasterxml.jackson.databind.ObjectMapper;
3333
import graphql.schema.DataFetchingEnvironment;
3434
import graphql.schema.DataFetchingEnvironmentImpl;
35+
import org.assertj.core.api.CollectionAssert;
3536
import org.junit.jupiter.api.Test;
3637

3738
import org.springframework.core.ResolvableType;
@@ -281,6 +282,44 @@ void shouldUseTargetCollectionType() throws Exception {
281282
assertThat(((ItemSetHolder) result).getItems()).hasSize(5);
282283
}
283284

285+
@Test
286+
@SuppressWarnings("unchecked")
287+
void list() throws Exception {
288+
Object result = this.binder.bind(
289+
environment("{\"key\": [\"1\", \"2\", \"3\"]}"),
290+
"key",
291+
ResolvableType.forClassWithGenerics(List.class, String.class)
292+
);
293+
294+
assertThat(result).isNotNull().isInstanceOf(List.class);
295+
new CollectionAssert<>((List<String>) result).containsExactly("1", "2", "3");
296+
}
297+
298+
@Test
299+
@SuppressWarnings("unchecked")
300+
void listWithNullItem() throws Exception {
301+
Object result = this.binder.bind(
302+
environment("{\"key\": [\"1\", null, \"3\"]}"),
303+
"key",
304+
ResolvableType.forClassWithGenerics(List.class, String.class)
305+
);
306+
307+
assertThat(result).isNotNull().isInstanceOf(List.class);
308+
new CollectionAssert<>((List<String>) result).containsExactly("1", null, "3");
309+
}
310+
311+
@Test
312+
@SuppressWarnings("unchecked")
313+
void emptyList() throws Exception {
314+
Object result = this.binder.bind(
315+
environment("{\"key\": []}"),
316+
"key",
317+
ResolvableType.forClassWithGenerics(List.class, String.class)
318+
);
319+
320+
assertThat(result).isNotNull().isInstanceOf(List.class);
321+
new CollectionAssert<>((List<String>) result).isEmpty();
322+
}
284323

285324
@SuppressWarnings("unchecked")
286325
private DataFetchingEnvironment environment(String jsonPayload) throws JsonProcessingException {

0 commit comments

Comments
 (0)