ArgumentException when mapping from IEnumerable<T> to T[] when AllowNullCollections is on

44 views
Skip to first unread message

ccbr...@gmail.com

unread,
Jul 6, 2016, 2:05:03 PM7/6/16
to AutoMapper-users
Hey Jimmy - A second issue I am hitting during upgrade to 5.0.

I am getting this exception:

Unable to create instance of class AutoMapperTest.UnitTest1. Error: System.ArgumentException: Argument types do not match.
    at System.Linq.Expressions.Expression.Condition(Expression test, Expression ifTrue, Expression ifFalse)
   at AutoMapper.Mappers.ArrayMapper.MapExpression(TypeMapRegistry typeMapRegistry, IConfigurationProvider configurationProvider, PropertyMap propertyMap, Expression sourceExpression, Expression destExpression, Expression contextExpression)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreatePropertyMapFunc(PropertyMap propertyMap, IConfigurationProvider configurationProvider, TypeMapRegistry typeMapRegistry, ParameterExpression srcParam, ParameterExpression destParam, ParameterExpression ctxtParam)
   at AutoMapper.Execution.TypeMapPlanBuilder.TryPropertyMap(PropertyMap pm, IConfigurationProvider configurationProvider, TypeMapRegistry registry, ParameterExpression srcParam, ParameterExpression destParam, ParameterExpression ctxtParam)
   at AutoMapper.Execution.TypeMapPlanBuilder.<>c__DisplayClass5_0.<CreateAssignmentFunc>b__1(PropertyMap pm)
   at System.Linq.Enumerable.WhereSelectArrayIterator`2.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at AutoMapper.Execution.TypeMapPlanBuilder.CreateAssignmentFunc(TypeMap typeMap, IConfigurationProvider configurationProvider, TypeMapRegistry registry, ParameterExpression srcParam, ParameterExpression destParam, ParameterExpression ctxtParam, Expression destinationFunc)
   at AutoMapper.Execution.TypeMapPlanBuilder.BuildMapperFunc(TypeMap typeMap, IConfigurationProvider configurationProvider, TypeMapRegistry 

when using these mappings:

[TestClass]
public class UnitTest1
{
	static UnitTest1()
	{
		Mapper.Initialize(cfg =>
		{
			cfg.AllowNullCollections = true; // removing this makes it work; unfortunately, I can't turn it off

			cfg.CreateMap<Source, Target>();
			cfg.CreateMap<SourceItem, TargetItem>();
		});
	}

	[TestMethod]
	public void IncludedMappings()
	{
		var src = new Source
		{
			X = 5,
			Items = new List<SourceItem>
					{
						new SourceItem { I = 1 },
						new SourceItem { I = 2 },
						new SourceItem { I = 3 }
					}
		};

		var dest = Mapper.Map<Source, Target>(src);

		Assert.AreEqual(src.X, dest.X);

		Assert.AreEqual(3, dest.Items.Length);
		Assert.AreEqual(1, dest.Items[0].I);
		Assert.AreEqual(2, dest.Items[1].I);
		Assert.AreEqual(3, dest.Items[2].I);
	}
}

class Source
{
	public int X { get; set; }
	public IEnumerable<SourceItem> Items { get; set; }
}

class SourceItem
{
	public int I { get; set; }
}

class Target
{
	public int X { get; set; }
	public TargetItem[] Items { get; set; }
}

class TargetItem
{
	public int I { get; set; }
}

From what I see, it looks like this worked from 3.3 all the way through the 4.2.1 release, but no longer works in 5.0.

Thanks again!
Chris Bristol

Jimmy Bogard

unread,
Jul 6, 2016, 2:17:59 PM7/6/16
to automapper-users
Have you tried the MyGet version? There's a link on the GH site.

--
You received this message because you are subscribed to the Google Groups "AutoMapper-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to automapper-use...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ccbr...@gmail.com

unread,
Jul 6, 2016, 2:26:48 PM7/6/16
to AutoMapper-users
Yep - same result with the 5.0.1-alpha-01152 version.

Jimmy Bogard

unread,
Jul 6, 2016, 3:18:19 PM7/6/16
to automapper-users
Looks like a GH issue then.

ccbr...@gmail.com

unread,
Jul 6, 2016, 4:20:27 PM7/6/16
to AutoMapper-users
Created #1467

FWIW, I tried looking into this, but after cloning the repository from github AutoMapper project is failing to load. I figure that means I am missing a step somewhere. Is there a getting started for developers guide out there?

- Chris

Jimmy Bogard

unread,
Jul 6, 2016, 4:29:49 PM7/6/16
to automapper-users
You need VS 2015 with Update 3 and the .NET Core RTM SDK installed.
Reply all
Reply to author
Forward
0 new messages