diff --git a/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs b/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs index cfbcf1c95e..b94459dc68 100644 --- a/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs +++ b/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/AbpFeatureManagementApplicationContractsModule.cs @@ -4,7 +4,6 @@ using Volo.Abp.Application; using Volo.Abp.Authorization; using Volo.Abp.FeatureManagement.JsonConverters; using Volo.Abp.Json; -using Volo.Abp.Json.Newtonsoft; using Volo.Abp.Json.SystemTextJson; using Volo.Abp.Modularity; using Volo.Abp.VirtualFileSystem; @@ -32,11 +31,6 @@ public class AbpFeatureManagementApplicationContractsModule : AbpModule contractsOptionsActions.Configure(options); }); - Configure(options => - { - options.Converters.Add(); - }); - Configure(options => { options.JsonSerializerOptions.Converters.AddIfNotContains(new StringValueTypeJsonConverter(contractsOptionsActions.Configure())); diff --git a/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/NewtonsoftStringValueTypeJsonConverter.cs b/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/NewtonsoftStringValueTypeJsonConverter.cs deleted file mode 100644 index aa9a913718..0000000000 --- a/modules/feature-management/src/Volo.Abp.FeatureManagement.Application.Contracts/Volo/Abp/FeatureManagement/JsonConverters/NewtonsoftStringValueTypeJsonConverter.cs +++ /dev/null @@ -1,95 +0,0 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using Microsoft.Extensions.Options; -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using Volo.Abp.DependencyInjection; -using Volo.Abp.Validation.StringValues; - -namespace Volo.Abp.FeatureManagement.JsonConverters; - -public class NewtonsoftStringValueTypeJsonConverter : JsonConverter, ITransientDependency -{ - public override bool CanWrite => false; - - protected readonly AbpFeatureManagementApplicationContractsOptions Options; - - public NewtonsoftStringValueTypeJsonConverter(IOptions options) - { - Options = options.Value; - } - - public override bool CanConvert(Type objectType) - { - return objectType == typeof(IStringValueType); - } - - public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) - { - throw new NotImplementedException("This method should not be called to write (since CanWrite is false)."); - } - - public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) - { - if (reader.TokenType != JsonToken.StartObject) - { - return null; - } - - var jsonObject = JObject.Load(reader); - - var stringValue = CreateStringValueTypeByName(jsonObject, jsonObject["name"].ToString()); - foreach (var o in serializer.Deserialize>( - new JsonTextReader(new StringReader(jsonObject["properties"].ToString())))) - { - stringValue[o.Key] = o.Value; - } - - stringValue.Validator = CreateValueValidatorByName(jsonObject["validator"], jsonObject["validator"]["name"].ToString()); - foreach (var o in serializer.Deserialize>( - new JsonTextReader(new StringReader(jsonObject["validator"]["properties"].ToString())))) - { - stringValue.Validator[o.Key] = o.Value; - } - - return stringValue; - } - - protected virtual IStringValueType CreateStringValueTypeByName(JObject jObject, string name) - { - if (name == "SelectionStringValueType") - { - var selectionStringValueType = new SelectionStringValueType(); - if (jObject["itemSource"].HasValues) - { - selectionStringValueType.ItemSource = new StaticSelectionStringValueItemSource(jObject["itemSource"]["items"] - .Select(item => new LocalizableSelectionStringValueItem() - { - Value = item["value"].ToString(), - DisplayText = new LocalizableStringInfo(item["displayText"]["resourceName"].ToString(), item["displayText"]["name"].ToString()) - }).ToArray()); - } - - return selectionStringValueType; - } - - return name switch - { - "FreeTextStringValueType" => new FreeTextStringValueType(), - "ToggleStringValueType" => new ToggleStringValueType(), - _ => throw new ArgumentException($"{nameof(IStringValueType)} named {name} was not found!") - }; - } - - protected virtual IValueValidator CreateValueValidatorByName(JToken jObject, string name) - { - foreach (var factory in Options.ValueValidatorFactory.Where(factory => factory.CanCreate(name))) - { - return factory.Create(); - } - - throw new ArgumentException($"{nameof(IValueValidator)} named {name} was cannot be created!"); - } -} diff --git a/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/NewtonsoftStringValueJsonConverter_Tests.cs b/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/NewtonsoftStringValueJsonConverter_Tests.cs deleted file mode 100644 index 4a8a1a36be..0000000000 --- a/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/NewtonsoftStringValueJsonConverter_Tests.cs +++ /dev/null @@ -1,16 +0,0 @@ -using Microsoft.Extensions.DependencyInjection; -using Volo.Abp.Json; -using Volo.Abp.Json.SystemTextJson; - -namespace Volo.Abp.FeatureManagement; - -public class NewtonsoftStringValueJsonConverter_Tests : StringValueJsonConverter_Tests -{ - protected override void AfterAddApplication(IServiceCollection services) - { - services.PreConfigure(options => - { - options.Providers.Remove(); - }); - } -} diff --git a/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs b/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs deleted file mode 100644 index 28b8e55052..0000000000 --- a/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/StringValueJsonConverter_Tests.cs +++ /dev/null @@ -1,122 +0,0 @@ -using System; -using System.Collections.Generic; -using Microsoft.Extensions.DependencyInjection; -using Shouldly; -using Volo.Abp.FeatureManagement.JsonConverters; -using Volo.Abp.Json; -using Volo.Abp.Validation.StringValues; -using Xunit; - -namespace Volo.Abp.FeatureManagement; - -public abstract class StringValueJsonConverter_Tests : FeatureManagementApplicationTestBase -{ - private readonly IJsonSerializer _jsonSerializer; - - public StringValueJsonConverter_Tests() - { - _jsonSerializer = GetRequiredService(); - } - - protected override void BeforeAddApplication(IServiceCollection services) - { - services.PreConfigure(options => - { - options.ValueValidatorFactory.Add(new ValueValidatorFactory("URL")); - }); - - base.BeforeAddApplication(services); - } - - [Fact] - public void Should_Serialize_And_Deserialize() - { - var featureListDto = new GetFeatureListResultDto - { - Groups = new List - { - new FeatureGroupDto - { - Name = "MyGroup", - DisplayName = "MyGroup", - Features = new List - { - new FeatureDto - { - ValueType = new FreeTextStringValueType - { - Validator = new BooleanValueValidator() - } - }, - new FeatureDto - { - ValueType = new SelectionStringValueType - { - ItemSource = new StaticSelectionStringValueItemSource( - new LocalizableSelectionStringValueItem - { - Value = "TestValue", - DisplayText = new LocalizableStringInfo("TestResourceName", "TestName") - }), - Validator = new AlwaysValidValueValidator() - } - }, - new FeatureDto - { - ValueType = new ToggleStringValueType - { - Validator = new NumericValueValidator - { - MaxValue = 1000, - MinValue = 10 - } - } - }, - new FeatureDto - { - Provider = new FeatureProviderDto - { - Name = "FeatureName", - Key = "FeatureKey" - } - }, - new FeatureDto - { - ValueType = new FreeTextStringValueType - { - Validator = new UrlValueValidator("https") - } - } - } - } - } - }; - - var serialized = _jsonSerializer.Serialize(featureListDto, indented: true); - - var featureListDto2 = _jsonSerializer.Deserialize(serialized); - - featureListDto2.ShouldNotBeNull(); - featureListDto2.Groups[0].Features[0].ValueType.ShouldBeOfType(); - featureListDto2.Groups[0].Features[0].ValueType.Validator.ShouldBeOfType(); - - featureListDto2.Groups[0].Features[1].ValueType.ShouldBeOfType(); - featureListDto2.Groups[0].Features[1].ValueType.Validator.ShouldBeOfType(); - featureListDto2.Groups[0].Features[1].ValueType.As().ItemSource.Items.ShouldBeOfType(); - featureListDto2.Groups[0].Features[1].ValueType.As().ItemSource.Items.ShouldContain(x => - x.Value == "TestValue" && x.DisplayText.ResourceName == "TestResourceName" && - x.DisplayText.Name == "TestName"); - - featureListDto2.Groups[0].Features[2].ValueType.ShouldBeOfType(); - featureListDto2.Groups[0].Features[2].ValueType.Validator.ShouldBeOfType(); - featureListDto2.Groups[0].Features[2].ValueType.Validator.As().MaxValue.ShouldBe(1000); - featureListDto2.Groups[0].Features[2].ValueType.Validator.As().MinValue.ShouldBe(10); - - featureListDto2.Groups[0].Features[3].Provider.Name.ShouldBe("FeatureName"); - featureListDto2.Groups[0].Features[3].Provider.Key.ShouldBe("FeatureKey"); - - featureListDto2.Groups[0].Features[4].ValueType.ShouldBeOfType(); - featureListDto2.Groups[0].Features[4].ValueType.Validator.ShouldBeOfType(); - featureListDto2.Groups[0].Features[4].ValueType.Validator.As().Scheme.ShouldBe("https"); - } -} diff --git a/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/SystemTextJsonStringValueJsonConverter_Tests.cs b/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/SystemTextJsonStringValueJsonConverter_Tests.cs index e51f9885ac..828d5b3814 100644 --- a/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/SystemTextJsonStringValueJsonConverter_Tests.cs +++ b/modules/feature-management/test/Volo.Abp.FeatureManagement.Application.Tests/Volo/Abp/FeatureManagement/SystemTextJsonStringValueJsonConverter_Tests.cs @@ -1,16 +1,122 @@ -using Microsoft.Extensions.DependencyInjection; +using System; +using System.Collections.Generic; +using Microsoft.Extensions.DependencyInjection; +using Shouldly; +using Volo.Abp.FeatureManagement.JsonConverters; using Volo.Abp.Json; -using Volo.Abp.Json.Newtonsoft; +using Volo.Abp.Validation.StringValues; +using Xunit; namespace Volo.Abp.FeatureManagement; -public class SystemTextJsonStringValueJsonConverter_Tests : StringValueJsonConverter_Tests +public class SystemTextJsonStringValueJsonConverter_Tests : FeatureManagementApplicationTestBase { - protected override void AfterAddApplication(IServiceCollection services) + private readonly IJsonSerializer _jsonSerializer; + + public SystemTextJsonStringValueJsonConverter_Tests() + { + _jsonSerializer = GetRequiredService(); + } + + protected override void BeforeAddApplication(IServiceCollection services) { - services.PreConfigure(options => + services.PreConfigure(options => { - options.Providers.Remove(); + options.ValueValidatorFactory.Add(new ValueValidatorFactory("URL")); }); + + base.BeforeAddApplication(services); + } + + [Fact] + public void Should_Serialize_And_Deserialize() + { + var featureListDto = new GetFeatureListResultDto + { + Groups = new List + { + new FeatureGroupDto + { + Name = "MyGroup", + DisplayName = "MyGroup", + Features = new List + { + new FeatureDto + { + ValueType = new FreeTextStringValueType + { + Validator = new BooleanValueValidator() + } + }, + new FeatureDto + { + ValueType = new SelectionStringValueType + { + ItemSource = new StaticSelectionStringValueItemSource( + new LocalizableSelectionStringValueItem + { + Value = "TestValue", + DisplayText = new LocalizableStringInfo("TestResourceName", "TestName") + }), + Validator = new AlwaysValidValueValidator() + } + }, + new FeatureDto + { + ValueType = new ToggleStringValueType + { + Validator = new NumericValueValidator + { + MaxValue = 1000, + MinValue = 10 + } + } + }, + new FeatureDto + { + Provider = new FeatureProviderDto + { + Name = "FeatureName", + Key = "FeatureKey" + } + }, + new FeatureDto + { + ValueType = new FreeTextStringValueType + { + Validator = new UrlValueValidator("https") + } + } + } + } + } + }; + + var serialized = _jsonSerializer.Serialize(featureListDto, indented: true); + + var featureListDto2 = _jsonSerializer.Deserialize(serialized); + + featureListDto2.ShouldNotBeNull(); + featureListDto2.Groups[0].Features[0].ValueType.ShouldBeOfType(); + featureListDto2.Groups[0].Features[0].ValueType.Validator.ShouldBeOfType(); + + featureListDto2.Groups[0].Features[1].ValueType.ShouldBeOfType(); + featureListDto2.Groups[0].Features[1].ValueType.Validator.ShouldBeOfType(); + featureListDto2.Groups[0].Features[1].ValueType.As().ItemSource.Items.ShouldBeOfType(); + featureListDto2.Groups[0].Features[1].ValueType.As().ItemSource.Items.ShouldContain(x => + x.Value == "TestValue" && x.DisplayText.ResourceName == "TestResourceName" && + x.DisplayText.Name == "TestName"); + + featureListDto2.Groups[0].Features[2].ValueType.ShouldBeOfType(); + featureListDto2.Groups[0].Features[2].ValueType.Validator.ShouldBeOfType(); + featureListDto2.Groups[0].Features[2].ValueType.Validator.As().MaxValue.ShouldBe(1000); + featureListDto2.Groups[0].Features[2].ValueType.Validator.As().MinValue.ShouldBe(10); + + featureListDto2.Groups[0].Features[3].Provider.Name.ShouldBe("FeatureName"); + featureListDto2.Groups[0].Features[3].Provider.Key.ShouldBe("FeatureKey"); + + featureListDto2.Groups[0].Features[4].ValueType.ShouldBeOfType(); + featureListDto2.Groups[0].Features[4].ValueType.Validator.ShouldBeOfType(); + featureListDto2.Groups[0].Features[4].ValueType.Validator.As().Scheme.ShouldBe("https"); } }