Skip to content

JsonMappingException through nested getter with generic wildcard return type #2331

@sunchezz89

Description

@sunchezz89

Having the following Code, produces an Exception if i try to serialize it. Jackson v2.9.8.
Here is an Example-Project which fully reproduces the Error. Git-Project

public class Node<T extends SuperTestClass & Cloneable> extends SuperNode<Node<T>> implements Serializable {
   
    public static void main(String[] args) throws JsonProcessingException {
        Node root = new Node();
        root.children.add(new Node());

        System.out.println(new ObjectMapper().writeValueAsString(root));
    }
 
    public List<Node<T>> children;

    public Node() {
        children = new ArrayList<Node<T>>();
    }

    /**
     * The Wildcard here seems to be the Issue.
     * If we remove this full getter, everything is working as expected.
     * @return
     */
    public List<? extends SuperNode<Node<T>>> getChildren() {
        return children;
    }
}
com.fasterxml.jackson.databind.JsonMappingException: Index: 0 (through reference chain: com.test.Node["children"]->java.util.ArrayList[0])
	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:394)
	at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:365)
	at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:338)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:123)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:79)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serialize(IndexedListSerializer.java:18)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
	at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:155)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
	at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
	at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3905)
	at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3219)
	at com.test.MainJackson.main(MainJackson.java:22)
Caused by: java.lang.IndexOutOfBoundsException: Index: 0
	at java.util.Collections$EmptyList.get(Collections.java:4454)
	at com.fasterxml.jackson.databind.type.TypeFactory._verifyAndResolvePlaceholders(TypeFactory.java:505)
	at com.fasterxml.jackson.databind.type.TypeFactory._resolveTypePlaceholders(TypeFactory.java:458)
	at com.fasterxml.jackson.databind.type.TypeFactory._bindingsForSubtype(TypeFactory.java:429)
	at com.fasterxml.jackson.databind.type.TypeFactory.constructSpecializedType(TypeFactory.java:401)
	at com.fasterxml.jackson.databind.cfg.MapperConfig.constructSpecializedType(MapperConfig.java:297)
	at com.fasterxml.jackson.databind.DatabindContext.constructSpecializedType(DatabindContext.java:161)
	at com.fasterxml.jackson.databind.ser.impl.IndexedListSerializer.serializeContents(IndexedListSerializer.java:113)
	... 10 more

Please let me know if you need anything else from me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions