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
This commit is contained in:
@@ -32,7 +32,7 @@ public final class ArgumentValueValueExtractor implements ValueExtractor<Argumen
|
||||
|
||||
@Override
|
||||
public void extractValues(ArgumentValue<?> argumentValue, ValueReceiver receiver) {
|
||||
if (argumentValue.isPresent()) {
|
||||
if (!argumentValue.isOmitted()) {
|
||||
receiver.value(null, argumentValue.value());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,6 +73,18 @@ class ValidationHelperTests {
|
||||
|
||||
BiConsumer<Object, Object[]> validator3 = validateFunction(MyBean.class, "myValidArgumentValue");
|
||||
assertViolation(() -> validator3.accept(bean, new Object[] {ArgumentValue.ofNullable("")}), "myValidArgumentValue.arg0");
|
||||
|
||||
// Validate that an explicit null value is validated.
|
||||
assertViolation(() -> validator3.accept(bean, new Object[] {ArgumentValue.ofNullable(null)}), "myValidArgumentValue.arg0");
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotRaiseValidationErrorForOmittedArgumentValue() {
|
||||
MyBean bean = new MyBean();
|
||||
|
||||
// Validate that an omitted value is allowed.
|
||||
BiConsumer<Object, Object[]> validator3 = validateFunction(MyBean.class, "myValidArgumentValue");
|
||||
validator3.accept(bean, new Object[] {ArgumentValue.omitted()});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user