Skip to content

Commit 12b49ba

Browse files
committed
NullableExpressionVisitor improvement
and test
1 parent 155a723 commit 12b49ba

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

src/Mapster.Tests/WhenMappingNullablePrimitives.cs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
using System;
2-
using Microsoft.VisualStudio.TestTools.UnitTesting;
1+
using Microsoft.VisualStudio.TestTools.UnitTesting;
32
using Shouldly;
3+
using System;
44

55
namespace Mapster.Tests
66
{
@@ -134,8 +134,49 @@ public void Can_Map_From_Non_Nullable_Source_To_Nullable_Target()
134134
poco.IsImport.GetValueOrDefault().ShouldBeTrue();
135135
}
136136

137+
/// <summary>
138+
/// https://github.com/MapsterMapper/Mapster/issues/414
139+
/// </summary>
140+
[TestMethod]
141+
public void MappingNullTuple()
142+
{
143+
TypeAdapterConfig<(string?, string?, Application414), Output414>.NewConfig()
144+
.Map(dest => dest, src => src.Item1)
145+
.Map(dest => dest, src => src.Item2)
146+
.Map(dest => dest.Application, src => src.Item3 == null ? (Application414)null : new Application414()
147+
{
148+
Id = src.Item3.Id,
149+
Name = src.Item3.Name
150+
});
151+
152+
(string, string, Application414) source = (null, null, null);
153+
154+
var result = source.Adapt<Output414>();
155+
156+
result.Item1.ShouldBeNull();
157+
result.Item2.ShouldBeNull();
158+
result.Application.ShouldBeNull();
159+
}
160+
137161
#region TestClasses
138162

163+
164+
public class Output414
165+
{
166+
public string Item1 { get; set; }
167+
168+
public string Item2 { get; set; }
169+
170+
public Application414 Application { get; set; }
171+
}
172+
173+
public class Application414
174+
{
175+
public string Name { get; set; }
176+
177+
public int Id { get; set; }
178+
}
179+
139180
public class NullablePrimitivesPoco
140181
{
141182
public Guid Id { get; set; }

src/Mapster/Utils/NullableExpressionVisitor.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ protected override Expression VisitConstant(ConstantExpression node)
127127

128128
protected override Expression VisitMember(MemberExpression node)
129129
{
130-
CanBeNull = node.Member.GetCustomAttributesData().All(IsNullable);
130+
CanBeNull = node.Type.IsClass || node.Member.GetCustomAttributesData().All(IsNullable);
131131
return node;
132132
}
133133
}

0 commit comments

Comments
 (0)