mapperly icon indicating copy to clipboard operation
mapperly copied to clipboard

Invalid mapping for nullable arrays

Open TonEnfer opened this issue 1 year ago • 1 comments

Describe the bug Mapperly generates incorrect code for nullable arrays in some cases. See example code

Declaration code

public record Dto(int[]? Array);

[Mapper]
public partial class Mapper
{
    public partial Dto Map(int[]? array);
    public partial int[]? Map(int[]? model);
}

Actual relevant generated code

// <auto-generated />
#nullable enable
namespace ConsoleApp1
{
    public partial class Mapper
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
        public partial global::ConsoleApp1.Dto Map(int[]? model)
        {
            return model == null ? throw new System.ArgumentNullException(nameof(model)) : new global::ConsoleApp1.Dto(model);
        }

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
        public partial int[]? Map(int[]? model)
        {
            return model == null ? default : model;
        }
    }
}

Expected relevant generated code

// <auto-generated />
#nullable enable
namespace ConsoleApp1
{
    public partial class Mapper
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
        public partial global::ConsoleApp1.Dto Map(int[]? model)
        {
            return new global::ConsoleApp1.Dto(model);
        }

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
        public partial int[]? Map(int[]? model)
        {
            return model;
        }
    }
}

Environment (please complete the following information):

  • Mapperly Version: 4.1.1.0
  • Nullable reference types: enabled
  • .NET Version: 9.0.100
  • Target Framework: .net9.0
  • Compiler Version: 4.12.0-3.24558.5 (21192bfc)
  • C# Language Version: 13.0
  • IDE: Visual Studio 17.12.1
  • OS: Win 11

TonEnfer avatar Nov 26 '24 22:11 TonEnfer

Thank you for reporting this issue! It seems there are some bugs related to our null handling. I've created #1614 to address this and explore ways to improve our approach to null handling in general.

latonz avatar Nov 27 '24 12:11 latonz