From 033d277bcffacc50bcdb6e1a837ad0b4d99882c9 Mon Sep 17 00:00:00 2001 From: Roland Praml Date: Fri, 13 Dec 2024 08:23:38 +0100 Subject: [PATCH 1/2] Resolve base type in parameterized type if necessary See gh-34086 --- .../core/GenericTypeResolver.java | 2 +- .../standalone/GenericReturnTests.java | 81 +++++++++++++++++++ 2 files changed, 82 insertions(+), 1 deletion(-) create mode 100644 spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/GenericReturnTests.java diff --git a/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java b/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java index a5e0d74c65bd..d13acdbe26f2 100644 --- a/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java +++ b/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java @@ -180,7 +180,7 @@ else if (genericType instanceof ParameterizedType parameterizedType) { generics[i] = resolvedTypeArgument; } else { - generics[i] = ResolvableType.forType(typeArgument); + generics[i] = ResolvableType.forType(typeArgument).resolveType(); } } else if (typeArgument instanceof ParameterizedType) { diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/GenericReturnTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/GenericReturnTests.java new file mode 100644 index 000000000000..a10a56157bc7 --- /dev/null +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/GenericReturnTests.java @@ -0,0 +1,81 @@ +/* + * Copyright 2002-2024 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.test.web.servlet.samples.standalone; + +import java.util.ArrayList; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonSubTypes; +import com.fasterxml.jackson.annotation.JsonTypeInfo; +import org.junit.jupiter.api.Test; + +import org.springframework.http.MediaType; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; + +/** + * Demonstrates, if returning a List>? extends SomeType< can be correctly serialized. + * + * @author Roland Praml + */ +public class GenericReturnTests { + @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") + @JsonSubTypes({ + @JsonSubTypes.Type(value = SubType1.class, name = "one"), + @JsonSubTypes.Type(value = SubType2.class, name = "two") + }) + public static class BaseType { + + } + + public static class SubType1 extends BaseType { + } + + public static class SubType2 extends BaseType { + } + + @Test + public void genericReturnTest() throws Exception { + + standaloneSetup(new Controller()).build() + .perform(get("/genericReturnList").accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(content().contentType("application/json")) + .andExpect(jsonPath("$[0].type").value("one")) + .andExpect(jsonPath("$[1].type").value("two")); + } + + @RestController + @SuppressWarnings("unchecked") + public static class Controller { + + @GetMapping(value = "/genericReturnList", produces = MediaType.APPLICATION_JSON_VALUE) + public List get() { + List list = new ArrayList<>(); + list.add((T) new SubType1()); + list.add((T) new SubType2()); + return list; + } + + } +} From fe5f5d53b2f2dac95eaa5fe93a0be6756ccb71e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Nicoll?= Date: Wed, 15 Jan 2025 17:03:34 +0100 Subject: [PATCH 2/2] Polish "Resolve base type in parameterized type if necessary" See gh-34086 --- .../core/GenericTypeResolver.java | 2 +- .../core/GenericTypeResolverTests.java | 18 ++++- .../standalone/GenericReturnTests.java | 81 ------------------- 3 files changed, 18 insertions(+), 83 deletions(-) delete mode 100644 spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/GenericReturnTests.java diff --git a/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java b/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java index d13acdbe26f2..c27a23cd07e4 100644 --- a/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java +++ b/spring-core/src/main/java/org/springframework/core/GenericTypeResolver.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. diff --git a/spring-core/src/test/java/org/springframework/core/GenericTypeResolverTests.java b/spring-core/src/test/java/org/springframework/core/GenericTypeResolverTests.java index 6338bedea81b..1f80cc6c3431 100644 --- a/spring-core/src/test/java/org/springframework/core/GenericTypeResolverTests.java +++ b/spring-core/src/test/java/org/springframework/core/GenericTypeResolverTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2024 the original author or authors. + * Copyright 2002-2025 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. @@ -211,6 +211,14 @@ void resolveNestedTypeVariable() throws Exception { assertThat(ResolvableType.forType(resolved).getGeneric(0).getGeneric(0).resolve()).isEqualTo(String.class); } + @Test + void resolvedTypeWithBase() { + Type type = method(WithBaseTypes.class, "get").getGenericReturnType(); + Type resolvedType = resolveType(type, WithBaseTypes.class); + ParameterizedTypeReference> reference = new ParameterizedTypeReference<>() {}; + assertThat(resolvedType).isEqualTo(reference.getType()); + } + private static Method method(Class target, String methodName, Class... parameterTypes) { Method method = findMethod(target, methodName, parameterTypes); assertThat(method).describedAs(target.getName() + "#" + methodName).isNotNull(); @@ -398,4 +406,12 @@ public interface ListOfListSupplier { public interface StringListOfListSupplier extends ListOfListSupplier { } + static class WithBaseTypes { + + List get() { + return List.of(); + } + + } + } diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/GenericReturnTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/GenericReturnTests.java deleted file mode 100644 index a10a56157bc7..000000000000 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/GenericReturnTests.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Copyright 2002-2024 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.test.web.servlet.samples.standalone; - -import java.util.ArrayList; -import java.util.List; - -import com.fasterxml.jackson.annotation.JsonSubTypes; -import com.fasterxml.jackson.annotation.JsonTypeInfo; -import org.junit.jupiter.api.Test; - -import org.springframework.http.MediaType; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RestController; - -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.springframework.test.web.servlet.setup.MockMvcBuilders.standaloneSetup; - -/** - * Demonstrates, if returning a List>? extends SomeType< can be correctly serialized. - * - * @author Roland Praml - */ -public class GenericReturnTests { - @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type") - @JsonSubTypes({ - @JsonSubTypes.Type(value = SubType1.class, name = "one"), - @JsonSubTypes.Type(value = SubType2.class, name = "two") - }) - public static class BaseType { - - } - - public static class SubType1 extends BaseType { - } - - public static class SubType2 extends BaseType { - } - - @Test - public void genericReturnTest() throws Exception { - - standaloneSetup(new Controller()).build() - .perform(get("/genericReturnList").accept(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andExpect(content().contentType("application/json")) - .andExpect(jsonPath("$[0].type").value("one")) - .andExpect(jsonPath("$[1].type").value("two")); - } - - @RestController - @SuppressWarnings("unchecked") - public static class Controller { - - @GetMapping(value = "/genericReturnList", produces = MediaType.APPLICATION_JSON_VALUE) - public List get() { - List list = new ArrayList<>(); - list.add((T) new SubType1()); - list.add((T) new SubType2()); - return list; - } - - } -}