Skip to content

Commit 966276c

Browse files
koenpuntrstoyanchev
authored andcommitted
Allow null arguments in ValueExtractor
`isPresent()` returns false, even when an explicit `null` value is supplied. By checking `isOmitted()` instead, we can correctly use validation annotations like `@NotBlank`. Closes gh-842
1 parent 2a99c1f commit 966276c

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public final class ArgumentValueValueExtractor implements ValueExtractor<Argumen
3232

3333
@Override
3434
public void extractValues(ArgumentValue<?> argumentValue, ValueReceiver receiver) {
35-
if (argumentValue.isPresent()) {
35+
if (!argumentValue.isOmitted()) {
3636
receiver.value(null, argumentValue.value());
3737
}
3838
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ void shouldRaiseValidationErrorForAnnotatedParams() {
7373

7474
BiConsumer<Object, Object[]> validator3 = validateFunction(MyBean.class, "myValidArgumentValue");
7575
assertViolation(() -> validator3.accept(bean, new Object[] {ArgumentValue.ofNullable("")}), "myValidArgumentValue.arg0");
76+
77+
// Validate that an explicit null value is validated.
78+
assertViolation(() -> validator3.accept(bean, new Object[] {ArgumentValue.ofNullable(null)}), "myValidArgumentValue.arg0");
79+
}
80+
81+
@Test
82+
void shouldNotRaiseValidationErrorForOmittedArgumentValue() {
83+
MyBean bean = new MyBean();
84+
85+
// Validate that an omitted value is allowed.
86+
BiConsumer<Object, Object[]> validator3 = validateFunction(MyBean.class, "myValidArgumentValue");
87+
validator3.accept(bean, new Object[] {ArgumentValue.omitted()});
7688
}
7789

7890
@Test

0 commit comments

Comments
 (0)