Browse Source

Fix ExtraProperties dictionary reference handling in mapping

pull/23694/head
SALİH ÖZKARA 11 months ago
parent
commit
ecb87a3309
  1. 29
      framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/MapperlyAutoObjectMappingProvider.cs
  2. 3
      framework/test/Volo.Abp.Mapperly.Tests/Volo/Abp/Mapperly/ExtraProperties_Dictionary_Reference_Tests.cs

29
framework/src/Volo.Abp.Mapperly/Volo/Abp/Mapperly/MapperlyAutoObjectMappingProvider.cs

@ -46,7 +46,7 @@ public class MapperlyAutoObjectMappingProvider : IAutoObjectMappingProvider
{
mapper.BeforeMap((TSource)source);
var destination = mapper.Map((TSource)source);
TryMapExtraProperties(mapper.GetType().GetSingleAttributeOrNull<MapExtraPropertiesAttribute>(), (TSource)source, destination, new ExtraPropertyDictionary());
TryMapExtraProperties(mapper.GetType().GetSingleAttributeOrNull<MapExtraPropertiesAttribute>(), (TSource)source, destination, GetExtraProperties(destination));
mapper.AfterMap((TSource)source, destination);
return destination;
}
@ -245,15 +245,24 @@ public class MapperlyAutoObjectMappingProvider : IAutoObjectMappingProvider
{
return;
}
MapExtraProperties<TSource, TDestination>(
sourceHasExtraProperties,
destinationHasExtraProperties,
destinationExtraProperty,
mapExtraPropertiesAttribute?.DefinitionChecks ?? MappingPropertyDefinitionChecks.Null,
mapExtraPropertiesAttribute?.IgnoredProperties,
mapExtraPropertiesAttribute?.MapToRegularProperties ?? false
);
if (sourceHasExtraProperties.ExtraProperties != null && sourceHasExtraProperties.ExtraProperties ==
destinationHasExtraProperties.ExtraProperties)
{
ObjectHelper.TrySetProperty(destinationHasExtraProperties, x => x.ExtraProperties, () => new ExtraPropertyDictionary(destinationHasExtraProperties.ExtraProperties));;
}
if (mapExtraPropertiesAttribute != null)
{
MapExtraProperties<TSource, TDestination>(
sourceHasExtraProperties,
destinationHasExtraProperties,
destinationExtraProperty,
mapExtraPropertiesAttribute.DefinitionChecks,
mapExtraPropertiesAttribute.IgnoredProperties,
mapExtraPropertiesAttribute.MapToRegularProperties
);
}
}
protected virtual void MapExtraProperties<TSource, TDestination>(
IHasExtraProperties source,

3
framework/test/Volo.Abp.Mapperly.Tests/Volo/Abp/Mapperly/ExtraProperties_Dictionary_Reference_Tests.cs

@ -96,6 +96,9 @@ public class ExtraProperties_Dictionary_Reference_Tests : AbpIntegratedTest<Mapp
// Destination reference may change due to normal mapping process, but should not be same as source
ReferenceEquals(source.ExtraProperties, destination.ExtraProperties).ShouldBeFalse();
destination.ExtraProperties["SourceProperty"].ShouldBe("SourceValue");
destination.ExtraProperties["DestinationProperty"].ShouldBe("DestinationValue");
}
[Fact]

Loading…
Cancel
Save