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:
Koen Punt
2023-10-23 19:24:31 +02:00
committed by rstoyanchev
parent 2a99c1ffa0
commit 966276c01b
2 changed files with 13 additions and 1 deletions

View File

@@ -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());
}
}

View File

@@ -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