Skip to content

Commit

Permalink
Merge pull request #21660 from MarkusHoz/dev
Browse files Browse the repository at this point in the history
Enable object-mapping when source and destination is an enum
  • Loading branch information
maliming authored Dec 18, 2024
2 parents 7a7d191 + 803159b commit f36ae3d
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ public virtual TDestination Map<TSource, TDestination>(TSource source)
return specificMapper.Map(source);
}

var result = TryToMapCollection<TSource, TDestination>(scope, source, default);
if (result != null)
if (TryToMapCollection<TSource, TDestination>(scope, source, default, out var collectionResult))
{
return result;
return collectionResult;
}
}

Expand Down Expand Up @@ -100,10 +99,9 @@ public virtual TDestination Map<TSource, TDestination>(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;
}
}

Expand All @@ -122,19 +120,21 @@ public virtual TDestination Map<TSource, TDestination>(TSource source, TDestinat
return AutoMap(source, destination);
}

protected virtual TDestination? TryToMapCollection<TSource, TDestination>(IServiceScope serviceScope, TSource source, TDestination? destination)
protected virtual bool TryToMapCollection<TSource, TDestination>(IServiceScope serviceScope, TSource source, TDestination? destination, out TDestination collectionResult)
{
if (!IsCollectionGenericType<TSource, TDestination>(out var sourceArgumentType, out var destinationArgumentType, out var definitionGenericType))
{
return default;
collectionResult = default!;
return false;
}

var mapperType = typeof(IObjectMapper<,>).MakeGenericType(sourceArgumentType, destinationArgumentType);
var specificMapper = serviceScope.ServiceProvider.GetService(mapperType);
if (specificMapper == null)
{
//skip, no specific mapper
return default;
collectionResult = default!;
return false;
}

var cacheKey = $"{mapperType.FullName}_{(destination == null ? "MapMethodWithSingleParameter" : "MapMethodWithDoubleParameters")}";
Expand Down Expand Up @@ -209,11 +209,13 @@ public virtual TDestination Map<TSource, TDestination>(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<TSource, TDestination>(out Type sourceArgumentType, out Type destinationArgumentType, out Type definitionGenericType)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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, MyEnumDto>(MyEnum.Value3);
dto.ShouldBe(MyEnumDto.Value2); //Value2 is same as Value3
}

//[Fact] TODO: Disabled because of https://github.com/AutoMapper/AutoMapper/pull/2379#issuecomment-355899664
/*public void Should_Not_Map_Objects_With_AutoMap_Attributes()
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Volo.Abp.AutoMapper.SampleClasses;

public enum MyEnum
{
Value1 = 1,
Value2,
Value3
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Volo.Abp.AutoMapper.SampleClasses;

public enum MyEnumDto
{
Value1 = 2,
Value2,
Value3
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public MyMapProfile()
{
CreateMap<MyEntity, MyEntityDto>().ReverseMap();

CreateMap<MyEnum, MyEnumDto>().ReverseMap();

CreateMap<ExtensibleTestPerson, ExtensibleTestPersonDto>()
.MapExtraProperties(ignoredProperties: new[] { "CityName" });

Expand Down

0 comments on commit f36ae3d

Please sign in to comment.