Skip to content

Commit 9d45a8d

Browse files
committed
Merge branch '6.1.x'
2 parents f91f791 + 3008d97 commit 9d45a8d

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationAdapter.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import org.springframework.core.ParameterNameDiscoverer;
5151
import org.springframework.core.annotation.AnnotationUtils;
5252
import org.springframework.lang.Nullable;
53-
import org.springframework.util.Assert;
5453
import org.springframework.util.function.SingletonSupplier;
5554
import org.springframework.validation.BeanPropertyBindingResult;
5655
import org.springframework.validation.BindingResult;
@@ -321,7 +320,7 @@ else if (node.getKind().equals(ElementKind.RETURN_VALUE)) {
321320

322321
Object arg = argumentFunction.apply(parameter.getParameterIndex());
323322

324-
// If the arg is a container, we need to element, but the only way to extract it
323+
// If the arg is a container, we need the element, but the only way to extract it
325324
// is to check for and use a container index or key on the next node:
326325
// https://github.com/jakartaee/validation/issues/194
327326

@@ -346,12 +345,16 @@ else if (key != null && arg instanceof Map<?, ?> map) {
346345
value = map.get(key);
347346
container = map;
348347
}
348+
else if (arg instanceof Iterable<?>) {
349+
// No index or key, cannot access the specific value
350+
value = arg;
351+
container = arg;
352+
}
349353
else if (arg instanceof Optional<?> optional) {
350354
value = optional.orElse(null);
351355
container = optional;
352356
}
353357
else {
354-
Assert.state(!node.isInIterable(), "No way to unwrap Iterable without index");
355358
value = arg;
356359
container = null;
357360
}

spring-context/src/test/java/org/springframework/validation/beanvalidation/MethodValidationAdapterTests.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Method;
2020
import java.util.List;
2121
import java.util.Locale;
22+
import java.util.Set;
2223
import java.util.function.Consumer;
2324

2425
import jakarta.validation.Valid;
@@ -201,9 +202,7 @@ void validateValueListArgument() {
201202
Method method = getMethod(target, "addHobbies");
202203

203204
testArgs(target, method, new Object[] {List.of(" ")}, ex -> {
204-
205205
assertThat(ex.getAllValidationResults()).hasSize(1);
206-
207206
assertValueResult(ex.getValueResults().get(0), 0, " ", List.of("""
208207
org.springframework.context.support.DefaultMessageSourceResolvable: \
209208
codes [NotBlank.myService#addHobbies.hobbies,NotBlank.hobbies,NotBlank.java.util.List,NotBlank]; \
@@ -213,6 +212,22 @@ void validateValueListArgument() {
213212
});
214213
}
215214

215+
@Test // gh-33150
216+
void validateValueSetArgument() {
217+
MyService target = new MyService();
218+
Method method = getMethod(target, "addUniqueHobbies");
219+
220+
testArgs(target, method, new Object[] {Set.of("test", " ")}, ex -> {
221+
assertThat(ex.getAllValidationResults()).hasSize(1);
222+
assertValueResult(ex.getValueResults().get(0), 0, Set.of("test", " "), List.of("""
223+
org.springframework.context.support.DefaultMessageSourceResolvable: \
224+
codes [NotBlank.myService#addUniqueHobbies.hobbies,NotBlank.hobbies,NotBlank.java.util.Set,NotBlank]; \
225+
arguments [org.springframework.context.support.DefaultMessageSourceResolvable: \
226+
codes [myService#addUniqueHobbies.hobbies,hobbies]; \
227+
arguments []; default message [hobbies]]; default message [must not be blank]"""));
228+
});
229+
}
230+
216231
private void testArgs(Object target, Method method, Object[] args, Consumer<MethodValidationResult> consumer) {
217232
consumer.accept(this.validationAdapter.validateArguments(target, method, null, args, new Class<?>[0]));
218233
}
@@ -271,6 +286,8 @@ public void addPeople(@Valid List<Person> people) {
271286
public void addHobbies(List<@NotBlank String> hobbies) {
272287
}
273288

289+
public void addUniqueHobbies(Set<@NotBlank String> hobbies) {
290+
}
274291
}
275292

276293

0 commit comments

Comments
 (0)