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

JsonMappingException through nested getter with generic wildcard return type #2331

Closed
sunchezz89 opened this issue May 22, 2019 · 4 comments
Closed
Milestone

Comments

@sunchezz89
Copy link

sunchezz89 commented May 22, 2019

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.

@cowtowncoder
Copy link
Member

Interesting. Thank you for reporting this. Definitely sounds like a bug in type resolution.

cowtowncoder added a commit that referenced this issue Jun 6, 2019
@cowtowncoder
Copy link
Member

I can reproduce this; added failing test.

@YannLeCorse
Copy link

I'm having the same kind of issue but 1 line before, when the lists are accessed (expectedTypes contains 2 elements, actualTypes is empty)

Caused by: java.lang.IndexOutOfBoundsException: Index: 0
	at java.base/java.util.Collections$EmptyList.get(Collections.java:4481)
	at com.fasterxml.jackson.databind.type.TypeFactory._resolveTypePlaceholders(TypeFactory.java:456)
	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.BeanPropertyWriter._findAndAddDynamic(BeanPropertyWriter.java:893)
	at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:705)
	at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:719)
	... 11 more

@cowtowncoder cowtowncoder added this to the 2.10.0.pr2 milestone Jul 24, 2019
@cowtowncoder cowtowncoder changed the title JsonMappingException through nested getter with generic wildcard returntype JsonMappingException through nested getter with generic wildcard return type Jul 24, 2019
@cowtowncoder
Copy link
Member

Looks like what was missing was delegation of getBindings() by ResolvedRecursiveType; adding of which resolves problem we see here, and I hope a few other reports too.

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

No branches or pull requests

3 participants