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
StandardResolverAllowPrivate overwrites readonly fields set in constructor #1218
Readonly fields overwritten after constructor when using allow private.
Repro steps
[MessagePackObject]publicclassTest{[Key(0)]readonlystring_test;[SerializationConstructor]publicTest(stringtest){_test=test??"I am never null :D";}[IgnoreMember]publicstringProp=>_test;}[Fact]publicvoidAllowPrivateTest(){// write an empty array to sequsingvarseq=newSequence<byte>();varwriter=newMessagePackWriter(seq);writer.WriteArrayHeader(0);writer.Flush();// deserialize as Testvardata=seq.AsReadOnlySequence;varresolver=StandardResolverAllowPrivate.Instance;varoptions=MessagePackSerializerOptions.Standard.WithResolver(StandardResolverAllowPrivate.Instance);vartest=MessagePackSerializer.Deserialize<Test>(indata,options);// check testAssert.NotNull(test.Prop);}
The generated code here produces a call to the constructor (which assigns _test), then an assignment to the field _test.
Expected behavior
Only the constructor should be called, the field should not be overwritten after.
Actual behavior
The field is overwritten after the constructor has returned.
In here the test for IsWritable will always be true if allowPrivate is true. Rather, it should probably be (allowPrivate || field.IsPublic) && !field.IsInitOnly.
Good analysis of the problem. Your issue title says "properties" but your example uses fields. Is the problem restricted to fields?
Would you be interested in submitting a PR, including a unit test?
changed the title [-]StandardResolverAllowPrivate overwrites readonly properties set in constructor.[/-][+]StandardResolverAllowPrivate overwrites readonly fields set in constructor[/+]on Jun 13, 2021
Activity
AArnott commentedon Jun 13, 2021
Good analysis of the problem. Your issue title says "properties" but your example uses fields. Is the problem restricted to fields?
Would you be interested in submitting a PR, including a unit test?
[-]StandardResolverAllowPrivate overwrites readonly properties set in constructor.[/-][+]StandardResolverAllowPrivate overwrites readonly fields set in constructor[/+]Alxandr commentedon Jun 14, 2021
You're correct, it's related to fields. We fixed it by changing to properties :P, so that's probably where I got mixed up. I can try to drum up a PR.
Fix handling of readonly private fields