The recursive type variable resolution protections put into place in Gson 2.8.2 to fix #1128 does not work if a TypeVariable is referenced multiple times.
Example failing code:
enum TestEnum { ONE, TWO, THREE }
private static class TestEnumSetCollection extends SetCollection<TestEnum> {}
private static class SetCollection<T> extends BaseCollection<T, Set<T>> {}
private static class BaseCollection<U, C extends Collection<U>>
{
public C collection;
}
When used with the following code to unmarshal
TestEnumSetCollection withSet = gson.fromJson("{\"collection\":[\"ONE\",\"THREE\"]}", TestEnumSetCollection.class);
The enum values are unmarshaled as String instances instead of as TestEnum instances, causing ClassCastException to be raised at runtime. This is due to the fact that the visitedTypeVariables map receives an entry for T, resolves it properly, and then upon subsequent attempt to resolve T fails, since the visitedTypeVariables set indicates that T has already been resolved.
The recursive type variable resolution protections put into place in Gson 2.8.2 to fix #1128 does not work if a TypeVariable is referenced multiple times.
Example failing code:
When used with the following code to unmarshal
The enum values are unmarshaled as
Stringinstances instead of asTestEnuminstances, causingClassCastExceptionto be raised at runtime. This is due to the fact that thevisitedTypeVariablesmap receives an entry forT, resolves it properly, and then upon subsequent attempt to resolveTfails, since thevisitedTypeVariablesset indicates thatThas already been resolved.