From fb4b187400f9324b6de6b8db426f4ccca58b6437 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20NUSSBAUMER?= Date: Thu, 4 Jul 2024 22:15:42 +0200 Subject: [PATCH] Fix validation of Set values Since commit 8552e149b5faae31cab6c7d9d3f0f189904e4a8e, validation of Set values (for example `Set<@NotBlank String>`) crashes with error : No way to unwrap Iterable without index With this fix we don't know which value exactly is the problem, but at least we have a working validation --- .../MethodValidationAdapter.java | 4 ++++ .../MethodValidationAdapterTests.java | 22 +++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationAdapter.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationAdapter.java index fa218e5cd265..7df0b21deab1 100644 --- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationAdapter.java +++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/MethodValidationAdapter.java @@ -346,6 +346,10 @@ else if (key != null && arg instanceof Map map) { value = map.get(key); container = map; } + else if (arg instanceof Set) { + value = arg; + container = null; + } else if (arg instanceof Optional optional) { value = optional.orElse(null); container = optional; diff --git a/spring-context/src/test/java/org/springframework/validation/beanvalidation/MethodValidationAdapterTests.java b/spring-context/src/test/java/org/springframework/validation/beanvalidation/MethodValidationAdapterTests.java index 0fe51acdc9c6..a2e02b14df2f 100644 --- a/spring-context/src/test/java/org/springframework/validation/beanvalidation/MethodValidationAdapterTests.java +++ b/spring-context/src/test/java/org/springframework/validation/beanvalidation/MethodValidationAdapterTests.java @@ -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; @@ -39,6 +40,7 @@ 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}. @@ -213,6 +215,24 @@ void validateValueListArgument() { }); } + @Test + 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 consumer) { consumer.accept(this.validationAdapter.validateArguments(target, method, null, args, new Class[0])); } @@ -271,6 +291,8 @@ public void addPeople(@Valid List people) { public void addHobbies(List<@NotBlank String> hobbies) { } + public void addUniqueHobbies(Set<@NotBlank String> hobbies) { + } }