Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
mcumings opened this issue Sep 25, 2018 · 0 comments · Fixed by #1391

Comments

@mcumings
Copy link
Contributor

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.

eamonnmcmanus added a commit that referenced this issue Aug 2, 2021
Fix issue with recursive type variable protections to fix #1390
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant