Skip to content

Invalid mapping for nullable arrays #1611

Description

@TonEnfer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions