title: Why does protostuff corrupt objects when using generics combined with circular references?
body: <p>I am using protostuff binary with circular references and generics. As a very simplistic scenario i have the following classes:</p>
  
  <pre><code>    public class A {
  
      private String name;
      private B b;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public B getB() {
          return b;
      }
  
      public void setB(B b) {
          this.b = b;
          this.b.setA(this);
      }
  }
  </code></pre>
  
  <p>///////////////////////////////////////////////////////////////////////////////</p>
  
  <pre><code>public class B {
  
      private String name;
      private A a;
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
      public A getA() {
          return a;
      }
  
      public void setA(A a) {
          this.a = a;
      }
  }
  </code></pre>
  
  <p>///////////////////////////////////////////////////////////////////////////////</p>
  
  <pre><code>public class Container&lt;E&gt; {
      private E element;
      private String name;
  
      public Container() {
      }
  
      public Container(E e, String name) {
          super();
          this.element = e;
          this.name = name;
      }
  
      public E getElement() {
          return element;
      }
  
      public void setElement(E e) {
          this.element = e;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  }
  </code></pre>
  
  <p>When i run the following unit test to check that the round trip serialisation / deserialisation has executed correctly i get a very odd result. the last assertion fails:</p>
  
  <pre><code>public class CircularRefTest {
  
      @Test
      public void testCircularReferences() {
  
          A a = new A();
          a.setName("a");
          B b = new B();
          b.setName("b");
          a.setB(b);
          Container&lt;A&gt; container = new Container&lt;A&gt;(a, "container");
  
          Schema&lt;Container&gt; schema = RuntimeSchema.getSchema(Container.class);
          LinkedBuffer buffer = LinkedBuffer.allocate(256);
          byte[] data = GraphIOUtil.toByteArray(container, schema, buffer);
  
          Container&lt;A&gt; copy = new Container&lt;A&gt;();
          GraphIOUtil.mergeFrom(data, copy, schema);
  
          assertEquals(container.getName(), copy.getName());
          assertEquals(container.getElement().getName(), copy.getElement().getName());
          assertEquals(container.getElement().getB().getName(), copy.getElement().getB().getName());
  
          // something weird happens here with the circular references here
          System.out.println(copy.getElement().getB().getA().getClass());
          assertTrue(copy.getElement().getB().getA() instanceof A); // fails
      }
  }
  </code></pre>
  
  <p>Protostuff is corrupting the circular references from the child class back up to the parent. The last assertion should pass, but for some reason the class is of type Container. </p>
  
  <p>What am i doing wrong ? </p>
  
  <p>If i change the Container class to use strongly typed objects then the unit test passes.  Is this a bug ?? </p>
  
  <p>The maven artifact i am using is: </p>
  
  <pre><code>&lt;dependency&gt;
          &lt;groupId&gt;com.dyuproject.protostuff&lt;/groupId&gt;
          &lt;artifactId&gt;protostuff-api&lt;/artifactId&gt;
          &lt;version&gt;1.0.4&lt;/version&gt;
      &lt;/dependency&gt;
  </code></pre>