From cd3291e59a5f53210a1852f6e73daf44f228f4e8 Mon Sep 17 00:00:00 2001 From: MarkusHoz Date: Tue, 17 Dec 2024 11:51:30 +0100 Subject: [PATCH 1/2] Enable object-mapping when source and destination is an enum Before always the default value of the enum was returned from the Map function --- .../Volo/Abp/ObjectMapping/DefaultObjectMapper.cs | 9 ++++++--- .../Abp/AutoMapper/AbpAutoMapperModule_Basic_Tests.cs | 10 +++++++++- .../Volo/Abp/AutoMapper/SampleClasses/MyEnum.cs | 8 ++++++++ .../Volo/Abp/AutoMapper/SampleClasses/MyEnumDto.cs | 8 ++++++++ .../Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs | 2 ++ 5 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnum.cs create mode 100644 framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnumDto.cs diff --git a/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs b/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs index e3577bee6ef..1741a032b7a 100644 --- a/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs +++ b/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs @@ -55,10 +55,13 @@ public virtual TDestination Map(TSource source) return specificMapper.Map(source); } - var result = TryToMapCollection(scope, source, default); - if (result != null) + if (!(typeof(TSource).IsEnum || typeof(TSource).IsPrimitive)) { - return result; + var result = TryToMapCollection(scope, source, default); + if (result != null) + { + return result; + } } } diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_Basic_Tests.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_Basic_Tests.cs index cdaf58b9a4a..6384bf31b76 100644 --- a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_Basic_Tests.cs +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_Basic_Tests.cs @@ -1,4 +1,5 @@ -using Microsoft.Extensions.DependencyInjection; +using System; +using Microsoft.Extensions.DependencyInjection; using Shouldly; using Volo.Abp.AutoMapper.SampleClasses; using Volo.Abp.ObjectMapping; @@ -36,6 +37,13 @@ public void Should_Map_Objects_With_AutoMap_Attributes() dto.Number.ShouldBe(42); } + [Fact] + public void Should_Map_Enum() + { + var dto = _objectMapper.Map(MyEnum.Value3); + dto.ShouldBe(MyEnumDto.Value3); + } + //[Fact] TODO: Disabled because of https://github.com/AutoMapper/AutoMapper/pull/2379#issuecomment-355899664 /*public void Should_Not_Map_Objects_With_AutoMap_Attributes() { diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnum.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnum.cs new file mode 100644 index 00000000000..75f7019fd9d --- /dev/null +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnum.cs @@ -0,0 +1,8 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Volo.Abp.AutoMapper.SampleClasses; +public enum MyEnum { Value1, Value2, Value3 }; diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnumDto.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnumDto.cs new file mode 100644 index 00000000000..759865012bc --- /dev/null +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnumDto.cs @@ -0,0 +1,8 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Volo.Abp.AutoMapper.SampleClasses; +public enum MyEnumDto { Value1, Value2, Value3 }; diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs index f36322ddaac..295ff6b08e4 100644 --- a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs @@ -9,6 +9,8 @@ public MyMapProfile() { CreateMap().ReverseMap(); + CreateMap().ReverseMap(); + CreateMap() .MapExtraProperties(ignoredProperties: new[] { "CityName" }); From 803159bc1f716b69bf40362285d1ea5003b9c1ea Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 18 Dec 2024 14:15:37 +0800 Subject: [PATCH 2/2] Refactor `TryToMapCollection` method. --- .../Abp/ObjectMapping/DefaultObjectMapper.cs | 27 +++++++++---------- .../AbpAutoMapperModule_Basic_Tests.cs | 2 +- .../Abp/AutoMapper/SampleClasses/MyEnum.cs | 14 +++++----- .../Abp/AutoMapper/SampleClasses/MyEnumDto.cs | 14 +++++----- 4 files changed, 28 insertions(+), 29 deletions(-) diff --git a/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs b/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs index 1741a032b7a..30733b1e24b 100644 --- a/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs +++ b/framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs @@ -55,13 +55,9 @@ public virtual TDestination Map(TSource source) return specificMapper.Map(source); } - if (!(typeof(TSource).IsEnum || typeof(TSource).IsPrimitive)) + if (TryToMapCollection(scope, source, default, out var collectionResult)) { - var result = TryToMapCollection(scope, source, default); - if (result != null) - { - return result; - } + return collectionResult; } } @@ -103,10 +99,9 @@ public virtual TDestination Map(TSource source, TDestinat return specificMapper.Map(source, destination); } - var result = TryToMapCollection(scope, source, destination); - if (result != null) + if (TryToMapCollection(scope, source, destination, out var collectionResult)) { - return result; + return collectionResult; } } @@ -125,11 +120,12 @@ public virtual TDestination Map(TSource source, TDestinat return AutoMap(source, destination); } - protected virtual TDestination? TryToMapCollection(IServiceScope serviceScope, TSource source, TDestination? destination) + protected virtual bool TryToMapCollection(IServiceScope serviceScope, TSource source, TDestination? destination, out TDestination collectionResult) { if (!IsCollectionGenericType(out var sourceArgumentType, out var destinationArgumentType, out var definitionGenericType)) { - return default; + collectionResult = default!; + return false; } var mapperType = typeof(IObjectMapper<,>).MakeGenericType(sourceArgumentType, destinationArgumentType); @@ -137,7 +133,8 @@ public virtual TDestination Map(TSource source, TDestinat if (specificMapper == null) { //skip, no specific mapper - return default; + collectionResult = default!; + return false; } var cacheKey = $"{mapperType.FullName}_{(destination == null ? "MapMethodWithSingleParameter" : "MapMethodWithDoubleParameters")}"; @@ -212,11 +209,13 @@ public virtual TDestination Map(TSource source, TDestinat if (destination != null && destination.GetType().IsArray) { //Return the new collection if destination is an array, We won't change array just same behavior as AutoMapper. - return (TDestination)result; + collectionResult = (TDestination)result; + return true; } //Return the destination if destination exists. The parameter reference equals with return object. - return destination ?? (TDestination)result; + collectionResult = destination ?? (TDestination)result; + return true; } protected virtual bool IsCollectionGenericType(out Type sourceArgumentType, out Type destinationArgumentType, out Type definitionGenericType) diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_Basic_Tests.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_Basic_Tests.cs index 6384bf31b76..82a0369aa0c 100644 --- a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_Basic_Tests.cs +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_Basic_Tests.cs @@ -41,7 +41,7 @@ public void Should_Map_Objects_With_AutoMap_Attributes() public void Should_Map_Enum() { var dto = _objectMapper.Map(MyEnum.Value3); - dto.ShouldBe(MyEnumDto.Value3); + dto.ShouldBe(MyEnumDto.Value2); //Value2 is same as Value3 } //[Fact] TODO: Disabled because of https://github.com/AutoMapper/AutoMapper/pull/2379#issuecomment-355899664 diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnum.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnum.cs index 75f7019fd9d..d8b655b4d75 100644 --- a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnum.cs +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnum.cs @@ -1,8 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace Volo.Abp.AutoMapper.SampleClasses; -namespace Volo.Abp.AutoMapper.SampleClasses; -public enum MyEnum { Value1, Value2, Value3 }; +public enum MyEnum +{ + Value1 = 1, + Value2, + Value3 +} diff --git a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnumDto.cs b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnumDto.cs index 759865012bc..fb338186608 100644 --- a/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnumDto.cs +++ b/framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnumDto.cs @@ -1,8 +1,8 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +namespace Volo.Abp.AutoMapper.SampleClasses; -namespace Volo.Abp.AutoMapper.SampleClasses; -public enum MyEnumDto { Value1, Value2, Value3 }; +public enum MyEnumDto +{ + Value1 = 2, + Value2, + Value3 +}