denghongcai / protobuf-net

Automatically exported from code.google.com/p/protobuf-net
Other
0 stars 0 forks source link

[r488] Exception deserializing with DynamicType. Invalid field in source data: 0 #284

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
Please include an e-mail address if this might need a dialogue!
==============

What steps will reproduce the problem?
    public class Program
    {
        public static void Main(string[] args)
        {
            MyArgs test = new MyArgs
            {
                Value = 12,
            };

            byte[] buffer = new byte[256];
            using (MemoryStream ms = new MemoryStream(buffer))
            {
                Serializer.Serialize(ms, test);
            }

            using (MemoryStream ms = new MemoryStream(buffer))
            {
                Serializer.Deserialize<MyArgs>(ms);
            }
        }
    }

    [ProtoContract]
    public class MyArgs
    {
        [ProtoMember(1, DynamicType = true)]
        public object Value;
    } 

What is the expected output? What do you see instead?
Unhandled Exception: ProtoBuf.ProtoException: Invalid field in source data: 0
   at ProtoBuf.ProtoReader.ReadFieldHeader() in C:\temp\protosource\trunk\protobuf-net\ProtoReader.cs:line 623
   at proto_2(Object , ProtoReader )
   at ProtoBuf.Serializers.CompiledSerializer.ProtoBuf.Serializers.IProtoSerializer.Read(Object value, ProtoReader sourc
e) in 
C:\temp\protosource\trunk\protobuf-net\Serializers\CompiledSerializer.cs:line 49
   at ProtoBuf.Meta.RuntimeTypeModel.Deserialize(Int32 key, Object value, ProtoReader source) in C:\temp\protosource\tru
nk\protobuf-net\Meta\RuntimeTypeModel.cs:line 419
   at ProtoBuf.Meta.TypeModel.DeserializeCore(ProtoReader reader, Type type, Object value, Boolean noAutoCreate) in C:\t
emp\protosource\trunk\protobuf-net\Meta\TypeModel.cs:line 591
   at ProtoBuf.Meta.TypeModel.Deserialize(Stream source, Object value, Type type, SerializationContext context) in C:\te
mp\protosource\trunk\protobuf-net\Meta\TypeModel.cs:line 517
   at ProtoBuf.Serializer.Deserialize[T](Stream source) in C:\temp\protosource\trunk\protobuf-net\Serializer.cs:line 69

What version of the product are you using? On what operating system?
Windows 7 protobuf-net.
This repro fails with the official v2 release due to another bug. That was 
fixed in r488. But r486 introduced this bug.

Please provide any additional information below.
jay.thaler@gmail.com

Original issue reported on code.google.com by jay.tha...@gmail.com on 29 Mar 2012 at 4:03

GoogleCodeExporter commented 9 years ago
At the current time, this functionality is intended for use with dynamic 
*contract* types; I have added specific messages to clarify the problem (so it 
isn't a mystery), however, adding full support for all types (including inbuilt 
types) is a bit more involved. Can probably do it, but not today (and am about 
to disappear for a few days).

Original comment by marc.gravell on 30 Mar 2012 at 5:55

GoogleCodeExporter commented 9 years ago
I'm seeing the same issue here with r580 with non-contract types. Given 
something like:

    class Program {
        static void Main(string[] args) {
            cls val = new cls();
            val.something = 123.45;
            Serializer.DeepClone(val);
        }
    }
    [ProtoContract]
    public class cls {
        [ProtoMember(1, DynamicType = true)]
        public object something; // { get; set; }

The DeepClone call complains: "Dynamic type is not a contract-type: Double" 
(same with other non-contract types, eg int).

Original comment by j...@tabascoterrier.net on 22 Aug 2012 at 1:54

GoogleCodeExporter commented 9 years ago
Yes, this regression is *basically* why I haven't made a 58* build "listed" on 
NuGet or "featured" on google-code. This is on my list to investigate ASAP.

Original comment by marc.gravell on 22 Aug 2012 at 2:12

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
I'm having the same problem with r602.

My test

[TestMethod]
public void Execute()
{
    var m = RuntimeTypeModel.Default;
    m.DynamicTypeFormatting += OnDynamicTypeFormatting; 
    var fieldDefinition = new IntegerFieldDefinition();
    fieldDefinition.DefaultValue = 60;
    fieldDefinition.MinValue = 10;
    fieldDefinition.MaxValue = 999;
    var cloneTest = CloneTest(fieldDefinition);

    Assert.AreEqual(fieldDefinition.DefaultValue, cloneTest.DefaultValue);
    Assert.AreEqual(fieldDefinition.MinValue, cloneTest.MinValue);
    Assert.AreEqual(fieldDefinition.MaxValue, cloneTest.MaxValue);
}

public void OnDynamicTypeFormatting(object sender, TypeFormatEventArgs args)
{
    if (args.Type == typeof(Int32))
    {
        args.FormattedName = "System.Int32";
    }
    else if (args.FormattedName == "System.Int32")
    {
        args.Type = typeof(System.Int32);
    }
}

The event OnDynamicTypeFormatting is not called

The DefaultValue property of class IntegerFieldDefinition

[ProtoMember(7, DynamicType = true)]
public object DefaultValue {
    get { return _defaultValue; }
    set { _defaultValue = value; }
}

Original comment by h.rickma...@gmail.com on 10 Dec 2012 at 1:43

GoogleCodeExporter commented 9 years ago
Having this issue in 2.0.0.668
 [ProtoContract]
        [ProtoInclude(100, typeof(string))]
        [ProtoInclude(101, typeof(int))]
        internal class ZZZZ
        {
            [ProtoMember(1, DynamicType = true)] 
            private object _value = 56;
            [ProtoMember(2, DynamicType = true)]
            private object _value2 = "RRRRR";
        }

        private void TestProtobuf()
        {
            var zzz = new ZZZZ();

            using (var memoryStream = new MemoryStream())
            {
                Serializer.Serialize(memoryStream, zzz);

            }
        }

Original comment by evgen...@gmail.com on 27 Nov 2013 at 8:09