fury icon indicating copy to clipboard operation
fury copied to clipboard

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

Open mof-dev-3 opened this issue 3 years ago • 4 comments

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 avatar Nov 13 '23 03:11 mof-dev-3

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

mof-dev-3 avatar Nov 13 '23 03:11 mof-dev-3

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 avatar Nov 13 '23 13:11 chaokunyang

This is a bug introduced in #923

chaokunyang avatar Nov 13 '23 13:11 chaokunyang

Fury Version

0.7.1

JDK Version

21

Problem Description

When using Fury for serialization, a NullPointerException occurred. Here is the error message: java.lang.NullPointerException at org.apache.fury.resolver.ClassResolver.createSerializer(ClassResolver.java:1177) at org.apache.fury.resolver.ClassResolver.getClassInfo(ClassResolver.java:1108) at org.apache.fury.serializer.collection.AbstractCollectionSerializer.writeTypeHeader(AbstractCollectionSerializer.java:203) at org.apache.fury.serializer.collection.AbstractCollectionSerializer.writeElementsHeader(AbstractCollectionSerializer.java:135) at org.apache.fury.serializer.collection.AbstractCollectionSerializer.writeElements(AbstractCollectionSerializer.java:310) at org.apache.fury.serializer.collection.AbstractCollectionSerializer.write(AbstractCollectionSerializer.java:305) at org.apache.fury.Fury.writeNonRef(Fury.java:475) at org.apache.fury.serializer.ObjectSerializer.writeContainerFieldValue(ObjectSerializer.java:213) at org.apache.fury.serializer.ObjectSerializer.writeContainerFields(ObjectSerializer.java:195) at org.apache.fury.serializer.ObjectSerializer.write(ObjectSerializer.java:146) at org.apache.fury.serializer.collection.AbstractCollectionSerializer.writeSameTypeElements(AbstractCollectionSerializer.java:404) at org.apache.fury.serializer.collection.AbstractCollectionSerializer.generalJavaWrite(AbstractCollectionSerializer.java:391) at org.apache.fury.serializer.collection.AbstractCollectionSerializer.javaWriteWithGenerics(AbstractCollectionSerializer.java:369) at org.apache.fury.serializer.collection.AbstractCollectionSerializer.writeElements(AbstractCollectionSerializer.java:318) at org.apache.fury.serializer.collection.AbstractCollectionSerializer.write(AbstractCollectionSerializer.java:305) at org.apache.fury.Fury.writeNonRef(Fury.java:475) at org.apache.fury.serializer.ObjectSerializer.writeContainerFieldValue(ObjectSerializer.java:213) at org.apache.fury.serializer.ObjectSerializer.writeContainerFields(ObjectSerializer.java:195) at org.apache.fury.serializer.ObjectSerializer.write(ObjectSerializer.java:146) at org.apache.fury.Fury.writeData(Fury.java:584) at org.apache.fury.Fury.write(Fury.java:347) at org.apache.fury.Fury.serialize(Fury.java:269) at org.apache.fury.Fury.lambda$serialize$0(Fury.java:285) at org.apache.fury.Fury.serializeToStream(Fury.java:1427) at org.apache.fury.Fury.serialize(Fury.java:285) at com.ctrip.fury.FuryTest.testFurySerialize2(FuryTest.java:50) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)

Sample Code

` public class FuryTest {

@Test
public void testFurySerialize2() throws IOException {
    Fury fury = Fury.builder()
            .withLanguage(Language.JAVA)
            .withRefTracking(true)
            .withCompatibleMode(CompatibleMode.COMPATIBLE)
            .withDeserializeNonexistentClass(true)
            .requireClassRegistration(false)
            .withAsyncCompilation(true)
            .build();

    List<Data> dataList = new ArrayList<>();
    dataList.add(null);
    DataHolder holder = new DataHolder();
    holder.setDataList(dataList);

    FuryTestResponse response = new FuryTestResponse();
    response.setHolderList(Lists.newArrayList(holder));
    fury.serialize(new CountingOutputStream(newOutputStream(Paths.get("test"))), response);
}

@Getter
@Setter
class Data {
    private String message;
}

@Getter
@Setter
class DataHolder {
    private List<Data> dataList;

}

@Getter
@Setter
class FuryTestResponse {
    private List<DataHolder> holderList;
}

} `

Thank you for your time and effort in looking into this issue. I appreciate your help in resolving this problem.

GimoXie avatar Mar 05 '25 03:03 GimoXie