Skip to content

Commit 9d42779

Browse files
committed
Add reflection hints for data binding in Web controllers
Closes spring-projectsgh-28623
1 parent 4104ea7 commit 9d42779

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

spring-web/src/main/java/org/springframework/web/bind/annotation/RequestMappingReflectiveProcessor.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,8 @@ protected void registerParameterHints(ReflectionHints hints, Method method) {
7272
hints.registerMethod(method, hint -> hint.setModes(ExecutableMode.INVOKE));
7373
for (Parameter parameter : method.getParameters()) {
7474
MethodParameter methodParameter = MethodParameter.forParameter(parameter);
75-
if (methodParameter.hasParameterAnnotation(RequestBody.class)) {
75+
if (methodParameter.hasParameterAnnotation(RequestBody.class) ||
76+
methodParameter.hasParameterAnnotation(ModelAttribute.class)) {
7677
this.bindingRegistrar.registerReflectionHints(hints, methodParameter.getGenericParameterType());
7778
}
7879
else if (HttpEntity.class.isAssignableFrom(methodParameter.getParameterType())) {

spring-web/src/test/java/org/springframework/web/bind/annotation/RequestMappingReflectiveProcessorTests.java

+22
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,24 @@ void registerReflectiveHintsForMethodWithRequestBody() throws NoSuchMethodExcept
7474
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)));
7575
}
7676

77+
@Test
78+
void registerReflectiveHintsForMethodWithModelAttribute() throws NoSuchMethodException {
79+
Method method = SampleController.class.getDeclaredMethod("postForm", Request.class);
80+
processor.registerReflectionHints(hints, method);
81+
assertThat(hints.typeHints()).satisfiesExactlyInAnyOrder(
82+
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleController.class)),
83+
typeHint -> {
84+
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(Request.class));
85+
assertThat(typeHint.getMemberCategories()).containsExactlyInAnyOrder(
86+
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
87+
MemberCategory.DECLARED_FIELDS);
88+
assertThat(typeHint.methods()).satisfiesExactlyInAnyOrder(
89+
hint -> assertThat(hint.getName()).isEqualTo("getMessage"),
90+
hint -> assertThat(hint.getName()).isEqualTo("setMessage"));
91+
},
92+
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)));
93+
}
94+
7795
@Test
7896
void registerReflectiveHintsForMethodWithRestController() throws NoSuchMethodException {
7997
Method method = SampleRestController.class.getDeclaredMethod("get");
@@ -177,6 +195,10 @@ Response get() {
177195
void post(@RequestBody Request request) {
178196
}
179197

198+
@PostMapping
199+
void postForm(@ModelAttribute Request request) {
200+
}
201+
180202
@GetMapping
181203
@ResponseBody
182204
String message() {

0 commit comments

Comments
 (0)