Skip to content

Commit b81a291

Browse files
committed
Add reflection hints for @RequestPart
Closes gh-29749
1 parent 3348e74 commit b81a291

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -79,7 +79,8 @@ protected void registerMethodHints(ReflectionHints hints, Method method) {
7979

8080
protected void registerParameterTypeHints(ReflectionHints hints, MethodParameter methodParameter) {
8181
if (methodParameter.hasParameterAnnotation(RequestBody.class) ||
82-
methodParameter.hasParameterAnnotation(ModelAttribute.class)) {
82+
methodParameter.hasParameterAnnotation(ModelAttribute.class) ||
83+
methodParameter.hasParameterAnnotation(RequestPart.class)) {
8384
this.bindingRegistrar.registerReflectionHints(hints, methodParameter.getGenericParameterType());
8485
}
8586
else if (HttpEntity.class.isAssignableFrom(methodParameter.getParameterType())) {

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -183,6 +183,24 @@ void registerReflectiveHintsForMethodWithRawHttpEntityParameter() throws NoSuchM
183183
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleController.class)));
184184
}
185185

186+
@Test
187+
void registerReflectiveHintsForMethodWithPartToConvert() throws NoSuchMethodException {
188+
Method method = SampleController.class.getDeclaredMethod("postPartToConvert", Request.class);
189+
processor.registerReflectionHints(hints, method);
190+
assertThat(hints.typeHints()).satisfiesExactlyInAnyOrder(
191+
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleController.class)),
192+
typeHint -> {
193+
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(Request.class));
194+
assertThat(typeHint.getMemberCategories()).containsExactlyInAnyOrder(
195+
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
196+
MemberCategory.DECLARED_FIELDS);
197+
assertThat(typeHint.methods()).satisfiesExactlyInAnyOrder(
198+
hint -> assertThat(hint.getName()).isEqualTo("getMessage"),
199+
hint -> assertThat(hint.getName()).isEqualTo("setMessage"));
200+
},
201+
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)));
202+
}
203+
186204
static class SampleController {
187205

188206
@GetMapping
@@ -225,6 +243,10 @@ void postHttpEntity(HttpEntity<Request> entity) {
225243
void postRawHttpEntity(HttpEntity entity) {
226244
}
227245

246+
@PostMapping
247+
void postPartToConvert(@RequestPart Request request) {
248+
}
249+
228250
}
229251

230252
@RestController

0 commit comments

Comments
 (0)