Gson cannot find the runtime type of inner generic type
- Dominant language
- Java
- Stars
- 24.2k
- Forks
- 4.5k
- Avg merge
- 6d 4h
- Merged PRs (30d)
- 12
Description
Gson version : 2.3.1
```
public class Test {
public static void main(String[] args) {
B b = new B<>();
b.setT("hhh");
Map map = new HashMap<>();
b.setMsg("233");
Container container = new Container();
container.setA(b);
System.out.println(GsonUtil.toJson(b));
System.out.println(GsonUtil.toJson(container));
}
}
@Data
class Container{
A a;
}
@Data
class A{
T t;
}
@Data
class B extends A{
String msg;
}
```
output:
```
{"msg":"233","t":"hhh"}
{"a":{"t":"hhh"}}
```
the second output "{"a":{"t":"hhh"}}" is short of the field msg.
however if i remove the generic operator of 'a' in the container, like that
```
@Data
class Container{
A a;
}
```
then,output:
```
{"msg":"233","t":"hhh"}
{"a":{"msg":"233","t":"hhh"}}
```
I wonder know is it feature or bug.
I find the code in file TypeAdapterRuntimeTypeWrapper.class line 74,
```
/**
* Finds a compatible runtime type if it is more specific
*/
private Type getRuntimeTypeIfMoreSpecific(Type type, Object value) {
if (value != null
&& (type == Object.class || type instanceof TypeVariable || type instanceof Class)) {
type = value.getClass();
}
return type;
}
```
if input type is a generic type ,then the class of the input type will be the ParameterizedType,
dose it means generic type is specific ?
Contributor guide
Assessment
This issue has not been assessed yet.