{"action":"opened","issue":{"url":"https://api.github.com/repos/michaeledgar/protobuf-net/issues/286","labels_url":"https://api.github.com/repos/michaeledgar/protobuf-net/issues/286/labels{/name}","comments_url":"https://api.github.com/repos/michaeledgar/protobuf-net/issues/286/comments","events_url":"https://api.github.com/repos/michaeledgar/protobuf-net/issues/286/events","html_url":"https://github.com/michaeledgar/protobuf-net/issues/286","id":58234053,"number":286,"title":"Wrapped linked list with a cycle is not properly deserialized","user":{"login":"GoogleCodeExporter","id":9614759,"avatar_url":"https://avatars.githubusercontent.com/u/9614759?v=3","gravatar_id":"","url":"https://api.github.com/users/GoogleCodeExporter","html_url":"https://github.com/GoogleCodeExporter","followers_url":"https://api.github.com/users/GoogleCodeExporter/followers","following_url":"https://api.github.com/users/GoogleCodeExporter/following{/other_user}","gists_url":"https://api.github.com/users/GoogleCodeExporter/gists{/gist_id}","starred_url":"https://api.github.com/users/GoogleCodeExporter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/GoogleCodeExporter/subscriptions","organizations_url":"https://api.github.com/users/GoogleCodeExporter/orgs","repos_url":"https://api.github.com/users/GoogleCodeExporter/repos","events_url":"https://api.github.com/users/GoogleCodeExporter/events{/privacy}","received_events_url":"https://api.github.com/users/GoogleCodeExporter/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/michaeledgar/protobuf-net/labels/auto-migrated","name":"auto-migrated","color":"ededed"},{"url":"https://api.github.com/repos/michaeledgar/protobuf-net/labels/Priority-Medium","name":"Priority-Medium","color":"ededed"},{"url":"https://api.github.com/repos/michaeledgar/protobuf-net/labels/Type-Defect","name":"Type-Defect","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":0,"created_at":"2015-02-19T16:45:11Z","updated_at":"2015-02-19T16:45:11Z","closed_at":null,"body":"```\nWhat steps will reproduce the problem?\n\n    public class Element\n    {\n        static private int counter = 0;\n        private int id;\n\n        public int Num;\n        public Element Next;\n\n        public Element()\n        {\n            id = counter;\n            counter++;\n        }\n    }\n\n    public class CircularList\n    {\n        public int Key = 0;\n\n        public Element Root;\n    }\n\n    [TestMethod]\n    public void TestCircularList()\n    {\n        var obj = new CircularList();\n\n        obj.Root = new Element ();\n        obj.Root.Num = 1;\n        obj.Root.Next = new Element();\n        obj.Root.Next.Num = 2;\n        obj.Root.Next.Next = obj.Root;\n\n        var model = TypeModel.Create();\n        var topMeta = model.Add(typeof(CircularList), false);\n\n        topMeta.Add(1, \"Key\");\n        topMeta.Add(2, \"Root\");\n\n        var elemMeta = model.Add(typeof(Element), false);\n        elemMeta.AsReferenceDefault = true;\n\n        elemMeta.Add(1, \"Num\");\n        elemMeta.Add(2, \"Next\");\n\n        model.CompileInPlace();\n\n        using (var stream = new MemoryStream())\n        {\n            model.Serialize(stream, obj);\n\n            stream.Position = 0;\n\n            var resultObject = model.Deserialize(stream, null, typeof(CircularList)) as CircularList;\n\n            Assert.AreSame(resultObject.Root, resultObject.Root.Next.Next);\n        }\n    }\n\n\nWhat is the expected output? What do you see instead?\n\nThe assert should pass, but it fails. It looks like one extra element is \ngenerated in the resulting list and the cycle is shifted by one. The private id \nfield should help to identify the Element objects.\n\nthis seems to work, if the list is not wrapped in another class. See the \nfollowing test:\n\n    [TestMethod]\n    public void TestCircularRefs()\n    {\n        Element root = new Element();\n        root.Num = 1;\n        root.Next = new Element();\n        root.Next.Num = 2;\n        root.Next.Next = root;\n\n        var model = TypeModel.Create();\n\n        var elemMeta = model.Add(typeof(Element), false);\n        elemMeta.AsReferenceDefault = true;\n\n        elemMeta.Add(1, \"Num\");\n        elemMeta.Add(2, \"Next\");\n\n        model.CompileInPlace();\n\n        using (var stream = new MemoryStream())\n        {\n            model.Serialize(stream, root);\n\n            stream.Position = 0;\n\n            var resultObject = model.Deserialize(stream, null, typeof(Element)) as Element;\n\n            Assert.AreSame(resultObject, resultObject.Next.Next);\n        }\n    }\n\n\nWhat version of the product are you using? On what operating system?\n\nv2 Beta2 on Windows 7\n\nRegards,\nSlobodan\n\n```\n\n-----\nOriginal issue reported on code.google.com by `s.m.boja...@gmail.com` on 11 Apr 2012 at 11:21"}}