Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
xnoreq opened this issue Mar 15, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@xnoreq
Copy link

xnoreq commented Mar 15, 2024

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):

  • Mapperly Version: 3.4.0
  • .NET Version: 8.0.200
  • Target Framework: net8.0
  • Compiler Version: 4.9.0-3.24121.1 (a98c90d5)
  • C# Language Version: 12.0
  • IDE: Visual Studio v17.9.2
  • OS: Windows 10
@xnoreq xnoreq added the bug Something isn't working label Mar 15, 2024
@latonz latonz added enhancement New feature or request and removed bug Something isn't working labels Mar 20, 2024
@latonz
Copy link
Contributor

latonz commented Mar 20, 2024

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants