Skip to content

Commit

Permalink
Merge branch '6.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Jul 9, 2024
2 parents f91f791 + 3008d97 commit 9d45a8d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@
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;
Expand Down Expand Up @@ -321,7 +320,7 @@ else if (node.getKind().equals(ElementKind.RETURN_VALUE)) {

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

Expand All @@ -346,12 +345,16 @@ else if (key != null && arg instanceof Map<?, ?> map) {
value = map.get(key);
container = map;
}
else if (arg instanceof Iterable<?>) {
// No index or key, cannot access the specific value
value = arg;
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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.lang.reflect.Method;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.function.Consumer;

import jakarta.validation.Valid;
Expand Down Expand Up @@ -201,9 +202,7 @@ void validateValueListArgument() {
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]; \
Expand All @@ -213,6 +212,22 @@ void validateValueListArgument() {
});
}

@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]; \
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: \
codes [myService#addUniqueHobbies.hobbies,hobbies]; \
arguments []; default message [hobbies]]; default message [must not be blank]"""));
});
}

private void testArgs(Object target, Method method, Object[] args, Consumer<MethodValidationResult> consumer) {
consumer.accept(this.validationAdapter.validateArguments(target, method, null, args, new Class<?>[0]));
}
Expand Down Expand Up @@ -271,6 +286,8 @@ public void addPeople(@Valid List<Person> people) {
public void addHobbies(List<@NotBlank String> hobbies) {
}

public void addUniqueHobbies(Set<@NotBlank String> hobbies) {
}
}


Expand Down

0 comments on commit 9d45a8d

Please sign in to comment.