Skip to content

When a mapper has a parameter the wrong mapping functions (nullable) will be used #2275

@kammerjaeger

Description

@kammerjaeger

Please do the checklist before filing an issue:

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

Describe the bug
This could be related to #2241.
When mapping functions contain parameter, the wrong mapping functions will be used. Instead of nullable versions the non nullable version will be used and a null check will be added.
Without the parameter the correct code is created, see "Map(...)" function.

Repro
If needed can be provided.

Declaration code

public static class StringMapper {
    public static string? TrimToNull(this string? value) => string.IsNullOrWhiteSpace(value) ? null : value.Trim();
    public static string Trim(this string value) => value.Trim();
}
public class TestClassIn {
    public string? NullableStr { get; set; }
    public string Str { get; set; }
}
public class TestClassOut {
    public string? NullableStr { get; set; }
    public string Str { get; set; }
    public string? AdditionalParameter { get; set; }
}

[Mapper]
[UseStaticMapper(typeof(StringMapper))]
public static partial class TestMapper {
    [MapperIgnoreTarget(nameof(TestClassOut.AdditionalParameter))]
    public static partial TestClassOut Map(TestClassIn input);    
    public static partial TestClassOut MapWithParameter(TestClassIn input, string? additionalParameter);
}

public class NullableParameterTest {
....
    [Fact]
    public void MapWithParameter_ShouldMapAsNull_WhenEmptyProvided() {
        var input = new TestClassIn {
            NullableStr = " ",
            Str = " str"
        };

        var result = TestMapper.MapWithParameter(input, " ");

        result.NullableStr.Should().BeNull();
        result.Str.Should().Be(input.Str.Trim());
        result.AdditionalParameter.Should().BeNull();
    }
}

Actual relevant generated code

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "5.0.0.0")]
        public static partial global::Tests.Model.Mapper.TestClassOut MapWithParameter(global::Tests.Model.Mapper.TestClassIn input, string? additionalParameter)
        {
            var target = new global::Tests.Model.Mapper.TestClassOut();
            if (input.NullableStr != null)
            {
                target.NullableStr = global::Model.Mapper.Common.StringMapper.Trim(input.NullableStr);
            }
            else
            {
                target.NullableStr = null;
            }
            target.Str = global::Model.Mapper.Common.StringMapper.Trim(input.Str);
            if (additionalParameter != null)
            {
                target.AdditionalParameter = global::Model.Mapper.Common.StringMapper.Trim(additionalParameter);
            }
            else
            {
                target.AdditionalParameter = null;
            }
            return target;
        }

Expected relevant generated code

        [global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "5.0.0.0")]
        public static partial global::Tests.Model.Mapper.TestClassOut MapWithParameter(global::Tests.Model.Mapper.TestClassIn input, string? additionalParameter)
        {
            var target = new global::Tests.Model.Mapper.TestClassOut();
            target.NullableStr = global::Model.Mapper.Common.StringMapper.TrimToNull(input.NullableStr);
            target.Str = global::Model.Mapper.Common.StringMapper.Trim(input.Str);
            target.AdditionalParameter = global::Model.Mapper.Common.StringMapper.TrimToNull(additionalParameter);
            return target;
        }

Environment (please complete the following information):

  • Mapperly Version: 5.0.0-next.7
  • Nullable reference types: enabled
  • .NET Version: NET 10.0.203
  • Target Framework: .net10.0
  • Compiler Version: 5.3.0-2.26219.105 (c23858a6d860abe3ca94f9ac630c3e61b2625664)
  • C# Language Version: 14.0
  • IDE: Visual Studio v18.5.0
  • 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