From f5b311ea71329df0699c11051c9b968038cfadcd Mon Sep 17 00:00:00 2001 From: chaowlert Date: Mon, 4 Jul 2016 20:53:28 +0700 Subject: [PATCH] fix #68 - Mapster modifies underlying collection which is being iterated over, when TypeAdapterConfig.GlobalSettings.Compile() is called --- src/Mapster.Tests/Mapster.Tests.csproj | 1 + src/Mapster.Tests/WhenCompilingConfig.cs | 52 ++++++++++++++++++++++++ src/Mapster/Mapster.nuget.targets | 6 --- src/Mapster/TypeAdapterConfig.cs | 12 +++--- src/Mapster/global.json | 2 +- src/Mapster/project.json | 12 +++--- 6 files changed, 67 insertions(+), 18 deletions(-) create mode 100644 src/Mapster.Tests/WhenCompilingConfig.cs delete mode 100644 src/Mapster/Mapster.nuget.targets diff --git a/src/Mapster.Tests/Mapster.Tests.csproj b/src/Mapster.Tests/Mapster.Tests.csproj index 684f6a40..a2622829 100644 --- a/src/Mapster.Tests/Mapster.Tests.csproj +++ b/src/Mapster.Tests/Mapster.Tests.csproj @@ -70,6 +70,7 @@ + diff --git a/src/Mapster.Tests/WhenCompilingConfig.cs b/src/Mapster.Tests/WhenCompilingConfig.cs new file mode 100644 index 00000000..ed9c997f --- /dev/null +++ b/src/Mapster.Tests/WhenCompilingConfig.cs @@ -0,0 +1,52 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using NUnit.Framework; + +namespace Mapster.Tests +{ + [TestFixture] + public class WhenCompilingConfig + { + [TearDown] + public void TearDown() + { + TypeAdapterConfig.GlobalSettings.Clear(); + } + + [Test] + public void Compile_Success_When_Contain_Collection() + { + TypeAdapterConfig.ForType(); + + //Validate globally + TypeAdapterConfig.ForType() + .Map(d => d.DestItems, s => s.SrcItems); + + TypeAdapterConfig.GlobalSettings.Compile(); + } + + class MainSrc + { + public int SrcId { get; set; } + public List SrcItems { get; set; } + } + class MainDest + { + public int DestId { get; set; } + public List DestItems { get; set; } + } + class SrcItem + { + public int ItemId { get; set; } + public string StringData { get; set; } + } + class DestItem + { + public int ItemId { get; set; } + public string StringData { get; set; } + } + } +} diff --git a/src/Mapster/Mapster.nuget.targets b/src/Mapster/Mapster.nuget.targets deleted file mode 100644 index e69ce0e6..00000000 --- a/src/Mapster/Mapster.nuget.targets +++ /dev/null @@ -1,6 +0,0 @@ - - - - - - \ No newline at end of file diff --git a/src/Mapster/TypeAdapterConfig.cs b/src/Mapster/TypeAdapterConfig.cs index 5767ea3f..735a241b 100644 --- a/src/Mapster/TypeAdapterConfig.cs +++ b/src/Mapster/TypeAdapterConfig.cs @@ -399,10 +399,11 @@ orderby priority.Value descending public void Compile() { - foreach (var kvp in RuleMap) + var keys = RuleMap.Keys.ToList(); + foreach (var key in keys) { - _mapDict[kvp.Key] = CreateMapFunction(kvp.Key); - _mapToTargetDict[kvp.Key] = CreateMapToTargetFunction(kvp.Key); + _mapDict[key] = CreateMapFunction(key); + _mapToTargetDict[key] = CreateMapToTargetFunction(key); } } @@ -415,9 +416,10 @@ public void Compile(Type sourceType, Type destinationType) public void CompileProjection() { - foreach (var kvp in RuleMap) + var keys = RuleMap.Keys.ToList(); + foreach (var key in keys) { - _projectionDict[kvp.Key] = CreateProjectionCallExpression(kvp.Key); + _projectionDict[key] = CreateProjectionCallExpression(key); } } diff --git a/src/Mapster/global.json b/src/Mapster/global.json index 820d7b14..156738fd 100644 --- a/src/Mapster/global.json +++ b/src/Mapster/global.json @@ -1,6 +1,6 @@ { "projects": [ ], "sdk": { - "version": "1.0.0-preview1-002702" + "version": "1.0.0-preview2-003121" } } diff --git a/src/Mapster/project.json b/src/Mapster/project.json index 2bfc4aba..57979ad7 100644 --- a/src/Mapster/project.json +++ b/src/Mapster/project.json @@ -1,5 +1,5 @@ { - "version": "2.4.1", + "version": "2.4.2", "description": "A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, just simpler and way, way faster.", "packOptions": { "summary": "A fast, fun and stimulating object to object mapper. Kind of like AutoMapper, but simpler and way faster.", @@ -22,11 +22,11 @@ "net45": { }, "netstandard1.3": { "dependencies": { - "Microsoft.CSharp": "4.0.1-rc2-24027", - "NETStandard.Library": "1.5.0-rc2-24027", - "System.Collections.NonGeneric": "4.0.1-rc2-24027", - "System.Linq.Queryable": "4.0.1-rc2-24027", - "System.Reflection.TypeExtensions": "4.1.0-rc2-24027" + "Microsoft.CSharp": "4.0.1", + "NETStandard.Library": "1.6.0", + "System.Collections.NonGeneric": "4.0.1", + "System.Linq.Queryable": "4.0.1", + "System.Reflection.TypeExtensions": "4.1.0" } } }