Skip to content

StandardResolverAllowPrivate overwrites readonly fields set in constructor #1218

Closed

Description

@Alxandr

Bug description

Readonly fields overwritten after constructor when using allow private.

Repro steps

[MessagePackObject]
public class Test
{
    [Key(0)]
    readonly string _test;

    [SerializationConstructor]
    public Test(string test)
    {
        _test = test ?? "I am never null :D";
    }

    [IgnoreMember]
    public string Prop => _test;
}

[Fact]
public void AllowPrivateTest()
{
    // write an empty array to seq
    using var seq = new Sequence<byte>();
    var writer = new MessagePackWriter(seq);
    writer.WriteArrayHeader(0);
    writer.Flush();

    // deserialize as Test
    var data = seq.AsReadOnlySequence;
    var resolver = StandardResolverAllowPrivate.Instance;
    var options = MessagePackSerializerOptions.Standard.WithResolver(StandardResolverAllowPrivate.Instance);
    var test = MessagePackSerializer.Deserialize<Test>(in data, options);

    // check test
    Assert.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.

  • Version used: v2.2.85
  • Runtime: (e.g. .NET Framework, .NET Core, Unity, mono, etc.) .NET Core 3.1

Additional context

I believe the issue stems from the following:

member = new EmittableMember
{
    FieldInfo = field,
    IsReadable = allowPrivate || field.IsPublic,
    IsWritable = allowPrivate || (field.IsPublic && !field.IsInitOnly),
    StringKey = firstMemberByName ? item.Name : $"{item.DeclaringType.FullName}.{item.Name}",
};

In here the test for IsWritable will always be true if allowPrivate is true. Rather, it should probably be (allowPrivate || field.IsPublic) && !field.IsInitOnly.

Activity

  1. AArnott commented on Jun 13, 2021

    @AArnott
    Collaborator

    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?

  2. changed the title [-]StandardResolverAllowPrivate overwrites readonly properties set in constructor.[/-] [+]StandardResolverAllowPrivate overwrites readonly fields set in constructor[/+] on Jun 13, 2021
  3. Alxandr commented on Jun 14, 2021

    @Alxandr
    ContributorAuthor

    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.

  4. added a commit that references this issue on Jun 14, 2021
    db29bea
  5. added this to the v2.3 milestone on Jun 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions