eclipse-jdt/eclipse.jdt.core

Issue with nested records and jackson deserialization.

Open

#126 opened on 2022年6月7日

GitHub で見る
 (4 comments) (0 reactions) (0 assignees)Java (183 forks)auto 404
help wanted

Repository metrics

Stars
 (233 stars)
PR merge metrics
 (PR metrics pending)

説明

Found an issue where jackson is somehow losing type information for a nested record when adding a zero-arg constructor to a record and hence getting class cast exceptions.

package pkg;

import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;

public class RecordTest {

    public static void main(String[] args) throws Exception {
        MyType type = new ObjectMapper().readValue("""
                {
                    "value": 123,
                    "list": [
                      {
                        "value": 456,
                        "value2": "789"
                      }
                    ]
                }
                """, MyType.class);

        System.out.println(type.list.get(0).getClass());
    }

    public record MyType(int value, List<MyType2> list) {
        // Adding this constructor causes weird things in Jackson
//        public MyType() {
//            this(0, emptyList());
//        }
    }

    public record MyType2(int value, String value2) {
    }
}

Running this with the extra ctor in MyType yields

Exception in thread "main" java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class pkg.RecordTest$MyType2 (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; pkg.RecordTest$MyType2 is in unnamed module of loader 'app')
	at pkg.RecordTest.main(RecordTest.java:26)

but works as expected when using javac (OpenJDK 64-Bit Server VM Zulu17.32+13-CA (build 17.0.2+8-LTS, mixed mode, sharing))

Tried this on Eclipse Version: 2022-03 (4.23.0) Build id: 20220310-1457 Eclipse Java Development Tools 3.18.1100.v20220308-0310 org.eclipse.jdt.feature.group Eclipse.org

If this is not the place for these kinds of issues please guide me to the correct place. Thanks

コントリビューターガイド