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

Mapperly generates code which results in a NullReferenceException because nested destination object is not initialized #1650

Open
martijnkagie opened this issue Dec 20, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@martijnkagie
Copy link

Please do the checklist before filing an issue:

  • [ x] I have read the documentation, including the FAQ
  • [ x] I can reproduce the bug using the latest prerelease version
  • [ x] I have searched existing discussion and issue to avoid duplicates

Describe the bug
When mapping multiple properties to the same nested object the nested object is not initialized when some conditions are met. It seems that AllowNullPropertyAssignment = false should be set and the value of the first source property should be null

Declaration code
A sample program triggering the bug could be found here: https://github.com/martijnkagie/mapperly-null-reference-bug

Actual relevant generated code

#nullable enable
namespace MapperlyNullReferenceBug
{
    internal static partial class Mapper
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
        public static partial global::MapperlyNullReferenceBug.FooDto ToDto(this global::MapperlyNullReferenceBug.Foo foo)
        {
            var target = new global::MapperlyNullReferenceBug.FooDto();
            if (foo.Property1 != null)
            {
                target.BarDto ??= new global::MapperlyNullReferenceBug.BarDto();
                target.BarDto.PropertyA = MapToDouble(foo.Property1.Value);
            }
            if (foo.Property2 != null)
            {
                target.BarDto.PropertyB = MapToDouble(foo.Property2.Value);
            }
            return target;
        }
    }
}

Expected relevant generated code

#nullable enable
namespace MapperlyNullReferenceBug
{
    internal static partial class Mapper
    {
        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "4.1.1.0")]
        public static partial global::MapperlyNullReferenceBug.FooDto ToDto(this global::MapperlyNullReferenceBug.Foo foo)
        {
            var target = new global::MapperlyNullReferenceBug.FooDto();
            if (foo.Property1 != null)
            {
                target.BarDto ??= new global::MapperlyNullReferenceBug.BarDto();
                target.BarDto.PropertyA = MapToDouble(foo.Property1.Value);
            }
            if (foo.Property2 != null)
            {
                target.BarDto ??= new global::MapperlyNullReferenceBug.BarDto();
                target.BarDto.PropertyB = MapToDouble(foo.Property2.Value);
            }
            return target;
        }
    }
}

Reported relevant diagnostics
N/A

Environment (please complete the following information):

  • Mapperly Version: 4.1.1 (not present in version 3.6.0)
  • Nullable reference types: enabled
  • .NET Version: .NET 9.0 (also present in .NET 8.0)
  • Target Framework: net9.0
  • C# Language Version: 13.0
  • IDE: Visual Studio v17.12.3
  • OS: Windows 11
@martijnkagie martijnkagie added the bug Something isn't working label Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant