Browse Source

Merge pull request #15642 from abpframework/auto-merge/rel-7-0/1709

Merge branch rel-7.1 with rel-7.0
pull/15643/head
maliming 3 years ago
committed by GitHub
parent
commit
4c3b9a6701
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 35
      framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObjectMapper.cs
  2. 31
      framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/HasExtraPropertiesObjectExtendingExtensions_Tests.cs

35
framework/src/Volo.Abp.ObjectExtending/Volo/Abp/ObjectExtending/ExtensibleObjectMapper.cs

@ -11,7 +11,7 @@ public static class ExtensibleObjectMapper
/// <summary>
/// Copies extra properties from the <paramref name="source"/> object
/// to the <paramref name="destination"/> object.
///
///
/// Checks property definitions (over the <see cref="ObjectExtensionManager"/>)
/// based on the <paramref name="definitionChecks"/> preference.
/// </summary>
@ -34,14 +34,29 @@ public static class ExtensibleObjectMapper
Check.NotNull(source, nameof(source));
Check.NotNull(destination, nameof(destination));
ExtensibleObjectMapper.MapExtraPropertiesTo(
typeof(TSource),
typeof(TDestination),
source.ExtraProperties,
destination.ExtraProperties,
definitionChecks,
ignoredProperties
);
var sourceType = typeof(TSource);
var destinationType = typeof(TDestination);
Check.AssignableTo<IHasExtraProperties>(sourceType, nameof(sourceType));
Check.AssignableTo<IHasExtraProperties>(destinationType, nameof(destinationType));
Check.NotNull(source.ExtraProperties, nameof(source.ExtraProperties));
Check.NotNull(destination.ExtraProperties, nameof(destination.ExtraProperties));
var sourceObjectExtension = ObjectExtensionManager.Instance.GetOrNull(sourceType);
var destinationObjectExtension = ObjectExtensionManager.Instance.GetOrNull(destinationType);
foreach (var keyValue in source.ExtraProperties)
{
if (CanMapProperty(
keyValue.Key,
sourceObjectExtension,
destinationObjectExtension,
definitionChecks,
ignoredProperties))
{
destination.SetProperty(keyValue.Key, keyValue.Value);
}
}
}
/// <summary>
@ -80,7 +95,7 @@ public static class ExtensibleObjectMapper
/// <summary>
/// Copies extra properties from the <paramref name="sourceDictionary"/> object
/// to the <paramref name="destinationDictionary"/> object.
///
///
/// Checks property definitions (over the <see cref="ObjectExtensionManager"/>)
/// based on the <paramref name="definitionChecks"/> preference.
/// </summary>

31
framework/test/Volo.Abp.ObjectExtending.Tests/Volo/Abp/ObjectExtending/HasExtraPropertiesObjectExtendingExtensions_Tests.cs

@ -1,6 +1,10 @@
using Shouldly;
using System.ComponentModel.DataAnnotations;
using System.Globalization;
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.Localization;
using Volo.Abp.ObjectExtending.TestObjects;
using Volo.Abp.Validation;
using Xunit;
namespace Volo.Abp.ObjectExtending;
@ -96,4 +100,29 @@ public class HasExtraPropertiesObjectExtendingExtensions_Tests : AbpObjectExtend
_personDto.GetProperty<ExtensibleTestEnumProperty>("EnumProperty").ShouldBe(ExtensibleTestEnumProperty.Value1);
_personDto.GetProperty<string>("ExistingDtoProperty").ShouldBe("existing-value"); //Should not clear existing values
}
[Fact]
public void MapExtraPropertiesTo_Should_Validate_Properties()
{
using (CultureHelper.Use(CultureInfo.InvariantCulture))
{
ObjectExtensionManager.Instance
.AddOrUpdateProperty<ExtensibleTestPersonDto, string>(
"Name",
options =>
{
options.Attributes.Add(new StringLengthAttribute(4));
});
var person = new ExtensibleTestPerson().SetProperty("Name", "John");
var personDto = new ExtensibleTestPersonDto();
person.MapExtraPropertiesTo(personDto);
person.SetProperty("Name", "John Bush");
Assert.Throws<AbpValidationException>(() => person.MapExtraPropertiesTo(personDto)).ValidationErrors.ShouldContain(x => x.ErrorMessage == "The field Name must be a string with a maximum length of 4.");
}
}
}

Loading…
Cancel
Save