Skip to content

Readonly Dictionary with DataMember.Order is not being deserialized #1308

@OlegNadymov

Description

@OlegNadymov

Bug description

I have code with a class with private field of typereadonly Dictionary<string, object>.
It was working on MessagePack version 2.1.115. Then I updated MessagePack to version 2.3.75 and I got the bug. This dictionary is not being deserialized.

Repro steps

Demo:

[DataContract]
public class MyClass
{
    [DataMember(Name = "Key", Order = 0, EmitDefaultValue = false)]
    private readonly Guid? _key;

    [DataMember(Name = "Body", Order = 1, EmitDefaultValue = false)]
    private readonly Dictionary<string, object> _body = new Dictionary<string, object>();

    public MyClass(Guid? key)
    {
        _key = key;
    }

    public void SetValue(string name, object value)
    {
        _body[name] = value;
    }
}

var obj = new MyClass(Guid.NewGuid());
obj.SetValue("name", "my name");
var opt = MessagePack.MessagePackSerializerOptions.Standard.WithResolver(MessagePack.Resolvers.ContractlessStandardResolverAllowPrivate.Instance);
var bytes = MessagePack.MessagePackSerializer.Serialize(obj, opt);
var json = MessagePack.MessagePackSerializer.ConvertToJson(bytes); // just for check that json has _body' values.

var afObj = MessagePack.MessagePackSerializer.Deserialize(obj.GetType(), bytes, opt); // here afObj._body is null, it's a bug

Expected behavior

It should work like in previous versions: readonly Dictionary is being deserialized.
MsgPack_2 1 115

Actual behavior

Readonly Dictionary is not being deserialized.
I was testing: It is working again if I whether remove readonly modificator from Dictionary or Order from DataMember attributes. But it is not a good solution because the original code was working perfect on previous versions of MessagePack.
MsgPack_2 3 75

  • Version used: MessagaPack 2.3.75
  • Runtime: .Net Core 3.1

Additional context

If it is a bug I hope you will be able to fix it soon :)
In other case could you provide workaround for this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions