Skip to content

Commit 9f17596

Browse files
committed
fix: start Fix
1 parent 1d89074 commit 9f17596

3 files changed

Lines changed: 37 additions & 8 deletions

File tree

src/Mapster.Async.Tests/AsyncTest.cs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
using System;
2-
using System.Threading.Tasks;
31
using MapsterMapper;
42
using Microsoft.VisualStudio.TestTools.UnitTesting;
53
using Shouldly;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Threading.Tasks;
67

78
namespace Mapster.Async.Tests
89
{
@@ -18,12 +19,20 @@ public static void Setup(TestContext context)
1819
[TestMethod]
1920
public async Task Async()
2021
{
21-
TypeAdapterConfig<Poco, Dto>.NewConfig()
22-
.AfterMappingAsync(async dest => { dest.Name = await GetName(); });
22+
// Arrange
23+
var fooDto = new FooDto();
2324

24-
var poco = new Poco {Id = "foo"};
25-
var dto = await poco.BuildAdapter().AdaptToTypeAsync<Dto>();
26-
dto.Name.ShouldBe("bar");
25+
26+
TypeAdapterConfig.GlobalSettings.Default
27+
.AddDestinationTransform(DestinationTransform.EmptyCollectionIfNull);
28+
29+
// Act
30+
var foo = fooDto.Adapt<Foo>();
31+
32+
var str = fooDto.BuildAdapter().CreateMapExpression<Foo>();
33+
34+
// Assert
35+
foo.Strings.ShouldNotBeNull();
2736
}
2837

2938

@@ -138,6 +147,17 @@ private static async Task<DtoOwner> GetOwner(string id)
138147
}
139148
}
140149

150+
151+
152+
class FooDto
153+
{
154+
public string[] Strings { get; set; }
155+
}
156+
157+
record Foo(List<string> Strings);
158+
159+
160+
141161
public class Poco
142162
{
143163
public string Id { get; set; }

src/Mapster.Async.Tests/Mapster.Async.Tests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
</PropertyGroup>
88

99
<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
10-
<TargetFrameworks>$(TargetFrameworks);net48</TargetFrameworks>
1110
</PropertyGroup>
1211

1312
<ItemGroup>

src/Mapster/Utils/ExpressionEx.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,13 @@ public static bool CanBeNull(this Expression exp)
344344
return visitor.CanBeNull.GetValueOrDefault();
345345
}
346346

347+
public static bool CanBeNullParam(this Expression exp)
348+
{
349+
var visitor = new NullableExpressionVisitor();
350+
visitor.Visit(exp);
351+
return visitor.CanBeNull.GetValueOrDefault();
352+
}
353+
347354
public static bool IsComplex(this Expression exp)
348355
{
349356
var visitor = new ComplexExpressionVisitor();
@@ -456,6 +463,9 @@ public static Expression ApplyNullPropagationFromCtor(this Expression getter, Ex
456463
.Where(x=> !object.ReferenceEquals(x.arg,arg))
457464
.Select(x=>x.param?.Type);
458465

466+
if (!adapt.CanBeNullParam() && adapt is BinaryExpression)
467+
return adapt;
468+
459469
while (current != null)
460470
{
461471
Expression? compareNull = null;

0 commit comments

Comments
 (0)