Skip to content

Commit

Permalink
fix mapping from static property throw NullReferenceException
Browse files Browse the repository at this point in the history
  • Loading branch information
chaowlert committed Nov 16, 2017
1 parent a0f2220 commit 845efa4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/Mapster.Tests/WhenAddingCustomMappings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@ public void Property_Is_Mapped_To_Different_Property_Successfully()
{
TypeAdapterConfig<SimplePoco, SimpleDto>.NewConfig()
.Map(dest => dest.AnotherName, src => src.Name)
.Map(dest => dest.LastModified, src => DateTime.Now)
.Map(dest => dest.FileData, src => new FileData { Content = src.FileContent })
.Compile();

var poco = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName"};
var poco = new SimplePoco {Id = Guid.NewGuid(), Name = "TestName", FileContent = "Foo"};

var dto = TypeAdapter.Adapt<SimplePoco, SimpleDto>(poco);

dto.Id.ShouldBe(poco.Id);
dto.Name.ShouldBe(poco.Name);
dto.AnotherName.ShouldBe(poco.Name);
dto.LastModified.Ticks.ShouldBeGreaterThan(0);
dto.FileData.Content.ShouldBe("Foo");
}

[TestMethod]
Expand All @@ -46,6 +50,7 @@ public class SimplePoco
{
public Guid Id { get; set; }
public string Name { get; set; }
public string FileContent { get; set; }
}

public class SimpleDto
Expand All @@ -54,6 +59,13 @@ public class SimpleDto
public string Name { get; set; }

public string AnotherName { get; set; }
public DateTime LastModified { get; set; }
public FileData FileData { get; set; }
}

public class FileData
{
public string Content { get; set; }
}

public class ChildPoco
Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Mapster.NetCore.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<GenerateAssemblyProductAttribute>false</GenerateAssemblyProductAttribute>
<GenerateAssemblyCopyrightAttribute>false</GenerateAssemblyCopyrightAttribute>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<Version>3.1.7</Version>
<Version>3.1.8</Version>
<RootNamespace>Mapster</RootNamespace>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/Mapster/Utils/ReflectionUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public static MemberExpression GetMemberInfo(Expression member, bool source = fa
if (expr.NodeType == ExpressionType.MemberAccess)
{
var tmp = (MemberExpression) expr;
if (tmp.Expression.NodeType == ExpressionType.Parameter)
if (tmp.Expression?.NodeType == ExpressionType.Parameter)
memberExpr = tmp;
else if (!source)
throw new ArgumentException("Only first level member access on destination allowed (eg. dest => dest.Name)", nameof(member));
Expand Down

0 comments on commit 845efa4

Please sign in to comment.