Add support for serialization in RequestMappingReflectiveProcessor

Support reflection-based serialization of parameters annotated
with @RequestBody and return values annotated with @ResponseBody.

It leverages a new BindingReflectionHintsRegistrar class that
is designed to register transitively the types usually needed
for binding and reflection-based serialization on fields,
constructors and properties. Generics are taken in account
as well.

Closes gh-28518
This commit is contained in:
Sébastien Deleuze
2022-06-13 14:44:49 +02:00
parent 12d756a252
commit a57dead4be
4 changed files with 503 additions and 6 deletions

View File

@@ -0,0 +1,176 @@
/*
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.web.bind.annotation;
import java.lang.reflect.Method;
import org.junit.jupiter.api.Test;
import org.springframework.aot.hint.MemberCategory;
import org.springframework.aot.hint.ReflectionHints;
import org.springframework.aot.hint.TypeReference;
import static org.assertj.core.api.Assertions.assertThat;
/**
* Tests for {@link RequestMappingReflectiveProcessor}.
*
* @author Sebastien Deleuze
*/
public class RequestMappingReflectiveProcessorTests {
private final RequestMappingReflectiveProcessor processor = new RequestMappingReflectiveProcessor();
private final ReflectionHints hints = new ReflectionHints();
@Test
void registerReflectiveHintsForMethodWithResponseBody() throws NoSuchMethodException {
Method method = SampleController.class.getDeclaredMethod("get");
processor.registerReflectionHints(hints, method);
assertThat(hints.typeHints()).satisfiesExactlyInAnyOrder(
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleController.class)),
typeHint -> {
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(Response.class));
assertThat(typeHint.getMemberCategories()).containsExactlyInAnyOrder(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.DECLARED_FIELDS);
assertThat(typeHint.methods()).satisfiesExactlyInAnyOrder(
hint -> assertThat(hint.getName()).isEqualTo("getMessage"),
hint -> assertThat(hint.getName()).isEqualTo("setMessage"));
},
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)));
}
@Test
void registerReflectiveHintsForMethodWithRequestBody() throws NoSuchMethodException {
Method method = SampleController.class.getDeclaredMethod("post", Request.class);
processor.registerReflectionHints(hints, method);
assertThat(hints.typeHints()).satisfiesExactlyInAnyOrder(
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleController.class)),
typeHint -> {
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(Request.class));
assertThat(typeHint.getMemberCategories()).containsExactlyInAnyOrder(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.DECLARED_FIELDS);
assertThat(typeHint.methods()).satisfiesExactlyInAnyOrder(
hint -> assertThat(hint.getName()).isEqualTo("getMessage"),
hint -> assertThat(hint.getName()).isEqualTo("setMessage"));
},
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)));
}
@Test
void registerReflectiveHintsForMethodWithRestController() throws NoSuchMethodException {
Method method = SampleRestController.class.getDeclaredMethod("get");
processor.registerReflectionHints(hints, method);
assertThat(hints.typeHints()).satisfiesExactlyInAnyOrder(
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleRestController.class)),
typeHint -> {
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(Response.class));
assertThat(typeHint.getMemberCategories()).containsExactlyInAnyOrder(
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS,
MemberCategory.DECLARED_FIELDS);
assertThat(typeHint.methods()).satisfiesExactlyInAnyOrder(
hint -> assertThat(hint.getName()).isEqualTo("getMessage"),
hint -> assertThat(hint.getName()).isEqualTo("setMessage"));
},
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class)));
}
@Test
void registerReflectiveHintsForMethodWithString() throws NoSuchMethodException {
Method method = SampleController.class.getDeclaredMethod("message");
processor.registerReflectionHints(hints, method);
assertThat(hints.typeHints()).satisfiesExactlyInAnyOrder(
typeHint -> assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleController.class)),
typeHint -> {
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(String.class));
assertThat(typeHint.constructors()).isEmpty();
assertThat(typeHint.fields()).isEmpty();
assertThat(typeHint.methods()).isEmpty();
});
}
@Test
void registerReflectiveHintsForClassWithMapping() {
processor.registerReflectionHints(hints, SampleControllerWithClassMapping.class);
assertThat(hints.typeHints()).singleElement().satisfies(typeHint ->
assertThat(typeHint.getType()).isEqualTo(TypeReference.of(SampleControllerWithClassMapping.class)));
}
static class SampleController {
@GetMapping
@ResponseBody
Response get() {
return new Response("response");
}
@PostMapping
void post(@RequestBody Request request) {
}
@GetMapping
@ResponseBody
String message() {
return "";
}
}
@RestController
static class SampleRestController {
@GetMapping
Response get() {
return new Response("response");
}
}
@RequestMapping("/prefix")
static class SampleControllerWithClassMapping {
}
static class Request {
private String message;
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
static class Response {
private String message;
public Response(String message) {
this.message = message;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
}
}