stag-java icon indicating copy to clipboard operation
stag-java copied to clipboard

TypeAdapters for cross referential generic classes throw StackOverflowExceptions

Open anthonycr opened this issue 8 years ago • 1 comments

Issue Summary

If you have two generic classes (e.g. Model1<T> and Model2<T>) which each have a field that references the other, the TypeAdapters for these classes will not be able to be instantiated. This is because in the constructor of a generic type adapter, all the necessary type adapters are created using new TypeAdapter. This means that Model1 instantiates Model2 in its constructor, and Model2 instantiates Model1 in its constructor, creating an infinite chain of instantiation.

Reproduction Steps

Create two generic classes that reference each other, and try to instantiate the TypeAdapters created.

Expected Behavior

The TypeAdapter should be correctly instantiated.

Actual Behavior

The TypeAdapter constructor throws a StackOverflowException

Note: Non-generic cross referential models work as expected

anthonycr avatar Feb 05 '17 15:02 anthonycr

See this branch for test cases https://github.com/vimeo/stag-java/tree/recursively-instantiated-generic-adapter

GenericCrossReferentialModel1: https://github.com/vimeo/stag-java/blob/recursively-instantiated-generic-adapter/sample-model/src/main/java/com/vimeo/sample_model/GenericCrossReferentialModel1.java GenericCrossReferentialModle2: https://github.com/vimeo/stag-java/blob/recursively-instantiated-generic-adapter/sample-model/src/main/java/com/vimeo/sample_model/GenericCrossReferentialModel1.java

anthonycr avatar Feb 05 '17 15:02 anthonycr