Skip to content

Commit b830fe9

Browse files
committed
Fix NPE for non-required missing input arguments
Prior to this commit, a missing, non-required input argument would throw an NullPointerException instead of returning `null` or `Optional.empty()`. Fixes gh-144
1 parent 17ce7b4 commit b830fe9

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

spring-graphql/src/main/java/org/springframework/graphql/data/method/annotation/support/ArgumentMethodArgumentResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public Object resolveArgument(MethodParameter parameter, DataFetchingEnvironment
7474
if (annotation.required()) {
7575
throw new MissingArgumentException(name, parameter);
7676
}
77-
returnValue(rawValue, parameterType.getType());
77+
return returnValue(rawValue, parameterType.getType());
7878
}
7979

8080
if (CollectionFactory.isApproximableCollectionType(rawValue.getClass())) {

spring-graphql/src/test/java/org/springframework/graphql/data/method/annotation/support/ArgumentMethodArgumentResolverTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,16 @@ void shouldResolveDefaultValue() throws Exception {
9696
assertThat(result).isNotNull().isInstanceOf(Long.class).isEqualTo(42L);
9797
}
9898

99+
@Test
100+
void shouldNotFailIfArgumentNotRequired() throws Exception {
101+
Method findByKeywords = ClassUtils.getMethod(BookController.class, "findByKeywords", List.class);
102+
String payload = "{ }";
103+
DataFetchingEnvironment environment = initEnvironment(payload);
104+
MethodParameter methodParameter = getMethodParameter(findByKeywords, 0);
105+
Object result = resolver.resolveArgument(methodParameter, environment);
106+
assertThat(result).isNull();
107+
}
108+
99109
@Test
100110
void shouldResolveListOfJavaBeansArgument() throws Exception {
101111
Method addBooks = ClassUtils.getMethod(BookController.class, "addBooks", List.class);
@@ -137,6 +147,11 @@ public Book findWithDefault(@Argument(defaultValue = "42") Long id) {
137147
return null;
138148
}
139149

150+
@QueryMapping
151+
public Book findByKeywords(@Argument(required = false) List<Keyword> keywords) {
152+
return null;
153+
}
154+
140155
@MutationMapping
141156
public Book addBook(@Argument BookInput bookInput) {
142157
return null;
@@ -172,4 +187,17 @@ public void setAuthorId(Long authorId) {
172187
}
173188
}
174189

190+
static class Keyword {
191+
192+
String term;
193+
194+
public String getTerm() {
195+
return this.term;
196+
}
197+
198+
public void setTerm(String term) {
199+
this.term = term;
200+
}
201+
}
202+
175203
}

0 commit comments

Comments
 (0)