Skip to content

Report a diagnostic if the source and target types are equal but the method has MapProperty attributes and deep cloning is disabled #1952

@Shiko1st

Description

@Shiko1st

Let's imagine the following entity:

public class Entity
{
    public int Id { get; set; }
    public int Prop { get; set; }
}

and the following mapping class:

[Mapper]
public static class Mapper
{
    // 1
    public static partial Entity Clone(this Entity source);

    // 2
    [MapperIgnoreSource(nameof(Entity.Id))]
    [MapperIgnoreTarget(nameof(Entity.Id))]
    public static partial Entity CloneWithoutId(this Entity source);
}

Actual relevant generated code

Both (1) and (2) methods after generation look in the same way. Attributes on the second method are totally ignored. Both methods return the same instance, their code looks like this:

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.1.0")]
public static partial global::MyNameSpace.Entity CloneWithoutId(this global::MyNameSpace.Entity source)
{
    return source;
}

Expected relevant generated code

Expected behaviour: at least the second method returns a new instance of Entity, something like this:

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.2.1.0")]
public static partial global::MyNameSpace.Entity CloneWithoutId(this global::MyNameSpace.Entity source)
{
    return new Entity
    {
        Prop = source.Prop,
    };
}

Environment (please complete the following information):

  • Mapperly Version: 4.2.1
  • Nullable reference types: enabled
  • .NET Version: 9.0.302
  • Target Framework: net9.0
  • Compiler Version: 4.14.0-3.25279.5 (995f12b6)
  • C# Language Version: 13.0
  • IDE: Rider 2025.1.3
  • OS: Win10

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions