Skip to content

Recursive TypeVariable resolution results in ClassCastException when type var is referenced multiple times #1390

@mcumings

Description

@mcumings

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions