Fix unit tests.

pull/13357/head
maliming 3 years ago
parent 1e9c0871a3
commit c59d75be61
No known key found for this signature in database
GPG Key ID: 096224957E51C89E

@ -23,9 +23,6 @@ public class AbpJsonOptionsSetup : IConfigureOptions<JsonOptions>
options.JsonSerializerOptions.ReadCommentHandling = JsonCommentHandling.Skip;
options.JsonSerializerOptions.AllowTrailingCommas = true;
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpDateTimeConverter>());
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpNullableDateTimeConverter>());
options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumFactory());
options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter());

@ -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)

@ -12,10 +12,5 @@ public class AbpJsonNewtonsoftModule : AbpModule
{
options.Providers.Add<AbpNewtonsoftJsonSerializerProvider>();
});
Configure<AbpNewtonsoftJsonSerializerOptions>(options =>
{
options.Converters.Add<AbpJsonIsoDateTimeConverter>();
});
}
}

@ -12,6 +12,7 @@ public class AbpJsonSystemTextJsonModule : AbpModule
public override void ConfigureServices(ServiceConfigurationContext context)
{
context.Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<AbpSystemTextJsonSerializerOptions>, AbpSystemTextJsonSerializerOptionsSetup>());
context.Services.TryAddEnumerable(ServiceDescriptor.Transient<IConfigureOptions<AbpSystemTextJsonSerializerModifiersOptions>, AbpSystemTextJsonSerializerModifiersOptionsSetup>());
Configure<AbpJsonOptions>(options =>
{

@ -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<Action<JsonTypeInfo>>
{
new IncludeNonPublicPropertiesModifiers<ExtensibleObject, ExtraPropertyDictionary>().CreateModifyAction("ExtraProperties")
IncludeExtraPropertiesModifiers.Modify,
};
}
}

@ -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<AbpSystemTextJsonSerializerModifiersOptions>
{
protected IServiceProvider ServiceProvider { get; }
public AbpSystemTextJsonSerializerModifiersOptionsSetup(IServiceProvider serviceProvider)
{
ServiceProvider = serviceProvider;
}
public void Configure(AbpSystemTextJsonSerializerModifiersOptions options)
{
options.Modifiers.Add(new AbpDateTimeConverterModifier().CreateModifyAction(ServiceProvider));
}
}

@ -18,9 +18,6 @@ public class AbpSystemTextJsonSerializerOptionsSetup : IConfigureOptions<AbpSyst
public void Configure(AbpSystemTextJsonSerializerOptions options)
{
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpDateTimeConverter>());
options.JsonSerializerOptions.Converters.Add(ServiceProvider.GetRequiredService<AbpNullableDateTimeConverter>());
options.JsonSerializerOptions.Converters.Add(new AbpStringToEnumFactory());
options.JsonSerializerOptions.Converters.Add(new AbpStringToBooleanConverter());

@ -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<JsonTypeInfo> CreateModifyAction(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
return Modify;
}
private void Modify(JsonTypeInfo jsonTypeInfo)
{
if (ReflectionHelper.GetAttributesOfMemberOrDeclaringType<DisableDateTimeNormalizationAttribute>(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<AbpDateTimeConverter>()
: _serviceProvider.GetRequiredService<AbpNullableDateTimeConverter>();
}
}
}
}

@ -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);
}
}
}
}

@ -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<AbpSystemTextJsonSerializerOptions>(options =>
{
options.UnsupportedTypes.Add<GetDateTimeKindModel>();
options.UnsupportedTypes.Add<GetDateTimeKindModel.GetDateTimeKindInnerModel>();
});
}
[Fact]
public async Task DateTimeKind_Test()
{

Loading…
Cancel
Save