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
This commit is contained in:
Brian Clozel
2021-09-22 11:06:46 +02:00
parent 17ce7b4838
commit b830fe9f3a
2 changed files with 29 additions and 1 deletions

View File

@@ -74,7 +74,7 @@ public class ArgumentMethodArgumentResolver implements HandlerMethodArgumentReso
if (annotation.required()) {
throw new MissingArgumentException(name, parameter);
}
returnValue(rawValue, parameterType.getType());
return returnValue(rawValue, parameterType.getType());
}
if (CollectionFactory.isApproximableCollectionType(rawValue.getClass())) {

View File

@@ -96,6 +96,16 @@ class ArgumentMethodArgumentResolverTests {
assertThat(result).isNotNull().isInstanceOf(Long.class).isEqualTo(42L);
}
@Test
void shouldNotFailIfArgumentNotRequired() throws Exception {
Method findByKeywords = ClassUtils.getMethod(BookController.class, "findByKeywords", List.class);
String payload = "{ }";
DataFetchingEnvironment environment = initEnvironment(payload);
MethodParameter methodParameter = getMethodParameter(findByKeywords, 0);
Object result = resolver.resolveArgument(methodParameter, environment);
assertThat(result).isNull();
}
@Test
void shouldResolveListOfJavaBeansArgument() throws Exception {
Method addBooks = ClassUtils.getMethod(BookController.class, "addBooks", List.class);
@@ -137,6 +147,11 @@ class ArgumentMethodArgumentResolverTests {
return null;
}
@QueryMapping
public Book findByKeywords(@Argument(required = false) List<Keyword> keywords) {
return null;
}
@MutationMapping
public Book addBook(@Argument BookInput bookInput) {
return null;
@@ -172,4 +187,17 @@ class ArgumentMethodArgumentResolverTests {
}
}
static class Keyword {
String term;
public String getTerm() {
return this.term;
}
public void setTerm(String term) {
this.term = term;
}
}
}