From c59d75be61b69c3122311a7dc65bb5bbb427c28b Mon Sep 17 00:00:00 2001 From: maliming Date: Wed, 17 Aug 2022 11:14:14 +0800 Subject: [PATCH] Fix unit tests. --- .../Mvc/Json/AbpJsonOptionsSetup.cs | 3 -- .../Newtonsoft/AbpJsonIsoDateTimeConverter.cs | 7 +--- .../Newtonsoft/AbpJsonNewtonsoftModule.cs | 5 --- .../AbpJsonSystemTextJsonModule.cs | 1 + ...ystemTextJsonSerializerModifiersOptions.cs | 5 +-- ...TextJsonSerializerModifiersOptionsSetup.cs | 20 ++++++++++ ...AbpSystemTextJsonSerializerOptionsSetup.cs | 3 -- .../Modifiers/AbpDateTimeConverterModifier.cs | 39 +++++++++++++++++++ .../IncludeExtraPropertiesModifiers.cs | 29 ++++++++++++++ .../ModelBindingController_Tests.cs | 10 ----- 10 files changed, 92 insertions(+), 30 deletions(-) create mode 100644 framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptionsSetup.cs create mode 100644 framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpDateTimeConverterModifier.cs create mode 100644 framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeExtraPropertiesModifiers.cs diff --git a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs index 650d3890c1..05c57ee5a7 100644 --- a/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs +++ b/framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/Json/AbpJsonOptionsSetup.cs @@ -23,9 +23,6 @@ public class AbpJsonOptionsSetup : IConfigureOptions options.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip; options.JsonSerializerOptions.AllowTrailingCommas = true; - options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService()); - options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService()); - options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumFactory()); options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter()); diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs index 660a69e4e7..605f06e9a6 100644 --- a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonIsoDateTimeConverter.cs @@ -23,12 +23,7 @@ public class AbpJsonIsoDateTimeConverter : IsoDateTimeConverter, ITransientDepen public override bool CanConvert(Type objectType) { - if (objectType == typeof(DateTime) || objectType == typeof(DateTime?)) - { - return true; - } - - return false; + return objectType == typeof(DateTime) || objectType == typeof(DateTime?); } public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) diff --git a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonNewtonsoftModule.cs b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonNewtonsoftModule.cs index afa419bba2..e276821bcb 100644 --- a/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonNewtonsoftModule.cs +++ b/framework/src/Volo.Abp.Json.Newtonsoft/Volo/Abp/Json/Newtonsoft/AbpJsonNewtonsoftModule.cs @@ -12,10 +12,5 @@ public class AbpJsonNewtonsoftModule : AbpModule { options.Providers.Add(); }); - - Configure(options => - { - options.Converters.Add(); - }); } } diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpJsonSystemTextJsonModule.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpJsonSystemTextJsonModule.cs index 895c9bcfe4..ac7e66a7ef 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpJsonSystemTextJsonModule.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpJsonSystemTextJsonModule.cs @@ -12,6 +12,7 @@ public class AbpJsonSystemTextJsonModule : AbpModule public override void ConfigureServices(ServiceConfigurationContext context) { context.Services.TryAddEnumerable(ServiceDescriptor.Transient, AbpSystemTextJsonSerializerOptionsSetup>()); + context.Services.TryAddEnumerable(ServiceDescriptor.Transient, AbpSystemTextJsonSerializerModifiersOptionsSetup>()); Configure(options => { diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptions.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptions.cs index 4f4f8d1bac..efc0a6a883 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptions.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptions.cs @@ -1,9 +1,8 @@ using System; using System.Collections.Generic; using System.Text.Json.Serialization.Metadata; -using Volo.Abp.Data; using Volo.Abp.Json.SystemTextJson.Modifiers; -using Volo.Abp.ObjectExtending; + namespace Volo.Abp.Json.SystemTextJson; @@ -15,7 +14,7 @@ public class AbpSystemTextJsonSerializerModifiersOptions { Modifiers = new List> { - new IncludeNonPublicPropertiesModifiers().CreateModifyAction("ExtraProperties") + IncludeExtraPropertiesModifiers.Modify, }; } } diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptionsSetup.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptionsSetup.cs new file mode 100644 index 0000000000..f0ee1e9cfb --- /dev/null +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerModifiersOptionsSetup.cs @@ -0,0 +1,20 @@ +using System; +using Microsoft.Extensions.Options; +using Volo.Abp.Json.SystemTextJson.Modifiers; + +namespace Volo.Abp.Json.SystemTextJson; + +public class AbpSystemTextJsonSerializerModifiersOptionsSetup : IConfigureOptions +{ + protected IServiceProvider ServiceProvider { get; } + + public AbpSystemTextJsonSerializerModifiersOptionsSetup(IServiceProvider serviceProvider) + { + ServiceProvider = serviceProvider; + } + + public void Configure(AbpSystemTextJsonSerializerModifiersOptions options) + { + options.Modifiers.Add(new AbpDateTimeConverterModifier().CreateModifyAction(ServiceProvider)); + } +} diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs index a5806b28be..a9a35c1d47 100644 --- a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/AbpSystemTextJsonSerializerOptionsSetup.cs @@ -18,9 +18,6 @@ public class AbpSystemTextJsonSerializerOptionsSetup : IConfigureOptions()); - options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService()); - options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumFactory()); options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter()); diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpDateTimeConverterModifier.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpDateTimeConverterModifier.cs new file mode 100644 index 0000000000..a691423cf3 --- /dev/null +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/AbpDateTimeConverterModifier.cs @@ -0,0 +1,39 @@ +using System; +using System.Linq; +using System.Text.Json.Serialization.Metadata; +using Microsoft.Extensions.DependencyInjection; +using Volo.Abp.Json.SystemTextJson.JsonConverters; +using Volo.Abp.Reflection; +using Volo.Abp.Timing; + +namespace Volo.Abp.Json.SystemTextJson.Modifiers; + +public class AbpDateTimeConverterModifier +{ + private IServiceProvider _serviceProvider; + + public Action CreateModifyAction(IServiceProvider serviceProvider) + { + _serviceProvider = serviceProvider; + return Modify; + } + + private void Modify(JsonTypeInfo jsonTypeInfo) + { + if (ReflectionHelper.GetAttributesOfMemberOrDeclaringType(jsonTypeInfo.Type).Any()) + { + return; + } + + foreach (var property in jsonTypeInfo.Properties.Where(x => x.PropertyType == typeof(DateTime) || x.PropertyType == typeof(DateTime?))) + { + if (property.AttributeProvider == null || + !property.AttributeProvider.GetCustomAttributes(typeof(DisableDateTimeNormalizationAttribute), false).Any()) + { + property.CustomConverter = property.PropertyType == typeof(DateTime) + ? _serviceProvider.GetRequiredService() + : _serviceProvider.GetRequiredService(); + } + } + } +} diff --git a/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeExtraPropertiesModifiers.cs b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeExtraPropertiesModifiers.cs new file mode 100644 index 0000000000..b96a73e1f7 --- /dev/null +++ b/framework/src/Volo.Abp.Json.SystemTextJson/Volo/Abp/Json/SystemTextJson/Modifiers/IncludeExtraPropertiesModifiers.cs @@ -0,0 +1,29 @@ +using System; +using System.Linq; +using System.Text.Json.Serialization.Metadata; +using Volo.Abp.Data; +using Volo.Abp.ObjectExtending; + +namespace Volo.Abp.Json.SystemTextJson.Modifiers; + +public static class IncludeExtraPropertiesModifiers +{ + public static void Modify(JsonTypeInfo jsonTypeInfo) + { + var propertyJsonInfo = jsonTypeInfo.Properties.FirstOrDefault(x => x.PropertyType == typeof(ExtraPropertyDictionary) && + x.Name.Equals(nameof(ExtensibleObject.ExtraProperties), StringComparison.OrdinalIgnoreCase) && + x.Set == null); + if (propertyJsonInfo != null) + { + var propertyInfo = jsonTypeInfo.Type.GetProperty(nameof(ExtensibleObject.ExtraProperties)); + if (propertyInfo != null) + { + var jsonPropertyInfo = jsonTypeInfo.CreateJsonPropertyInfo(typeof(ExtraPropertyDictionary), propertyJsonInfo.Name); + jsonPropertyInfo.Get = propertyInfo.GetValue; + jsonPropertyInfo.Set = propertyInfo.SetValue; + jsonTypeInfo.Properties.Remove(propertyJsonInfo); + jsonTypeInfo.Properties.Add(jsonPropertyInfo); + } + } + } +} diff --git a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs index 0d30d101cb..2850ca82c7 100644 --- a/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs +++ b/framework/test/Volo.Abp.AspNetCore.Mvc.Tests/Volo/Abp/AspNetCore/Mvc/ModelBinding/ModelBindingController_Tests.cs @@ -8,7 +8,6 @@ using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using Shouldly; using Volo.Abp.Http; -using Volo.Abp.Json.SystemTextJson; using Volo.Abp.Timing; using Xunit; @@ -18,15 +17,6 @@ public abstract class ModelBindingController_Tests : AspNetCoreMvcTestBase { protected DateTimeKind Kind { get; set; } - protected override void ConfigureServices(HostBuilderContext context, IServiceCollection services) - { - services.Configure(options => - { - options.UnsupportedTypes.Add(); - options.UnsupportedTypes.Add(); - }); - } - [Fact] public async Task DateTimeKind_Test() {