Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/Riok.Mapperly/Descriptors/Mappings/NullDelegateMapping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ public override ExpressionSyntax Build(TypeMappingBuildContext ctx)
if (_delegateMapping.SourceType.IsNullable())
return _delegateMapping.Build(ctx);

if (SourceType.IsNullable() && TargetType.IsNullable() && SymbolEqualityComparer.Default.Equals(SourceType, TargetType))
{
return _delegateMapping.Build(ctx);
}

if (!SourceType.IsNullable())
{
// if the target type is a nullable value type, there needs to be an additional cast in some cases
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static partial int DirectInt(int value)
[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
public static partial int? DirectIntNullable(int? value)
{
return value == null ? default(int?) : value.Value;
return value;
}

[global::System.CodeDom.Compiler.GeneratedCode("Riok.Mapperly", "0.0.1.0")]
Expand Down
2 changes: 1 addition & 1 deletion test/Riok.Mapperly.Tests/Mapping/DictionaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public void DictionaryToSameDictionaryDeepCloningInDisabledNullableContext()
var target = new global::System.Collections.Generic.Dictionary<string, string?>(source.Count);
foreach (var item in source)
{
target[item.Key] = item.Value == null ? default : item.Value;
target[item.Key] = item.Value;
}
return target;
"""
Expand Down
7 changes: 7 additions & 0 deletions test/Riok.Mapperly.Tests/Mapping/NullableTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,13 @@ TestSourceBuilderOptions.Default with

[Fact]
public void NonNullableToNullableValueType()
{
var source = TestSourceBuilder.Mapping("int[]?", "int[]?");
TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return source;");
}

[Fact]
public void NonNullableToNullableValueType2()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test method name 'NonNullableToNullableValueType2' is unclear due to the '2' suffix. Consider a more descriptive name that explains what distinguishes this test from similar tests, such as 'NonNullableToNullableValueTypeCast' or 'NonNullableDateTimeToNullableDateTime'.

{
var source = TestSourceBuilder.Mapping("DateTime", "DateTime?");
TestHelper.GenerateMapper(source).Should().HaveSingleMethodBody("return (global::System.DateTime?)source;");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public void NullableObjectToNullableObjectDeepCloning()
TestHelper
.GenerateMapper(source, TestHelperOptions.AllowInfoDiagnostics)
.Should()
.HaveMapMethodBody("return source == null ? default : source;")
.HaveMapMethodBody("return source;")
.HaveDiagnostic(DiagnosticDescriptors.MappedObjectToObjectWithoutDeepClone)
.HaveAssertedAllDiagnostics();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public partial class Mapper
var target = new global::System.Collections.Generic.Dictionary<string, string?>(source.Count);
foreach (var item in source)
{
target[item.Key] = item.Value == null ? default : item.Value;
target[item.Key] = item.Value;
}
return target;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public partial class Mapper
var target = new string?[source.Length];
for (var i = 0; i < source.Length; i++)
{
target[i] = source[i] == null ? default : source[i]!;
target[i] = source[i];
}
return target;
}
Expand Down