Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
24 changes: 16 additions & 8 deletions src/AutoMapper/ExpressionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ protected override Expression VisitMember(MemberExpression node)
internal class RemoveIfNotNullVisitor : ExpressionVisitor
{
private readonly Expression[] _expressions;
private readonly IList<Expression> AllreadyUpdated = new List<Expression>();

public RemoveIfNotNullVisitor(params Expression[] expressions)
{
Expand Down Expand Up @@ -196,9 +195,12 @@ public ConvertingVisitor(ParameterExpression oldParam, Expression newParam)

protected override Expression VisitMember(MemberExpression node)
{
return node.Expression == _oldParam
? MakeMemberAccess(ToType(_newParam, _oldParam.Type), node.Member)
: base.VisitMember(node);
if (node.Expression == _oldParam)
{
node = MakeMemberAccess(ToType(_newParam, _oldParam.Type), node.Member);
}

return base.VisitMember(node);
}

protected override Expression VisitParameter(ParameterExpression node)
Expand All @@ -208,9 +210,12 @@ protected override Expression VisitParameter(ParameterExpression node)

protected override Expression VisitMethodCall(MethodCallExpression node)
{
return node.Object == _oldParam
? Call(ToType(_newParam, _oldParam.Type), node.Method, node.Arguments)
: base.VisitMethodCall(node);
if (node.Object == _oldParam)
{
node = Call(ToType(_newParam, _oldParam.Type), node.Method, node.Arguments);
}

return base.VisitMethodCall(node);
}
}

Expand All @@ -227,7 +232,10 @@ public ReplaceExpressionVisitor(Expression oldExpression, Expression newExpressi

public override Expression Visit(Expression node)
{
return _oldExpression == node ? _newExpression : base.Visit(node);
if (_oldExpression == node)
node = _newExpression;

return base.Visit(node);
}
}

Expand Down
72 changes: 72 additions & 0 deletions src/UnitTests/Bug/MapFromClosureBug.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
namespace AutoMapper.UnitTests.Bug
{
using System;
using Rhino.Mocks;
using Should;

public class MapFromClosureBug : SpecBaseBase
{
private static readonly IDateProvider _dateProvider;

static MapFromClosureBug()
{
_dateProvider = MockRepository.GenerateMock<IDateProvider>();
}

public interface IDateProvider
{
DateTime CurrentRestaurantTime(Restaurant restaurant);
}

public class Result
{
public Booking Booking { get; set; }
}

public class Restaurant
{
}

public class Booking
{
public Restaurant Restaurant { get; set; }

public int? CalculateTotal(DateTime currentTime)
{
return null;
}
}

public class ResultDto
{
public BookingDto Booking { get; set; }
}

public class BookingDto
{
public int? Total { get; set; }
}

public void Should_map_successfully()
{
var mapperConfiguration = new MapperConfiguration(cfg =>
{
cfg.CreateMap<Result, ResultDto>();
cfg.CreateMap<Booking, BookingDto>()
.ForMember(d => d.Total,
o => o.MapFrom(b => b.CalculateTotal(_dateProvider.CurrentRestaurantTime(b.Restaurant))));
});

var mapper = mapperConfiguration.CreateMapper();

var result = new Result { Booking = new Booking() };

// Act
var dto = mapper.Map<ResultDto>(result);

// Assert
dto.ShouldNotBeNull();
}

}
}
1 change: 1 addition & 0 deletions src/UnitTests/UnitTests.Net4.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
<Compile Include="Bug\CollectionBaseClassGetConvention.cs" />
<Compile Include="Bug\InitializeNRE.cs" />
<Compile Include="Bug\GuidTryExpression.cs" />
<Compile Include="Bug\MapFromClosureBug.cs" />
<Compile Include="Bug\NullToString.cs" />
<Compile Include="Bug\NullSubstituteInnerClass.cs" />
<Compile Include="Bug\MapExpandoObjectProperty.cs" />
Expand Down