Skip to content

Commit

Permalink
Fix validation of Set values
Browse files Browse the repository at this point in the history
Since commit 8552e14, 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
  • Loading branch information
snussbaumer committed Jul 4, 2024
1 parent f4607da commit fb4b187
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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 All @@ -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}.
Expand Down Expand Up @@ -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<MethodValidationResult> consumer) {
consumer.accept(this.validationAdapter.validateArguments(target, method, null, args, new Class<?>[0]));
}
Expand Down Expand Up @@ -271,6 +291,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 fb4b187

Please sign in to comment.