You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Readonly Dictionary with DataMember.Order is not being deserialized #1308
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]publicclassMyClass{[DataMember(Name="Key",Order=0,EmitDefaultValue=false)]privatereadonlyGuid?_key;[DataMember(Name="Body",Order=1,EmitDefaultValue=false)]privatereadonlyDictionary<string,object>_body=newDictionary<string,object>();publicMyClass(Guid?key){_key=key;}publicvoidSetValue(stringname,objectvalue){_body[name]=value;}}varobj=newMyClass(Guid.NewGuid());obj.SetValue("name","my name");varopt=MessagePack.MessagePackSerializerOptions.Standard.WithResolver(MessagePack.Resolvers.ContractlessStandardResolverAllowPrivate.Instance);varbytes=MessagePack.MessagePackSerializer.Serialize(obj,opt);varjson=MessagePack.MessagePackSerializer.ConvertToJson(bytes);// just for check that json has _body' values.varafObj=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.
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.
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?
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.
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.
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.
Activity
AArnott commentedon Aug 21, 2021
This may be an unintentional regression, which I will look into.
In the meantime, I'm curious why you're using
ContractlessStandardResolverAllowPrivatewhen you in fact are serializing a type that uses DCS attributes, so you should be able to use the default resolver.Add test for a DCS type with a private, readonly dictionary
AArnott commentedon Aug 21, 2021
From
git bisectI see that this regressed in db29bea (which fixed #1218).AArnott commentedon Aug 21, 2021
Ok, so the only reason this worked in 2.1 is because we were setting
readonlyfields, which feels wrong. TheDataContractSerializerdoes 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
.Addon the collection to populate it.Add test for a DCS type with a private, readonly dictionary
Skip setting fields when the ctor already took the value
Skip setting fields when the ctor already took the value
OlegNadymov commentedon Aug 25, 2021
@AArnott I have different cases in my real project. So I have to use
ContractlessStandardResolverAllowPrivate.OlegNadymov commentedon Aug 25, 2021
@AArnott thank you for this fix #1314
Could you tell me when I can expect the fix in new version on Nuget?
AArnott commentedon Sep 2, 2021
We can service v2.3 on nuget.org by early next week.