Polishing contribution
Closes gh-33150
This commit is contained in:
@@ -50,7 +50,6 @@ import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ParameterNameDiscoverer;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.lang.Nullable;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.function.SingletonSupplier;
|
||||
import org.springframework.validation.BeanPropertyBindingResult;
|
||||
import org.springframework.validation.BindingResult;
|
||||
@@ -321,7 +320,7 @@ public class MethodValidationAdapter implements MethodValidator {
|
||||
|
||||
Object arg = argumentFunction.apply(parameter.getParameterIndex());
|
||||
|
||||
// If the arg is a container, we need to element, but the only way to extract it
|
||||
// If the arg is a container, we need the element, but the only way to extract it
|
||||
// is to check for and use a container index or key on the next node:
|
||||
// https://github.com/jakartaee/validation/issues/194
|
||||
|
||||
@@ -346,16 +345,16 @@ public class MethodValidationAdapter implements MethodValidator {
|
||||
value = map.get(key);
|
||||
container = map;
|
||||
}
|
||||
else if (arg instanceof Set<?>) {
|
||||
else if (arg instanceof Iterable<?>) {
|
||||
// No index or key, cannot access the specific value
|
||||
value = arg;
|
||||
container = null;
|
||||
container = arg;
|
||||
}
|
||||
else if (arg instanceof Optional<?> optional) {
|
||||
value = optional.orElse(null);
|
||||
container = optional;
|
||||
}
|
||||
else {
|
||||
Assert.state(!node.isInIterable(), "No way to unwrap Iterable without index");
|
||||
value = arg;
|
||||
container = null;
|
||||
}
|
||||
|
||||
@@ -40,7 +40,6 @@ import org.springframework.validation.method.ParameterErrors;
|
||||
import org.springframework.validation.method.ParameterValidationResult;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.InstanceOfAssertFactories.SET;
|
||||
|
||||
/**
|
||||
* Tests for {@link MethodValidationAdapter}.
|
||||
@@ -203,9 +202,7 @@ class MethodValidationAdapterTests {
|
||||
Method method = getMethod(target, "addHobbies");
|
||||
|
||||
testArgs(target, method, new Object[] {List.of(" ")}, ex -> {
|
||||
|
||||
assertThat(ex.getAllValidationResults()).hasSize(1);
|
||||
|
||||
assertValueResult(ex.getValueResults().get(0), 0, " ", List.of("""
|
||||
org.springframework.context.support.DefaultMessageSourceResolvable: \
|
||||
codes [NotBlank.myService#addHobbies.hobbies,NotBlank.hobbies,NotBlank.java.util.List,NotBlank]; \
|
||||
@@ -215,15 +212,13 @@ class MethodValidationAdapterTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
@Test // gh-33150
|
||||
void validateValueSetArgument() {
|
||||
MyService target = new MyService();
|
||||
Method method = getMethod(target, "addUniqueHobbies");
|
||||
|
||||
testArgs(target, method, new Object[] {Set.of("test", " ")}, ex -> {
|
||||
|
||||
assertThat(ex.getAllValidationResults()).hasSize(1);
|
||||
|
||||
assertValueResult(ex.getValueResults().get(0), 0, Set.of("test", " "), List.of("""
|
||||
org.springframework.context.support.DefaultMessageSourceResolvable: \
|
||||
codes [NotBlank.myService#addUniqueHobbies.hobbies,NotBlank.hobbies,NotBlank.java.util.Set,NotBlank]; \
|
||||
|
||||
Reference in New Issue
Block a user