Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix adaptation of violations on Set method parameter #33150

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading