Skip to content

Commit

Permalink
Fixes Issue #152. EF.Property not mapped correctly. (#153)
Browse files Browse the repository at this point in the history
  • Loading branch information
BlaiseD authored Nov 25, 2022
1 parent 22a1a46 commit 2e3c1f3
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,18 @@ public PrependParentNameVisitor(ParameterExpression currentParameter, string par
public string ParentFullName { get; }
public Expression NewParameter { get; }

protected override Expression VisitParameter(ParameterExpression node)
{
if (object.ReferenceEquals(CurrentParameter, node))
{
return string.IsNullOrEmpty(ParentFullName)
? NewParameter
: ExpressionHelpers.MemberAccesses(ParentFullName, NewParameter);
}

return base.VisitParameter(node);
}

protected override Expression VisitTypeBinary(TypeBinaryExpression node)
{
if (!(node.Expression is ParameterExpression))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Xunit;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class CanMapParameterBodyFromChildReferenceWithoutMemberExpression
{
[Fact]
public void Can_map_parameter_body_from_child_reference_without_member_expression()
{
// Arrange
var config = new MapperConfiguration(c =>
{
c.CreateMap<TestCategory, TestProductDTO>()
.ForMember(p => p.Brand, c => c.MapFrom(p => EF.Property<int>(p, "BrandId"))); ;
c.CreateMap<TestProduct, TestProductDTO>()
.IncludeMembers(p => p.Category);
});

config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();

var products = new List<TestProduct>() {
new TestProduct { }
}.AsQueryable();

//Act
Expression<Func<TestProductDTO, bool>> expr = x => x.Brand == 2;
var mappedExpression = mapper.MapExpression<Expression<Func<TestProduct, bool>>>(expr);

//Assert
Assert.Equal("x => (Convert(Property(x.Category, \"BrandId\"), Int32) == 2)", mappedExpression.ToString());
}

public class TestCategory
{
// Has FK BrandId
}
public class TestProduct
{
public TestCategory? Category { get; set; }
}

public class TestProductDTO
{
public int Brand { get; set; }
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Xunit;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
public class CanMapParameterBodyWithoutMemberExpression
{
[Fact]
public void Can_map_parameter_body_without_member_expression()
{
// Arrange
var config = new MapperConfiguration(c =>
{
c.CreateMap<TestProduct, TestProductDTO>()
.ForMember(p => p.Brand, c => c.MapFrom(p => EF.Property<int>(p, "BrandId")));
});

config.AssertConfigurationIsValid();
var mapper = config.CreateMapper();

var products = new List<TestProduct>() {
new TestProduct { }
}.AsQueryable();

//Act
Expression<Func<TestProductDTO, bool>> expr = x => x.Brand == 2;
var mappedExpression = mapper.MapExpression<Expression<Func<TestProduct, bool>>>(expr);

//Assert
Assert.Equal("x => (Convert(Property(x, \"BrandId\"), Int32) == 2)", mappedExpression.ToString());
}

public class TestProduct
{
// Empty, has shadow key named BrandId
}

public class TestProductDTO
{
public int Brand { get; set; }
}
}
}
12 changes: 12 additions & 0 deletions tests/AutoMapper.Extensions.ExpressionMapping.UnitTests/EF.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;

namespace AutoMapper.Extensions.ExpressionMapping.UnitTests
{
internal class EF
{
internal static object Property<T>(object p, string v)
{
throw new NotImplementedException();
}
}
}

0 comments on commit 2e3c1f3

Please sign in to comment.