telenths / protostuff

Automatically exported from code.google.com/p/protostuff
Apache License 2.0
0 stars 0 forks source link

graph serialization bug (protostuff-runtime) on collections/map without generics and dynamic objects #85

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago

public class LinkedListBeanTest extends TestCase {

    protected void setUp() throws Exception {
        super.setUp();
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testGraph() {
        LinkedListBean bean = new LinkedListBean();
        bean.aname = "test";
        Item share = new Item("share");
        LinkedList<Item> linkedList = new LinkedList<Item>();
        linkedList.add(share);
        bean.linkedList = linkedList;
        List<Item> list = new ArrayList<Item>();
        list.add(share);
        bean.list = list;

        Schema<LinkedListBean> schema = RuntimeSchema.getSchema(LinkedListBean.class);
        byte[] bytes = GraphIOUtil.toByteArray(bean, schema, LinkedBuffer.allocate(256));
        LinkedListBean deBean = new LinkedListBean();
        GraphIOUtil.mergeFrom(bytes, deBean, schema);
        System.out.println("linkedList:"+deBean.linkedList);
        //output: linkedList:[com.alibaba.cat.bean.LinkedListBean@1050169]
        // why??
        System.out.println("list:"+deBean.list);
        //output: list:[name:share]  

    }

}

class LinkedListBean {

    public List       list;
    public LinkedList linkedList;
    public String     aname;
}

class Item {

    public String name;

    public Item(String name){
        this.name = name;
    }

    public String toString() {
        return "name:" + name;
    }
}

Original issue reported on code.google.com by without...@hotmail.com on 20 Oct 2011 at 4:40

Attachments:

GoogleCodeExporter commented 8 years ago
On test startup, do: 
System.setProperty("protostuff.runtime.collection_schema_on_repeated_fields", 
"true");

I've updated the wiki explaining why that is needed for cyclic collections.
http://code.google.com/p/protostuff/wiki/SerializingObjectGraphs (bottom part)

Original comment by david.yu...@gmail.com on 20 Oct 2011 at 9:26

GoogleCodeExporter commented 8 years ago
it still wrong.

modify source to this:

package com.alibaba.cat.bean;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import com.dyuproject.protostuff.GraphIOUtil;
import com.dyuproject.protostuff.LinkedBuffer;
import com.dyuproject.protostuff.Schema;
import com.dyuproject.protostuff.runtime.RuntimeSchema;

public class LinkedListBeanTest {

    public static void main(String[] args) {
        System.setProperty("protostuff.runtime.collection_schema_on_repeated_fields", "true");
        LinkedListBean bean = new LinkedListBean();
        bean.aname = "test";
        Item share = new Item("share");
        LinkedList<Item> linkedList = new LinkedList<Item>();
        linkedList.add(share);
        linkedList.add(new Item("aaaa"));
        bean.linkedList = linkedList;
        List<Item> list = new ArrayList<Item>();
        list.add(share);
        list.add(new Item("bbbb"));
        bean.list = list;

        Schema<LinkedListBean> schema = RuntimeSchema.getSchema(LinkedListBean.class);
        byte[] bytes = GraphIOUtil.toByteArray(bean, schema, LinkedBuffer.allocate(256));
        LinkedListBean deBean = new LinkedListBean();
        GraphIOUtil.mergeFrom(bytes, deBean, schema);
        System.out.println("linkedList:" + deBean.linkedList);
        //linkedList:[[name:share, name:bbbb], name:aaaa]
        System.out.println("list:" + deBean.list);
        //list:[name:share, name:bbbb]

    }

}

class LinkedListBean {

    public List       list;
    public LinkedList linkedList;
    public String     aname;
}

class Item {

    public String name;

    public Item(String name){
        this.name = name;
    }

    public String toString() {
        return "name:" + name;
    }
}

thanks, help!

Original comment by without...@hotmail.com on 20 Oct 2011 at 11:00

GoogleCodeExporter commented 8 years ago
It looks like you've found a bug.  I've a fix w/c I'll commit in a little bit.

If you try to change the declaration from:
    public List       list;
    public LinkedList linkedList;
to:
    public List<Item>       list;
    public LinkedList<Item> linkedList;

It will work.  But without the generics, it will fail.  

With the fix, both scenarios will work.

Original comment by david.yu...@gmail.com on 20 Oct 2011 at 3:37

GoogleCodeExporter commented 8 years ago
thanks

Original comment by without...@hotmail.com on 21 Oct 2011 at 4:28

GoogleCodeExporter commented 8 years ago
I've committed the fix.  Can you try it out and report back if you still have 
problems? 
I still have one remaining issue to fix regarding EnumMaps on graph io.

Collections without generics will be serialized differently (larger size 
because of type metadata) compared to having them.
E.g on production systems, you cannot just refactor to add/remove generics if 
you want to keep compatibility on the wire.

I'll be adding more detailed docs regarding refactoring/schema evolution on 
dynamic types.

Original comment by david.yu...@gmail.com on 21 Oct 2011 at 5:47

GoogleCodeExporter commented 8 years ago
completed @ rev 1393-1395

Original comment by david.yu...@gmail.com on 22 Oct 2011 at 6:58