Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolve method-only bounded generics correctly #34086

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as far as I understand the code here, we want to resolve the T of a generic. If the T is not defined by the contextType, (because ist is no class Foo<T extends Bar>) we get to this line.

In my case, the T is bound by the function public <T extends BaseType> List<T> get() and means, that T is at least a BaseType

IMHO the type must be resolved here, so that the following code knows , that the elements in the list are BaseType or subtypes. This is later required for jackson, otherwise we have problems mentioned in this SO post.

}
}
else if (typeArgument instanceof ParameterizedType) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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&gt;? extends SomeType&lt; 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 <T extends BaseType> List<T> get() {
List<T> list = new ArrayList<>();
list.add((T) new SubType1());
list.add((T) new SubType2());
return list;
}

}
}
Loading