Skip to content

Commit

Permalink
Unit test for type variable inference test.
Browse files Browse the repository at this point in the history
  • Loading branch information
msevcenko authored Dec 7, 2023
1 parent e0628ef commit 266043a
Showing 1 changed file with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.google.gson.functional;

import static com.google.common.truth.Truth.assertThat;

import com.google.gson.Gson;
import org.junit.Before;
import org.junit.Test;

/**
* Test deserialization of generic wrapper with type bound.
*
* @author sevcenko
*/
public class InferenceFromTypeVariableTest {
private Gson gson;

@Before
public void setUp() throws Exception {
gson = new Gson();
}

public static class Foo {
private final String text;

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

public String getText() {
return text;
}
}

public static class BarDynamic<T extends Foo> {
private final T foo;

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

public T getFoo() {
return foo;
}
}

@Test
public void testSubClassSerialization() {
BarDynamic<Foo> bar = new BarDynamic<>(new Foo("foo!"));
assertThat(gson.toJson(bar)).isEqualTo("{\"foo\":{\"text\":\"foo!\"}}");
// without #2563 fix, this would deserialize foo as Object and fails to assign it to foo field
BarDynamic<?> deserialized = gson.fromJson(gson.toJson(bar), BarDynamic.class);
assertThat(deserialized.getFoo().getText()).isEqualTo("foo!");
}
}

0 comments on commit 266043a

Please sign in to comment.