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
Fix for Issue 116 (Wrong Parameter Expression used in mapped expression)
Fix for Issue #118 (Mapping Select New fails for properties declared in a base class).
Supporting SourceLink
  • Loading branch information
BlaiseD committed Sep 8, 2021
commit a717f96a553e3e01475fc32b9208b684ac4639c8
8 changes: 6 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ name: CI

on:
push:
branches: [ master ]
branches:
- master
- v4.1.2
pull_request:
branches: [ master ]
branches:
- master
- v4.1.2

jobs:
build:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/AutoMapper/AutoMapper.Extensions.ExpressionMapping</RepositoryUrl>
<MinVerTagPrefix>v</MinVerTagPrefix>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<Deterministic>true</Deterministic>
<ContinuousIntegrationBuild Condition="'$(GITHUB_ACTIONS)' == 'true'">true</ContinuousIntegrationBuild>
</PropertyGroup>

<ItemGroup>
Expand All @@ -23,6 +29,7 @@
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,32 @@ namespace AutoMapper.Extensions.ExpressionMapping
{
internal class PrependParentNameVisitor : ExpressionVisitor
{
public PrependParentNameVisitor(Type currentParameterType, string parentFullName, Expression newParameter)
public PrependParentNameVisitor(ParameterExpression currentParameter, string parentFullName, Expression newParameter)
{
CurrentParameterType = currentParameterType;
CurrentParameter = currentParameter;
ParentFullName = parentFullName;
NewParameter = newParameter;
}

public Type CurrentParameterType { get; }
public ParameterExpression CurrentParameter { get; }
public string ParentFullName { get; }
public Expression NewParameter { get; }
public Expression NewParameter { get; }

protected override Expression VisitMember(MemberExpression node)
{
if (node.NodeType == ExpressionType.Constant)
return base.VisitMember(node);

string sourcePath;

var parameterExpression = node.GetParameterExpression();
var sType = parameterExpression?.Type;
if (sType != null && sType == CurrentParameterType && node.IsMemberExpression())
{
sourcePath = node.GetPropertyFullName();
}
else
{
if (!object.ReferenceEquals(CurrentParameter, node.GetParameterExpression()) || !node.IsMemberExpression())
return base.VisitMember(node);
}

var fullName = string.IsNullOrEmpty(ParentFullName)
? sourcePath
: string.Concat(ParentFullName, ".", sourcePath);

return ExpressionHelpers.MemberAccesses(fullName, NewParameter);
return ExpressionHelpers.MemberAccesses
(
string.IsNullOrEmpty(ParentFullName)
? node.GetPropertyFullName()
: $"{ParentFullName}.{node.GetPropertyFullName()}",
NewParameter
);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ private Expression GetMemberExpressionFromCustomExpression(PropertyMapInfo lastW
(
new PrependParentNameVisitor
(
lastWithCustExpression.CustomExpression.Parameters[0].Type/*Parent type of current property*/,
lastWithCustExpression.CustomExpression.Parameters[0]/*Parent parameter of current property*/,
BuildFullName(beforeCustExpression),
visitedParentExpr
)
Expand Down Expand Up @@ -341,7 +341,7 @@ private MemberInitExpression GetMemberInit(MemberBindingGroup memberBindingGroup
return list;

bool ShouldBindPropertyMap(MemberAssignmentInfo memberAssignmentInfo)
=> (memberBindingGroup.IsRootMemberAssignment && sourceMember.ReflectedType == memberBindingGroup.NewType)
=> (memberBindingGroup.IsRootMemberAssignment && sourceMember.ReflectedType.IsAssignableFrom(memberBindingGroup.NewType))
|| (!memberBindingGroup.IsRootMemberAssignment && declaringMemberKey.Equals(memberBindingGroup.DeclaringMemberKey));
});

Expand All @@ -364,9 +364,9 @@ bool ShouldBindPropertyMap(MemberAssignmentInfo memberAssignmentInfo)

bool ShouldBindChildReference(MemberBindingGroup group)
=> (memberBindingGroup.IsRootMemberAssignment
&& group.DeclaringMemberKey.DeclaringMemberInfo.ReflectedType == memberBindingGroup.NewType)
&& group.DeclaringMemberKey.DeclaringMemberInfo.ReflectedType.IsAssignableFrom(memberBindingGroup.NewType))
|| (!memberBindingGroup.IsRootMemberAssignment
&& group.DeclaringMemberKey.DeclaringMemberInfo.ReflectedType == memberBindingGroup.NewType
&& group.DeclaringMemberKey.DeclaringMemberInfo.ReflectedType.IsAssignableFrom(memberBindingGroup.NewType)
&& group.DeclaringMemberKey.DeclaringMemberFullName.StartsWith(memberBindingGroup.DeclaringMemberKey.DeclaringMemberFullName));

return Expression.MemberInit(Expression.New(memberBindingGroup.NewType), bindings);
Expand Down
Loading