Skip to content

Commit 266043a

Browse files
authored
Unit test for type variable inference test.
See google#2563
1 parent e0628ef commit 266043a

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.google.gson.functional;
2+
3+
import static com.google.common.truth.Truth.assertThat;
4+
5+
import com.google.gson.Gson;
6+
import org.junit.Before;
7+
import org.junit.Test;
8+
9+
/**
10+
* Test deserialization of generic wrapper with type bound.
11+
*
12+
* @author sevcenko
13+
*/
14+
public class InferenceFromTypeVariableTest {
15+
private Gson gson;
16+
17+
@Before
18+
public void setUp() throws Exception {
19+
gson = new Gson();
20+
}
21+
22+
public static class Foo {
23+
private final String text;
24+
25+
public Foo(String text) {
26+
this.text = text;
27+
}
28+
29+
public String getText() {
30+
return text;
31+
}
32+
}
33+
34+
public static class BarDynamic<T extends Foo> {
35+
private final T foo;
36+
37+
public BarDynamic(T foo) {
38+
this.foo = foo;
39+
}
40+
41+
public T getFoo() {
42+
return foo;
43+
}
44+
}
45+
46+
@Test
47+
public void testSubClassSerialization() {
48+
BarDynamic<Foo> bar = new BarDynamic<>(new Foo("foo!"));
49+
assertThat(gson.toJson(bar)).isEqualTo("{\"foo\":{\"text\":\"foo!\"}}");
50+
// without #2563 fix, this would deserialize foo as Object and fails to assign it to foo field
51+
BarDynamic<?> deserialized = gson.fromJson(gson.toJson(bar), BarDynamic.class);
52+
assertThat(deserialized.getFoo().getText()).isEqualTo("foo!");
53+
}
54+
}

0 commit comments

Comments
 (0)