apache / fury

A blazingly fast multi-language serialization framework powered by JIT and zero-copy.
https://fury.apache.org/
Apache License 2.0
3.08k stars 246 forks source link

[Java] AbstractCollectionSerializer serialize Collection with only null object inside has NPE problem #1085

Closed mof-dev-3 closed 12 months ago

mof-dev-3 commented 12 months ago

How to reproduce ` public static class TestCollectionObj {

    private T value;

    public TestCollectionObj(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public void setValue(T value) {
        this.value = value;
    }

}

@Test
public void testCollection() {
    List data = new ArrayList<>();
    data.add(null);
    TestCollectionObj h = new TestCollectionObj(data);
    Fury f = Fury.builder()
            .withLanguage(Language.JAVA)
            .build();
    f.register(TestCollectionObj.class);
    byte[] serialize = f.serialize(h);
    Object deserialize = f.deserialize(serialize);
    Assert.assertEquals(deserialize.getClass(), h.getClass());
    TestCollectionObj h2 = (TestCollectionObj) deserialize;
    Assert.assertEquals(h2.getValue(), h.getValue());
}

`

Expected behavior serialize with no exception

maybe we should check elemGenericType is null case and write a special flag like Fury.NULL_FLAG instead

image

Environment (please complete the following information): openjdk version "1.8.0_312" ubuntu 20

mof-dev-3 commented 12 months ago

Pls also consider following variant case AbstractCollectionSerializer.writeTypeNullabilityHeader method, which fail the NPE assert

`

public class CollectionsTest {

public static class TestCollectionHolder<T> {
    private List<T> data;
    public List getData() {
        return data;
    }

    public void setData(List data) {
        this.data = data;
    }

    public TestCollectionHolder(List data) {
        this.data = data;
    }

}

@org.testng.annotations.Test
public void testCollection() {
    List data = new ArrayList<>();
    data.add(null);
    CollectionsTest.TestCollectionHolder h = new CollectionsTest.TestCollectionHolder(data);
    Fury f = Fury.builder()
            .withLanguage(Language.JAVA)
            .build();
    f.register(CollectionsTest.TestCollectionHolder.class);
    byte[] serialize = f.serialize(h);
    Object deserialize = f.deserialize(serialize);
    Assert.assertEquals(deserialize.getClass(), h.getClass());
    CollectionsTest.TestCollectionHolder h2 = (CollectionsTest.TestCollectionHolder) deserialize;
    Assert.assertEquals(h2.getData(), h.getData());
}

}

`

image

image

chaokunyang commented 12 months ago

Thanks for creating this issue @mof-dev-3 , I can reproduce it. I fixed it in https://github.com/alipay/fury/pull/1086 , please try it out and reopen this issue if the bug still persists.

chaokunyang commented 12 months ago

This is a bug introduced in #923