mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Collection property with `init` setter is not mapped

Open cremor opened this issue 2 years ago • 0 comments

Describe the bug I have a working "existing target object" (void method) mapper for a type that contains a collection property. If I change the collection property to an init setter then the collection is not mapped any more. That makes no sense because it is mapped with a private setter or with no setter at all. So an init setter should also work.

To Reproduce Steps to reproduce the behavior:

  1. Paste the code from below.
  2. Check the generated code. Notice that the collection is mapped.
  3. Modify the commented line to have an init setter.
  4. Notice that the collection is not mapped any more.
  5. Notice that the message "RMG015 Cannot map from member Source.Subs of type ICollection<SourceSub> to init only member path Target.Subs of type ICollection<TargetSub>" is shown.
  6. Change the property setter to private set. Notice that the collection is mapped again.
  7. Even weirder: Change the property setter to private init and notice that it is also mapped. Only public init isn't.

Expected behavior init collection properties should be mapped just like private set (or private init) properties and properties without any setter are mapped.

Code snippets

public class Source
{
    public required ICollection<SourceSub> Subs { get; init; }
}

public class SourceSub
{
    public required string Name { get; init; }
}

public class Target
{
    // Change this to an 'init' setter to reproduce.
    public ICollection<TargetSub> Subs { get; } = new List<TargetSub>();
}

public class TargetSub
{
    public required string Name { get; set; }
}

[Mapper]
public static partial class Mapper
{
    public static partial void MapToEntity(Source source, Target target);
}

Environment (please complete the following information):

  • Mapperly Version: 3.1.0
  • .NET Version: 7.0.400
  • Target Framework: net7.0
  • IDE: Visual Studio 2022 17.7.1
  • OS: Windows 10

cremor avatar Aug 21 '23 06:08 cremor