mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Ignore Properties on Multiple Derived Types and Base Class Simultaneously

Open cmeyertons opened this issue 1 year ago • 4 comments

Describe the bug This is related to: https://github.com/riok/mapperly/issues/694

To Reproduce Have ignores set up on separate derive types + the base class for a given mapping structure.

Results in RMG021: IgnoredSourceMember was not found

There is currently no way to Ignore base mappings due to the inability to apply the MapDerivedType logic to an existing mapping. Expected behavior A clear and concise description of what you expected to happen.

I would expect the ability to separately ignore members at the base class level, and at each of the derived types without introducing errors or suppressions.

Code snippets

public class SourceBase
{
    public int BaseId { get; set; }
}

public class Source : SourceBase
{
    public int Id { get; set; }
}

public class AnotherSource: SourceBase
{
    public int NewProp { get; set; }
}

public class TargetBase // missing ID
{ }

public class Target : TargetBase
{ }

public class AnotherTarget : TargetBase
{}

[Mapper]
internal static partial class TestMapper
{
    [MapDerivedType<Source, Target>]
    [MapDerivedType<AnotherSource, AnotherTarget>]
    [MapperIgnoreSource(nameof(SourceBase.BaseId))]
    [MapperIgnoreSource(nameof(Source.Id))]
    [MapperIgnoreSource(nameof(AnotherSource.NewProp))]
    public static partial TargetBase ToTarget(this SourceBase source);
}

Environment (please complete the following information):

  • Mapperly Version: e.g. 3.2.0
  • .NET Version: e.g. .NET 7.0.*
  • Target Framework: .net7.0
  • IDE: Visual Studio v17.8
  • OS: Windows 10

cmeyertons avatar Nov 16 '23 15:11 cmeyertons

This would be a great enhancement. Would be happy to accept a PR, let me know if you want to contribute and need any hints 😊

latonz avatar Nov 19 '23 10:11 latonz

The problem I see here is how to decide which configurations apply to which type mappings. Should all applied to all? Even if there is a specific mapping method for a given type pair?

latonz avatar Mar 03 '24 01:03 latonz

Could it follow the existing precedence conventions / behaviors that already exist?

cmeyertons avatar Mar 03 '24 14:03 cmeyertons

What conventions are you referring to?

latonz avatar Mar 05 '24 04:03 latonz

Closing in favor of the new MapperIgnoreAttribute that allows ignoring properties at source.

latonz avatar May 28 '24 03:05 latonz