Skip to content

ArgumentException when mapping field from common base-class to disjoint target types #130

@bslbckr

Description

@bslbckr

I start feeling bad, but I probably found another bug. I receive the following ArgumentException due to duplicate keys in a dictionary:

Run-time exception (line 56): An item with the same key has already been added.

Stack Trace:

[System.ArgumentException: An item with the same key has already been added.]
   at System.ThrowHelper.ThrowArgumentException(ExceptionResource resource)
   at System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add)
   at AgileObjects.AgileMapper.Members.MemberMapperDataExtensions.RegisterTargetMemberDataSourcesIfRequired(IMemberMapperData mapperData, DataSourceSet dataSources)
   at AgileObjects.AgileMapper.Members.Population.MemberPopulator.WithRegistration(IChildMemberMappingData mappingData, DataSourceSet dataSources, Expression populateCondition)
   at AgileObjects.AgileMapper.Members.Population.MemberPopulatorFactory.Create(QualifiedMember targetMember, IObjectMappingData mappingData)
   at AgileObjects.AgileMapper.Members.Population.MemberPopulatorFactory.<>c__DisplayClass3_0.<Create>b__0(QualifiedMember tm)
   at AgileObjects.AgileMapper.Extensions.PublicEnumerableExtensions.<Project>d__0`2.MoveNext()
   at AgileObjects.AgileMapper.Extensions.PublicEnumerableExtensions.<Filter>d__2`1.MoveNext()
   at AgileObjects.AgileMapper.ObjectPopulation.ComplexTypes.PopulationExpressionFactoryBase.<GetPopulationsAndCallbacks>d__3.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at AgileObjects.AgileMapper.ObjectPopulation.ComplexTypes.PopulationExpressionFactoryBase.<GetPopulation>d__0.MoveNext()
   at AgileObjects.AgileMapper.Extensions.PublicEnumerableExtensions.<Filter>d__2`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at AgileObjects.AgileMapper.ObjectPopulation.MappingExpressionFactoryBase.<GetConfiguredRootDataSourcePopulations>d__10.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at AgileObjects.AgileMapper.ObjectPopulation.MappingExpressionFactoryBase.Create(IObjectMappingData mappingData)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMapperFactory.Create[TSource,TTarget](ObjectMappingData`2 mappingData)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingData`2.GetOrCreateMapper()
   at AgileObjects.AgileMapper.ObjectPopulation.MappingFactory.GetInlineMappingBlock(IObjectMappingData mappingData, MappingValues mappingValues, Expression createMappingDataCall)
   at AgileObjects.AgileMapper.ObjectPopulation.MappingFactory.GetChildMapping(IObjectMappingData childMappingData, MappingValues mappingValues, Int32 dataSourceIndex, ObjectMapperData declaredTypeMapperData)
   at AgileObjects.AgileMapper.ObjectPopulation.MappingFactory.GetChildMapping(IQualifiedMember sourceMember, Expression sourceMemberAccess, Int32 dataSourceIndex, IChildMemberMappingData childMappingData)
   at AgileObjects.AgileMapper.DataSources.ComplexTypeMappingDataSource.GetMapping(IDataSource complexTypeDataSource, Int32 dataSourceIndex, IChildMemberMappingData complexTypeMappingData)
   at AgileObjects.AgileMapper.DataSources.Finders.DataSourceFindContext.GetFinalDataSource(IDataSource foundDataSource, IChildMemberMappingData mappingData)
   at AgileObjects.AgileMapper.DataSources.Finders.ConfiguredDataSourceFinder.<FindFor>d__0.MoveNext()
   at AgileObjects.AgileMapper.DataSources.Finders.DataSourceFinder.<EnumerateDataSources>d__1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.Enumerable.ToArray[TSource](IEnumerable`1 source)
   at AgileObjects.AgileMapper.DataSources.Finders.DataSourceFinder.FindFor(IChildMemberMappingData childMappingData)
   at AgileObjects.AgileMapper.Members.Population.MemberPopulatorFactory.Create(QualifiedMember targetMember, IObjectMappingData mappingData)
   at AgileObjects.AgileMapper.Members.Population.MemberPopulatorFactory.<>c__DisplayClass3_0.<Create>b__0(QualifiedMember tm)
   at AgileObjects.AgileMapper.Extensions.PublicEnumerableExtensions.<Project>d__0`2.MoveNext()
   at AgileObjects.AgileMapper.Extensions.PublicEnumerableExtensions.<Filter>d__2`1.MoveNext()
   at AgileObjects.AgileMapper.ObjectPopulation.ComplexTypes.PopulationExpressionFactoryBase.<GetPopulationsAndCallbacks>d__3.MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at AgileObjects.AgileMapper.ObjectPopulation.ComplexTypes.PopulationExpressionFactoryBase.<GetPopulation>d__0.MoveNext()
   at AgileObjects.AgileMapper.Extensions.PublicEnumerableExtensions.<Filter>d__2`1.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at AgileObjects.AgileMapper.ObjectPopulation.MappingExpressionFactoryBase.Create(IObjectMappingData mappingData)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMapperFactory.Create[TSource,TTarget](ObjectMappingData`2 mappingData)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingData`2.GetOrCreateMapper()
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMapperFactory.<>c__7`2.<GetOrCreateRoot>b__7_0(IRootMapperKey key)
   at AgileObjects.AgileMapper.Caching.ArrayCache`2.GetOrAdd(TKey key, Func`2 valueFactory)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMapperFactory.GetOrCreateRoot[TSource,TTarget](ObjectMappingData`2 mappingData)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingData`2..ctor(TSource source, TTarget target, Nullable`1 enumerableIndex, MappingTypes mappingTypes, IMappingContext mappingContext, IObjectMappingData declaredTypeMappingData, IObjectMappingData parent, Boolean createMapper)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingDataFactory.ForRootFixedTypes[TSource,TTarget](TSource source, TTarget target, MappingTypes mappingTypes, IMappingContext mappingContext, Boolean createMapper)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingDataFactory.ForRootFixedTypes[TSource,TTarget](TSource source, TTarget target, IMappingContext mappingContext, Boolean createMapper)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingDataFactory.ForRootFixedTypes[TSource,TTarget](IMappingContext mappingContext, Boolean createMapper)
   at AgileObjects.AgileMapper.ObjectPopulation.ObjectMappingDataFactory.ForRootFixedTypes[TSource,TTarget](IMappingContext mappingContext)
   at AgileObjects.AgileMapper.Api.PlanTargetSelector`1.GetMappingPlan[TTarget](IMappingContext planContext, Func`2 mappingDataFactory, ICollection`1 configurations)
   at AgileObjects.AgileMapper.Api.PlanTargetSelector`1.GetMappingPlan[TTarget](MappingRuleSet ruleSet, ICollection`1 configurations)
   at AgileObjects.AgileMapper.Api.PlanTargetSelector`1.ToANew[TResult](Expression`1[] configurations)

For some reasons AgileMapper gets confused by my target types:

namespace Target {
	public class DataA {
		public IList<string> ReferencedIds {get; set;}
		
		public string Name{get;set;}
	}
	public class DataB {
		public IList<string> ReferencedIds {get; set;}
		public string Id {get;set;}
	}
	public class Wrapper {
		public enum WrapperCase { A, B}
		public WrapperCase Case {get;set;}
		public DataA A {get;set;}
		public DataB B {get;set;}
	}
}

I assume that the problem is that Wrapper.A.ReferencedIds could not be distinguished from Wrapper.B.ReferencedIds.

The complete example can be found in this small fiddle: https://dotnetfiddle.net/6pU2Af

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions