Browse Source

Merge pull request #21660 from MarkusHoz/dev

Enable object-mapping when source and destination is an enum
pull/21666/head
maliming 2 years ago
committed by GitHub
parent
commit
f36ae3d68d
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 24
      framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs
  2. 10
      framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/AbpAutoMapperModule_Basic_Tests.cs
  3. 8
      framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnum.cs
  4. 8
      framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnumDto.cs
  5. 2
      framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs

24
framework/src/Volo.Abp.ObjectMapping/Volo/Abp/ObjectMapping/DefaultObjectMapper.cs

@ -55,10 +55,9 @@ public class DefaultObjectMapper : IObjectMapper, ITransientDependency
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;
}
}
@ -100,10 +99,9 @@ public class DefaultObjectMapper : IObjectMapper, ITransientDependency
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;
}
}
@ -122,11 +120,12 @@ public class DefaultObjectMapper : IObjectMapper, ITransientDependency
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);
@ -134,7 +133,8 @@ public class DefaultObjectMapper : IObjectMapper, ITransientDependency
if (specificMapper == null)
{
//skip, no specific mapper
return default;
collectionResult = default!;
return false;
}
var cacheKey = $"{mapperType.FullName}_{(destination == null ? "MapMethodWithSingleParameter" : "MapMethodWithDoubleParameters")}";
@ -209,11 +209,13 @@ public class DefaultObjectMapper : IObjectMapper, ITransientDependency
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)

10
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 class AbpAutoMapperModule_Basic_Tests : AbpIntegratedTest<AutoMapperTestM
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()
{

8
framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnum.cs

@ -0,0 +1,8 @@
namespace Volo.Abp.AutoMapper.SampleClasses;
public enum MyEnum
{
Value1 = 1,
Value2,
Value3
}

8
framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyEnumDto.cs

@ -0,0 +1,8 @@
namespace Volo.Abp.AutoMapper.SampleClasses;
public enum MyEnumDto
{
Value1 = 2,
Value2,
Value3
}

2
framework/test/Volo.Abp.AutoMapper.Tests/Volo/Abp/AutoMapper/SampleClasses/MyMapProfile.cs

@ -9,6 +9,8 @@ public class MyMapProfile : Profile
{
CreateMap<MyEntity, MyEntityDto>().ReverseMap();
CreateMap<MyEnum, MyEnumDto>().ReverseMap();
CreateMap<ExtensibleTestPerson, ExtensibleTestPersonDto>()
.MapExtraProperties(ignoredProperties: new[] { "CityName" });

Loading…
Cancel
Save