Skip to content

Commit 3008d97

Browse files
committed
Polishing contribution
Closes gh-33150
1 parent f0c7584 commit 3008d97

File tree

2 files changed

+5
-11
lines changed

2 files changed

+5
-11
lines changed

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

+4-5
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,16 +345,16 @@ else if (key != null && arg instanceof Map<?, ?> map) {
346345
value = map.get(key);
347346
container = map;
348347
}
349-
else if (arg instanceof Set<?>) {
348+
else if (arg instanceof Iterable<?>) {
349+
// No index or key, cannot access the specific value
350350
value = arg;
351-
container = null;
351+
container = arg;
352352
}
353353
else if (arg instanceof Optional<?> optional) {
354354
value = optional.orElse(null);
355355
container = optional;
356356
}
357357
else {
358-
Assert.state(!node.isInIterable(), "No way to unwrap Iterable without index");
359358
value = arg;
360359
container = null;
361360
}

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.springframework.validation.method.ParameterValidationResult;
4141

4242
import static org.assertj.core.api.Assertions.assertThat;
43-
import static org.assertj.core.api.InstanceOfAssertFactories.SET;
4443

4544
/**
4645
* Tests for {@link MethodValidationAdapter}.
@@ -203,9 +202,7 @@ void validateValueListArgument() {
203202
Method method = getMethod(target, "addHobbies");
204203

205204
testArgs(target, method, new Object[] {List.of(" ")}, ex -> {
206-
207205
assertThat(ex.getAllValidationResults()).hasSize(1);
208-
209206
assertValueResult(ex.getValueResults().get(0), 0, " ", List.of("""
210207
org.springframework.context.support.DefaultMessageSourceResolvable: \
211208
codes [NotBlank.myService#addHobbies.hobbies,NotBlank.hobbies,NotBlank.java.util.List,NotBlank]; \
@@ -215,15 +212,13 @@ void validateValueListArgument() {
215212
});
216213
}
217214

218-
@Test
215+
@Test // gh-33150
219216
void validateValueSetArgument() {
220217
MyService target = new MyService();
221218
Method method = getMethod(target, "addUniqueHobbies");
222219

223220
testArgs(target, method, new Object[] {Set.of("test", " ")}, ex -> {
224-
225221
assertThat(ex.getAllValidationResults()).hasSize(1);
226-
227222
assertValueResult(ex.getValueResults().get(0), 0, Set.of("test", " "), List.of("""
228223
org.springframework.context.support.DefaultMessageSourceResolvable: \
229224
codes [NotBlank.myService#addUniqueHobbies.hobbies,NotBlank.hobbies,NotBlank.java.util.Set,NotBlank]; \

0 commit comments

Comments
 (0)