Affects: \
As you can see,after resolve,that method return class named ComApproveRequest to replace genericType.
ComApproveRequest is a class which has generic parameter named ComDealExDTO. But the bounds of genericType
is class which is subclass of ComDealExDTO. So this leads to Jackson not being able to correctly convert the json String to type of subclass of ComDealExDTO.
Is this a bug ?And how do i deal with it?
Comment From: quaff
You should attach a test case.
Comment From: zhanlinfeng
You should attach a test case.
Well,The following is a test case. The case give a class named TestClass2 which hold a generic parameters with bounds SuperParamClass, While TestClass2 is bounds of generic parameters held by TestClass1.On TestClass1, Generic parameters of TestClass2 is specified as SubParamClass which extends from SuperParamClass。
static class TestClass1<Req extends TestClass2<SubParamClass>> {
public void testMethod(Req req) {}
}
@Data
static class TestClass2<T extends SuperParamClass> {
T data;
}
@Data
static class SuperParamClass {
String field1;
}
@Data
static class SubParamClass extends SuperParamClass{
}
@Test
public void test() throws NoSuchMethodException, JsonProcessingException {
// step 1: get Method named testMethod and parameter type
Method testMethod = TestClass1.class.getMethod("testMethod", TestClass2.class);
Type genericParameterType = testMethod.getGenericParameterTypes()[0];
String testJson = "{\"data\" : {\"field1\" : \"1\"}}";
ObjectMapper objectMapper = new ObjectMapper();
TypeFactory typeFactory = objectMapper.getTypeFactory();
// step 2: parse json using origin parameter type
JavaType javaTypeWithoutResolve = typeFactory.constructType(genericParameterType);
TestClass2 resultWithoutResolve = objectMapper.readValue(testJson, javaTypeWithoutResolve);
System.out.println(resultWithoutResolve.data.getClass());
// step 3: parse json using parameter type resolved by GenericTypeResolver.resolveType. Actually, SpringMvc do the same, when bind http request body in json format
JavaType javaType = typeFactory.constructType(GenericTypeResolver.resolveType(genericParameterType, TestClass1.class));
TestClass2 result = objectMapper.readValue(testJson, javaType);
System.out.println(result.data.getClass());
}
And the result after that case executed is as following:
Without GenericTypeResolver.resolveType: SubParamClass
With GenericTypeResolver.resolveType: SuperParamClass
So It prove that Method resolveType of GenericTypeResolver lost bounds info of TypeVariableImpl Actually, Spring invoke GenericTypeResolver.resolveType to transfer paramteer type of method of Controller, this result in binding http request body in json format to a java object wrong.
Comment From: snicoll
This test case is failing on step2 for me. I am not sure if you're reporting a bug against ResolvableType
or if it's a misuse of the API. Can you please update that code in text into an actual sample that we can run? You can do so by attaching a zip to this issue or pushing the code to a GitHub repository.
Comment From: spring-projects-issues
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.
Comment From: spring-projects-issues
Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.