google / google/gson

Generic deserializer should extract type information from generic boundary

Open
#2,563 15 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
24.2k
Forks
4.5k
Avg merge
6d 4h
Merged PRs (30d)
12

Description

# Gson version
2.10.1

# Java / Android version
17

# Description
If field is declared with static type, the type is correctly used during deserialization. If type is generic with boundary, the boundary information is not used, it is presumably deserialized as object, that is usually as map.

## Expected behavior
Generic type with boundary should deserialize similarly as static type.

## Actual behavior
Boundary type information is ignored

# Reproduction steps

```
public class Foo {
private final String text;

public Foo(String text) {
this.text = text;
}

public String getText() {
return text;
}
}

public class BarStatic {
private final Foo foo;

public BarStatic(Foo foo) {
this.foo = foo;
}

public Foo getFoo() {
return foo;
}

}

public class BarDynamic {
private final T foo;

public BarDynamic(T foo) {
this.foo = foo;
}

public T getFoo() {
return foo;
}

}

@Test
public void testSerialize() {
Gson gson = new GsonBuilder().create();

BarStatic barStatic = new BarStatic(new Foo("foo"));
String barJsonStatic = gson.toJson(barStatic);

BarDynamic barDynamic = new BarDynamic<>(new Foo("foo"));
String barJsonDynamic = gson.toJson(barDynamic);

Assert.assertEquals(barJsonStatic, barJsonDynamic);

BarStatic barStaticDeserialized = gson.fromJson(barJsonStatic, BarStatic.class);
Assert.assertEquals(barStaticDeserialized.getFoo().getText(), "foo");

// A problem!
BarDynamic barDeserialized = gson.fromJson(barJsonDynamic, BarDynamic.class);

}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.