Skip to content

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

Closed

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?

Activity

  1. AArnott commented on Aug 21, 2021

    @AArnott
    Collaborator

    This may be an unintentional regression, which I will look into.

    In the meantime, I'm curious why you're using ContractlessStandardResolverAllowPrivate when you in fact are serializing a type that uses DCS attributes, so you should be able to use the default resolver.

  2. AArnott commented on Aug 21, 2021

    @AArnott
    Collaborator

    From git bisect I see that this regressed in db29bea (which fixed #1218).

  3. AArnott commented on Aug 21, 2021

    @AArnott
    Collaborator

    Ok, so the only reason this worked in 2.1 is because we were setting readonly fields, which feels wrong. The DataContractSerializer does this, but it doesn't even invoke the ctor at all (yes, that's actually possible), and MessagePackSerializer certainly doesn't copy that technique.
    The way I'd like to make this work is the same as how we might make deserializing into a collection property that exposes only a property getter: require that the collection instance is already set, and just call .Add on the collection to populate it.

  4. OlegNadymov commented on Aug 25, 2021

    @OlegNadymov
    Author

    In the meantime, I'm curious why you're using ContractlessStandardResolverAllowPrivate when you in fact are serializing a type that uses DCS attributes, so you should be able to use the default resolver.

    @AArnott I have different cases in my real project. So I have to use ContractlessStandardResolverAllowPrivate.

  5. OlegNadymov commented on Aug 25, 2021

    @OlegNadymov
    Author

    @AArnott thank you for this fix #1314
    Could you tell me when I can expect the fix in new version on Nuget?

  6. AArnott commented on Sep 2, 2021

    @AArnott
    Collaborator

    We can service v2.3 on nuget.org by early next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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