Skip to content

Analyzers (and possibly AOT) adds requirement for Key/ctor ordering #1993

Description

@pkindruk

I have tried MessagePack 3.0.134-beta on my project. So far new analyzer detections force a very strict style that requires a lot of refactoring to get it going. This might be not the best code, but on large code base refactoring is always a matter of time you can spend for this. Analyzer for 2.5.172 was fine with that code.

Questions:

  1. Test1 - Why are we forced to start with 0 for constructor params? For me it is not always the case and I cannot change that.
  2. Test2, Test3 - Requirement to make partial class doesn't make sense. Code gen still has to use constructor.
  3. Test4 - Maybe analyzer should detect private member not annotated with attributes only when class is partial?

Common patterns detected:

[MessagePackObject]
public class Test1
{
    [Key(1)]
    //[Key("1")]// works fine with string keys
    public string Name { get; }

    [SerializationConstructor]
    public Test1(string name) => Name = name;
}

[MessagePackObject]
public class Test2
{
    private readonly string _name;

    [Key(0)]
    public string Name => _name;

    [SerializationConstructor]
    public Test2(string name) => _name = name;
}

// another way Test2 can implemented
[MessagePackObject]
public class Test3
{
    [Key(0)]
    public string Name { get; private set; }

    [SerializationConstructor]
    public Test3(string name) => Name = name;
}

[MessagePackObject]
public class Test4
{
    private int? _cachedValue;// not serialized by design

    [Key(0)]
    public string Name { get; set; }
}

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions