Skip to content

Commit 459338f

Browse files
committedDec 21, 2023
Unwrap Optional in MethodValidationAdapter
See gh-31746
1 parent f0add92 commit 459338f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed
 

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

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import java.util.LinkedHashSet;
2525
import java.util.List;
2626
import java.util.Map;
27+
import java.util.Optional;
2728
import java.util.Set;
2829
import java.util.function.Function;
2930
import java.util.function.Supplier;
@@ -354,6 +355,10 @@ else if (containerKey != null && arg instanceof Map<?, ?> map) {
354355
bean = map.get(containerKey);
355356
container = map;
356357
}
358+
else if (arg instanceof Optional<?> optional) {
359+
bean = optional.orElse(null);
360+
container = optional;
361+
}
357362
else {
358363
Assert.state(!node.isInIterable(), "No way to unwrap Iterable without index");
359364
bean = arg;

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

+19-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222
import java.util.Map;
23+
import java.util.Optional;
2324

2425
import jakarta.validation.Valid;
2526
import jakarta.validation.constraints.NotBlank;
@@ -146,6 +147,20 @@ void fieldOfObjectPropertyOfMapValue() {
146147
assertSingleFieldError(errors, 1, courses, null, "CS 101", "professor.name", invalidPerson.name());
147148
}
148149

150+
@Test
151+
void fieldOfObjectPropertyOfOptionalBean() {
152+
Method method = getMethod("addOptionalCourse");
153+
Optional<Course> optional = Optional.of(new Course("CS 101", invalidPerson, Collections.emptyList()));
154+
Object[] args = {optional};
155+
156+
MethodValidationResult result =
157+
validationAdapter.validateArguments(new MyService(), method, null, args, HINTS);
158+
159+
assertThat(result.getAllErrors()).hasSize(1);
160+
ParameterErrors errors = result.getBeanResults().get(0);
161+
assertSingleFieldError(errors, 1, optional, null, null, "professor.name", invalidPerson.name());
162+
}
163+
149164
}
150165

151166

@@ -204,7 +219,7 @@ private static Method getMethod(String methodName) {
204219
}
205220

206221

207-
@SuppressWarnings("unused")
222+
@SuppressWarnings({"unused", "OptionalUsedAsFieldOrParameterType"})
208223
private static class MyService {
209224

210225
public void addCourse(@Valid Course course) {
@@ -219,6 +234,9 @@ public void addCourseArray(@Valid Course[] courses) {
219234
public void addCourseMap(@Valid Map<String, Course> courses) {
220235
}
221236

237+
public void addOptionalCourse(@Valid Optional<Course> course) {
238+
}
239+
222240
@Valid
223241
public Course getCourse(Course course) {
224242
throw new UnsupportedOperationException();

0 commit comments

Comments
 (0)
Please sign in to comment.