riok / mapperly

A .NET source generator for generating object mappings. No runtime reflection.
https://mapperly.riok.app
Apache License 2.0
2.61k stars 141 forks source link

Mapping method not used when nullable parameter or return value but target is non-nullable #1178

Open xnoreq opened 7 months ago

xnoreq commented 7 months ago

Describe the bug In some cases it makes sense to allow/support nullable reference types in mapping methods. As far as I can see, Mapperly does not use such methods if the target is not nullable.

Overloading both variants is not possible and would just be redundant code duplication anyway, so that is not an option.

Instead I suggest changing the code generation to use these methods

Declaration code

[Mapper(ThrowOnPropertyMappingNullMismatch = true)]
public static partial class Mapper
{
    public static partial ATarget AToTarget(A a);

    // this is not used by AToTarget
    [return: NotNullIfNotNull(nameof(b))]
    public static partial BTarget? BToTarget(B? b);

    // this would be used by AToTarget
    //public static partial BTarget BToTarget(B b);
}

public class A
{
    public B B { get; set; }
}

public class B
{
    public string? Name { get; set; }
}

public class ATarget
{
    public BTarget B { get; set; }
}

public class BTarget
{
    public string? Name { get; set; }
}

Actual relevant generated code

// <auto-generated />
#nullable enable
namespace ConsoleApp1
{
    public static partial class Mapper
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.4.0.0")]
        public static partial global::ConsoleApp1.ATarget AToTarget(global::ConsoleApp1.A a)
        {
            var target = new global::ConsoleApp1.ATarget();
            target.B = MapToBTarget(a.B);
            return target;
        }

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.4.0.0")]
        public static partial global::ConsoleApp1.BTarget? BToTarget(global::ConsoleApp1.B? b)
        {
            if (b == null)
                return default;
            var target = new global::ConsoleApp1.BTarget();
            target.Name = b.Name;
            return target;
        }

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.4.0.0")]
        private static global::ConsoleApp1.BTarget MapToBTarget(global::ConsoleApp1.B source)
        {
            var target = new global::ConsoleApp1.BTarget();
            target.Name = source.Name;
            return target;
        }
    }
}

Expected relevant generated code something like

public static partial class Mapper
{
    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.4.0.0")]
    public static partial global::ConsoleApp1.ATarget AToTarget(global::ConsoleApp1.A a)
    {
        var target = new global::ConsoleApp1.ATarget();
        var targetB = BToTarget(a.B);
        if (targetB == null)
            throw new System.ArgumentNullException(nameof(targetB));
        target.B = targetB;
        return target;
    }

    [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "3.4.0.0")]
    public static partial global::ConsoleApp1.BTarget? BToTarget(global::ConsoleApp1.B? b)
    {
        if (b == null)
            return default;
        var target = new global::ConsoleApp1.BTarget();
        target.Name = b.Name;
        return target;
    }
}

Environment (please complete the following information):

latonz commented 7 months ago

Currently Mapperly only uses user-implemented mappings if the types match exactly.

Overloading both variants is not possible and would just be redundant code duplication anyway, so that is not an option.

As a workaround: Instead of overloading you can create the method with the parameter types for Mapperly with another name and just call your existing method. The point made on code duplication obviously still applies...

If this is implemented, other nullable attributes need to be considered too (e.g. MayBeNullAttribute). See MS docs.