Browse Source

Merge pull request #14472 from abpframework/AbpIncludeExtraPropertiesModifiers

Update `AbpIncludeExtraPropertiesModifiers`.
pull/14502/head
liangshiwei 4 years ago
committed by GitHub
parent
commit
978f8cfb6d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 25
      framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeExtraPropertiesModifiers.cs
  2. 15
      framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObjectModifiers_Tests.cs

25
framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpIncludeExtraPropertiesModifiers.cs

@ -11,19 +11,22 @@ public static class AbpIncludeExtraPropertiesModifiers
{
public static void Modify(JsonTypeInfo jsonTypeInfo)
{
var propertyJsonInfo = jsonTypeInfo.Properties
.Where(x => x.AttributeProvider is MemberInfo)
.FirstOrDefault(x =>
x.PropertyType == typeof(ExtraPropertyDictionary) &&
x.AttributeProvider.As<MemberInfo>().Name == nameof(ExtensibleObject.ExtraProperties) &&
x.Set == null);
if (propertyJsonInfo != null)
if (typeof(IHasExtraProperties).IsAssignableFrom(jsonTypeInfo.Type))
{
propertyJsonInfo.Set = (extraProperties, value) =>
var propertyJsonInfo = jsonTypeInfo.Properties
.Where(x => x.AttributeProvider is MemberInfo)
.FirstOrDefault(x =>
x.PropertyType == typeof(ExtraPropertyDictionary) &&
x.AttributeProvider.As<MemberInfo>().Name == nameof(ExtensibleObject.ExtraProperties) &&
x.Set == null);
if (propertyJsonInfo != null)
{
ObjectHelper.TrySetProperty(extraProperties.As<ExtensibleObject>(), x => x.ExtraProperties, () => (ExtraPropertyDictionary)value);
};
propertyJsonInfo.Set = (obj, value) =>
{
ObjectHelper.TrySetProperty(obj.As<IHasExtraProperties>(), x => x.ExtraProperties, () => value);
};
}
}
}
}

15
framework/test/Volo.Abp.Json.Tests/Volo/Abp/Json/ExtensibleObjectModifiers_Tests.cs

@ -1,4 +1,5 @@
using Shouldly;
using Volo.Abp.Data;
using Volo.Abp.Json.SystemTextJson;
using Volo.Abp.ObjectExtending;
using Xunit;
@ -13,7 +14,19 @@ public class ExtensibleObjectModifiers_Tests : AbpJsonTestBase
var jsonSerializer = GetRequiredService<AbpSystemTextJsonSerializer>();
var extensibleObject = jsonSerializer.Deserialize<ExtensibleObject>("{\"ExtraProperties\": {\"Test-Key\":\"Test-Value\"}}");
extensibleObject.ExtraProperties.ShouldContainKeyAndValue("Test-Key", "Test-Value");
var bar = jsonSerializer.Deserialize<Bar>("{\"ExtraProperties\": {\"Test-Key\":\"Test-Value\"}}");
bar.ExtraProperties.ShouldContainKeyAndValue("Test-Key", "Test-Value");
}
}
public abstract class Foo : IHasExtraProperties
{
public ExtraPropertyDictionary ExtraProperties { get; protected set; }
}
public class Bar : Foo
{
}

Loading…
Cancel
Save