Skip to content

Fix for issue #8 (Source and destination must have same number of arg… #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2018
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
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<Authors>Jimmy Bogard</Authors>
<LangVersion>latest</LangVersion>
<VersionPrefix>1.0.2</VersionPrefix>
<VersionPrefix>1.0.3</VersionPrefix>
<WarningsAsErrors>true</WarningsAsErrors>
<NoWarn>$(NoWarn);1701;1702;1591</NoWarn>
</PropertyGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ void FindMaps(List<PropertyMap> maps)
{
foreach (PropertyMap pm in maps)
{
if (pm.SourceMember == null)
if (!pm.SourceMembers.Any())
continue;

AddChildMappings
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ protected void FindDestinationFullName(Type typeSource, Type typeDestination, st
throw new InvalidOperationException(Resource.customResolversNotSupported);
}

if (propertyMap.CustomExpression == null && propertyMap.SourceMember == null)
if (propertyMap.CustomExpression == null && !propertyMap.SourceMembers.Any())
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.srcMemberCannotBeNullFormat, typeSource.Name, typeDestination.Name, sourceFullName));

CompareSourceAndDestLiterals
Expand All @@ -351,7 +351,7 @@ void CompareSourceAndDestLiterals(Type mappedPropertyType, string mappedProperty
var propertyMap = typeMap.GetPropertyMapByDestinationProperty(propertyName);

var sourceMemberInfo = typeSource.GetFieldOrProperty(propertyMap.DestinationProperty.Name);
if (propertyMap.CustomExpression == null && propertyMap.SourceMember == null)//If sourceFullName has a period then the SourceMember cannot be null. The SourceMember is required to find the ProertyMap of its child object.
if (propertyMap.CustomExpression == null && !propertyMap.SourceMembers.Any())//If sourceFullName has a period then the SourceMember cannot be null. The SourceMember is required to find the ProertyMap of its child object.
throw new InvalidOperationException(string.Format(CultureInfo.CurrentCulture, Resource.srcMemberCannotBeNullFormat, typeSource.Name, typeDestination.Name, propertyName));

propertyMapInfoList.Add(new PropertyMapInfo(propertyMap.CustomExpression, propertyMap.SourceMembers.ToList()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,19 @@ public void Test_Error_when_mapping_nested_expressions_when_enumerable_is_array_
//Assert
Assert.NotNull(expMapped);
}

[Fact]
public void Can_correctly_type_maps_for_child_properties()
{
//Arrange
Expression<Func<OrderModel, bool>> src = x => x.Items.Any(i => i.Name == "Test");

//Act
Expression<Func<OrderEntity, bool>> dest = mapper.Map<Expression<Func<OrderEntity, bool>>>(src);

//Assert
Assert.NotNull(dest);
}
#endregion Tests

private static void SetupAutoMapper()
Expand Down Expand Up @@ -936,6 +949,31 @@ public class ChildDataClass
public string Data { get; set; }
}

class OrderModel
{
public IEnumerable<ItemModel> Items { get; set; }
}

class ItemModel
{
public string Name { get; set; }
}

class OrderEntity
{
public OrderData OrderData { get; set; }
}

class OrderData
{
public IEnumerable<ItemEntity> Items { get; set; }
}

class ItemEntity
{
public string Name { get; set; }
}

public class OrganizationProfile : Profile
{
public OrganizationProfile()
Expand Down Expand Up @@ -1024,6 +1062,10 @@ public OrganizationProfile()
.ReverseMap()
.ForMember(d => d.ID, opt => opt.MapFrom(s => s.ID_));

CreateMap<OrderEntity, OrderModel>()
.ForMember(x => x.Items, opt => opt.MapFrom(x => x.OrderData.Items));
CreateMap<ItemEntity, ItemModel>();

CreateMissingTypeMaps = true;
}
}
Expand Down