Please do the checklist before filing an issue:
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
Please do the checklist before filing an issue:
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
Actual relevant generated code
Expected relevant generated code
Environment (please complete the following information):