Skip to content

Commit 9c72f76

Browse files
committed
NullableExpressionVisitor improvement
and test
1 parent 155a723 commit 9c72f76

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/Mapster.Tests/WhenMappingNullablePrimitives.cs

Lines changed: 40 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,46 @@ public void Can_Map_From_Non_Nullable_Source_To_Nullable_Target()
134134
poco.IsImport.GetValueOrDefault().ShouldBeTrue();
135135
}
136136

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

160+
161+
public class Output414
162+
{
163+
public string Item1 { get; set; }
164+
165+
public string Item2 { get; set; }
166+
167+
public Application414 Application { get; set; }
168+
}
169+
170+
public class Application414
171+
{
172+
public string Name { get; set; }
173+
174+
public int Id { get; set; }
175+
}
176+
139177
public class NullablePrimitivesPoco
140178
{
141179
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)