Skip to content

Serialization may throw an exception when a generic bound type is discovered, but not of type Class #682

@jamezp

Description

@jamezp

Describe the bug
In #670 an attempt was made to fix a StackOverflowError attempting to resolve a generic type. The fix was not quite right and will throw an exception if the type seems to have been resolved, but the search type and the discovered type are the same.

The original fix was misguided and if the types are equal, the type should simply be returned.

To Reproduce

Consider the following types:

public static class ListContainer<T> {

    private List<T> list;

    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }

}
public static class UniformTree<T extends UniformTree<T>> {

    private List<T> children = new ArrayList<>();

    public List<T> getChildren() {
        return children;
    }

    public void setChildren(List<T> children) {
        this.children = children;
    }

}
public static class ExampleTree extends UniformTree<ExampleTree> {

    private String another = "another";

    public String getAnother() {
        return another;
    }

    public void setAnother(String another) {
        this.another = another;
    }

}

Then consider this test:

public class JsonbTest {

    @Test
    public void testJsonb() throws Exception {
        try (Jsonb jsonb = JsonbBuilder.create()) {
            ListContainer<ExampleTree> lt2 = new ListContainer<>();

            ExampleTree et2 = new ExampleTree();
            et2.setChildren(List.of(new ExampleTree()));
            lt2.setList(List.of(et2));
            Assertions.assertEquals("{\"list\":[{\"children\":[{\"children\":[],\"another\":\"another\"}],\"another\":\"another\"}]}", jsonb.toJson(lt2));
        }

    }
}

When an attempt to resolve UniformTree<T> in the toJson(lt2) in the ReflectionUtils.resolveTypeArguments() the resolved type and the typeToResolve are the same resulting in an IllegalStateException being thrown. However, this should simply return the type as it's already been resolved.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working right

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions