From 1a0cd2f9181f3ddc1f0501396cf6f7456e04870c Mon Sep 17 00:00:00 2001 From: Mike Alhayek Date: Fri, 8 Mar 2024 09:20:30 -0800 Subject: [PATCH] Use ContentSerializerJsonOptions to configure content-serializer (#15474) --- .../Deployment/AdminMenuDeploymentSource.cs | 6 ++-- .../Recipes/AdminMenuStep.cs | 5 +-- .../DeploymentPlanDeploymentSource.cs | 5 +-- .../Recipes/DeploymentPlansRecipeStep.cs | 5 +-- .../Deployment/AllLayersDeploymentSource.cs | 5 +-- .../OrchardCore.Layers/Recipes/LayerStep.cs | 5 +-- .../Deployment/PlacementsDeploymentSource.cs | 5 +-- .../Deployment/AllQueriesDeploymentSource.cs | 5 +-- .../OrchardCore.Queries/Recipes/QueryStep.cs | 5 +-- .../OrchardCore.Queries/Sql/SqlQuerySource.cs | 5 +-- .../AllWorkflowTypeDeploymentSource.cs | 5 +-- .../Recipes/WorkflowTypeStep.cs | 5 +-- .../Services/WorkflowManager.cs | 5 +-- .../Drivers/UserTaskEventContentDriver.cs | 5 +-- ...ntentSerializerJsonOptionsConfiguration.cs | 31 ++++++++++++++++++ .../JsonSerializerOptionsConfiguration.cs | 32 ------------------- .../Json/ContentSerializerJsonOptions.cs | 8 +++++ .../Json/JsonOptionsConfigurations.cs | 4 +-- .../DefaultJsonContentSerializer.cs | 5 +-- .../OrchardCoreBuilderExtensions.cs | 6 ++-- .../Extensions/ServiceCollectionExtensions.cs | 3 +- .../GraphQL/ContentItemsFieldTypeTests.cs | 17 +++++----- .../OrchardCore.Tests.csproj | 1 + .../Workflows/WorkflowManagerTests.cs | 8 +++-- 24 files changed, 106 insertions(+), 80 deletions(-) create mode 100644 src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs delete mode 100644 src/OrchardCore/OrchardCore.Abstractions/Extensions/JsonSerializerOptionsConfiguration.cs create mode 100644 src/OrchardCore/OrchardCore.Abstractions/Json/ContentSerializerJsonOptions.cs diff --git a/src/OrchardCore.Modules/OrchardCore.AdminMenu/Deployment/AdminMenuDeploymentSource.cs b/src/OrchardCore.Modules/OrchardCore.AdminMenu/Deployment/AdminMenuDeploymentSource.cs index 0cb65240c45..007e4797fd6 100644 --- a/src/OrchardCore.Modules/OrchardCore.AdminMenu/Deployment/AdminMenuDeploymentSource.cs +++ b/src/OrchardCore.Modules/OrchardCore.AdminMenu/Deployment/AdminMenuDeploymentSource.cs @@ -4,6 +4,7 @@ using Microsoft.Extensions.Options; using OrchardCore.AdminMenu.Services; using OrchardCore.Deployment; +using OrchardCore.Json; namespace OrchardCore.AdminMenu.Deployment { @@ -12,10 +13,11 @@ public class AdminMenuDeploymentSource : IDeploymentSource private readonly IAdminMenuService _adminMenuService; private readonly JsonSerializerOptions _serializationOptions; - public AdminMenuDeploymentSource(IAdminMenuService adminMenuService, IOptions serializationOptions) + public AdminMenuDeploymentSource(IAdminMenuService adminMenuService, + IOptions serializationOptions) { _adminMenuService = adminMenuService; - _serializationOptions = serializationOptions.Value; + _serializationOptions = serializationOptions.Value.SerializerOptions; } public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result) diff --git a/src/OrchardCore.Modules/OrchardCore.AdminMenu/Recipes/AdminMenuStep.cs b/src/OrchardCore.Modules/OrchardCore.AdminMenu/Recipes/AdminMenuStep.cs index dff8ef5d672..9f4faaf5286 100644 --- a/src/OrchardCore.Modules/OrchardCore.AdminMenu/Recipes/AdminMenuStep.cs +++ b/src/OrchardCore.Modules/OrchardCore.AdminMenu/Recipes/AdminMenuStep.cs @@ -5,6 +5,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Options; using OrchardCore.AdminMenu.Services; +using OrchardCore.Json; using OrchardCore.Recipes.Models; using OrchardCore.Recipes.Services; @@ -20,12 +21,12 @@ public class AdminMenuStep : IRecipeStepHandler public AdminMenuStep( IAdminMenuService adminMenuService, - IOptions serializationOptions) + IOptions serializationOptions) { _adminMenuService = adminMenuService; // The recipe step contains polymorphic types (menu items) which need to be resolved - _serializationOptions = serializationOptions.Value; + _serializationOptions = serializationOptions.Value.SerializerOptions; } public async Task ExecuteAsync(RecipeExecutionContext context) diff --git a/src/OrchardCore.Modules/OrchardCore.Deployment/Deployment/DeploymentPlanDeploymentSource.cs b/src/OrchardCore.Modules/OrchardCore.Deployment/Deployment/DeploymentPlanDeploymentSource.cs index 8f3c6f1b867..4e1c7450262 100644 --- a/src/OrchardCore.Modules/OrchardCore.Deployment/Deployment/DeploymentPlanDeploymentSource.cs +++ b/src/OrchardCore.Modules/OrchardCore.Deployment/Deployment/DeploymentPlanDeploymentSource.cs @@ -4,6 +4,7 @@ using System.Text.Json.Nodes; using System.Threading.Tasks; using Microsoft.Extensions.Options; +using OrchardCore.Json; namespace OrchardCore.Deployment.Deployment { @@ -16,11 +17,11 @@ public class DeploymentPlanDeploymentSource : IDeploymentSource public DeploymentPlanDeploymentSource( IDeploymentPlanService deploymentPlanService, IEnumerable deploymentStepFactories, - IOptions jsonSerializerOptions) + IOptions jsonSerializerOptions) { _deploymentPlanService = deploymentPlanService; _deploymentStepFactories = deploymentStepFactories; - _jsonSerializerOptions = jsonSerializerOptions.Value; + _jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions; } public async Task ProcessDeploymentStepAsync(DeploymentStep deploymentStep, DeploymentPlanResult result) diff --git a/src/OrchardCore.Modules/OrchardCore.Deployment/Recipes/DeploymentPlansRecipeStep.cs b/src/OrchardCore.Modules/OrchardCore.Deployment/Recipes/DeploymentPlansRecipeStep.cs index aa4b85650f4..fe4870facce 100644 --- a/src/OrchardCore.Modules/OrchardCore.Deployment/Recipes/DeploymentPlansRecipeStep.cs +++ b/src/OrchardCore.Modules/OrchardCore.Deployment/Recipes/DeploymentPlansRecipeStep.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Options; +using OrchardCore.Json; using OrchardCore.Recipes.Models; using OrchardCore.Recipes.Services; @@ -22,11 +23,11 @@ public class DeploymentPlansRecipeStep : IRecipeStepHandler public DeploymentPlansRecipeStep( IServiceProvider serviceProvider, - IOptions jsonSerializerOptions, + IOptions jsonSerializerOptions, IDeploymentPlanService deploymentPlanService) { _serviceProvider = serviceProvider; - _jsonSerializerOptions = jsonSerializerOptions.Value; + _jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions; _deploymentPlanService = deploymentPlanService; } diff --git a/src/OrchardCore.Modules/OrchardCore.Layers/Deployment/AllLayersDeploymentSource.cs b/src/OrchardCore.Modules/OrchardCore.Layers/Deployment/AllLayersDeploymentSource.cs index a1fb33a8497..07547cda658 100644 --- a/src/OrchardCore.Modules/OrchardCore.Layers/Deployment/AllLayersDeploymentSource.cs +++ b/src/OrchardCore.Modules/OrchardCore.Layers/Deployment/AllLayersDeploymentSource.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Options; using OrchardCore.Deployment; +using OrchardCore.Json; using OrchardCore.Layers.Models; using OrchardCore.Layers.Services; using OrchardCore.Settings; @@ -18,11 +19,11 @@ public class AllLayersDeploymentSource : IDeploymentSource public AllLayersDeploymentSource( ILayerService layerService, ISiteService siteService, - IOptions serializationOptions) + IOptions serializationOptions) { _layerService = layerService; _siteService = siteService; - _jsonSerializerOptions = serializationOptions.Value; + _jsonSerializerOptions = serializationOptions.Value.SerializerOptions; } public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result) diff --git a/src/OrchardCore.Modules/OrchardCore.Layers/Recipes/LayerStep.cs b/src/OrchardCore.Modules/OrchardCore.Layers/Recipes/LayerStep.cs index 43ec709329e..0e2b4732f46 100644 --- a/src/OrchardCore.Modules/OrchardCore.Layers/Recipes/LayerStep.cs +++ b/src/OrchardCore.Modules/OrchardCore.Layers/Recipes/LayerStep.cs @@ -5,6 +5,7 @@ using System.Text.Json.Nodes; using System.Threading.Tasks; using Microsoft.Extensions.Options; +using OrchardCore.Json; using OrchardCore.Layers.Models; using OrchardCore.Layers.Services; using OrchardCore.Recipes.Models; @@ -30,13 +31,13 @@ public LayerStep( IRuleMigrator ruleMigrator, IConditionIdGenerator conditionIdGenerator, IEnumerable factories, - IOptions serializationOptions) + IOptions serializationOptions) { _layerService = layerService; _ruleMigrator = ruleMigrator; _conditionIdGenerator = conditionIdGenerator; _factories = factories; - _serializationOptions = serializationOptions.Value; + _serializationOptions = serializationOptions.Value.SerializerOptions; } public async Task ExecuteAsync(RecipeExecutionContext context) diff --git a/src/OrchardCore.Modules/OrchardCore.Placements/Deployment/PlacementsDeploymentSource.cs b/src/OrchardCore.Modules/OrchardCore.Placements/Deployment/PlacementsDeploymentSource.cs index be178f4910b..426f0cf1dc7 100644 --- a/src/OrchardCore.Modules/OrchardCore.Placements/Deployment/PlacementsDeploymentSource.cs +++ b/src/OrchardCore.Modules/OrchardCore.Placements/Deployment/PlacementsDeploymentSource.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Options; using OrchardCore.Deployment; +using OrchardCore.Json; using OrchardCore.Placements.Services; namespace OrchardCore.Placements.Deployment @@ -14,10 +15,10 @@ public class PlacementsDeploymentSource : IDeploymentSource public PlacementsDeploymentSource( PlacementsManager placementsManager, - IOptions jsonSerializerOptions) + IOptions jsonSerializerOptions) { _placementsManager = placementsManager; - _jsonSerializerOptions = jsonSerializerOptions.Value; + _jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions; } public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result) diff --git a/src/OrchardCore.Modules/OrchardCore.Queries/Deployment/AllQueriesDeploymentSource.cs b/src/OrchardCore.Modules/OrchardCore.Queries/Deployment/AllQueriesDeploymentSource.cs index 7ecdc9e65a8..ab56875db17 100644 --- a/src/OrchardCore.Modules/OrchardCore.Queries/Deployment/AllQueriesDeploymentSource.cs +++ b/src/OrchardCore.Modules/OrchardCore.Queries/Deployment/AllQueriesDeploymentSource.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Options; using OrchardCore.Deployment; +using OrchardCore.Json; namespace OrchardCore.Queries.Deployment { @@ -13,10 +14,10 @@ public class AllQueriesDeploymentSource : IDeploymentSource public AllQueriesDeploymentSource( IQueryManager queryManager, - IOptions jsonSerializerOptions) + IOptions jsonSerializerOptions) { _queryManager = queryManager; - _jsonSerializerOptions = jsonSerializerOptions.Value; + _jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions; } public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result) diff --git a/src/OrchardCore.Modules/OrchardCore.Queries/Recipes/QueryStep.cs b/src/OrchardCore.Modules/OrchardCore.Queries/Recipes/QueryStep.cs index e46b2baaf17..026f9a88bbd 100644 --- a/src/OrchardCore.Modules/OrchardCore.Queries/Recipes/QueryStep.cs +++ b/src/OrchardCore.Modules/OrchardCore.Queries/Recipes/QueryStep.cs @@ -6,6 +6,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using OrchardCore.Json; using OrchardCore.Recipes.Models; using OrchardCore.Recipes.Services; @@ -24,12 +25,12 @@ public class QueryStep : IRecipeStepHandler public QueryStep( IQueryManager queryManager, IEnumerable querySources, - IOptions jsonSerializerOptions, + IOptions jsonSerializerOptions, ILogger logger) { _queryManager = queryManager; _querySources = querySources; - _jsonSerializerOptions = jsonSerializerOptions.Value; + _jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions; _logger = logger; } diff --git a/src/OrchardCore.Modules/OrchardCore.Queries/Sql/SqlQuerySource.cs b/src/OrchardCore.Modules/OrchardCore.Queries/Sql/SqlQuerySource.cs index dac4c8d7120..741b2cdc392 100644 --- a/src/OrchardCore.Modules/OrchardCore.Queries/Sql/SqlQuerySource.cs +++ b/src/OrchardCore.Modules/OrchardCore.Queries/Sql/SqlQuerySource.cs @@ -10,6 +10,7 @@ using Microsoft.Extensions.Options; using OrchardCore.ContentManagement; using OrchardCore.Data; +using OrchardCore.Json; using OrchardCore.Liquid; using YesSql; @@ -27,13 +28,13 @@ public SqlQuerySource( ILiquidTemplateManager liquidTemplateManager, IDbConnectionAccessor dbConnectionAccessor, ISession session, - IOptions jsonSerializerOptions, + IOptions jsonSerializerOptions, IOptions templateOptions) { _liquidTemplateManager = liquidTemplateManager; _dbConnectionAccessor = dbConnectionAccessor; _session = session; - _jsonSerializerOptions = jsonSerializerOptions.Value; + _jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions; _templateOptions = templateOptions.Value; } diff --git a/src/OrchardCore.Modules/OrchardCore.Workflows/Deployment/AllWorkflowTypeDeploymentSource.cs b/src/OrchardCore.Modules/OrchardCore.Workflows/Deployment/AllWorkflowTypeDeploymentSource.cs index c432d37790d..3b2ef65fdc9 100644 --- a/src/OrchardCore.Modules/OrchardCore.Workflows/Deployment/AllWorkflowTypeDeploymentSource.cs +++ b/src/OrchardCore.Modules/OrchardCore.Workflows/Deployment/AllWorkflowTypeDeploymentSource.cs @@ -3,6 +3,7 @@ using System.Threading.Tasks; using Microsoft.Extensions.Options; using OrchardCore.Deployment; +using OrchardCore.Json; using OrchardCore.Workflows.Services; namespace OrchardCore.Workflows.Deployment @@ -14,10 +15,10 @@ public class AllWorkflowTypeDeploymentSource : IDeploymentSource public AllWorkflowTypeDeploymentSource( IWorkflowTypeStore workflowTypeStore, - IOptions jsonSerializerOptions) + IOptions jsonSerializerOptions) { _workflowTypeStore = workflowTypeStore; - _jsonSerializerOptions = jsonSerializerOptions.Value; + _jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions; } public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result) diff --git a/src/OrchardCore.Modules/OrchardCore.Workflows/Recipes/WorkflowTypeStep.cs b/src/OrchardCore.Modules/OrchardCore.Workflows/Recipes/WorkflowTypeStep.cs index f67e199bcc0..7d0262b88ea 100644 --- a/src/OrchardCore.Modules/OrchardCore.Workflows/Recipes/WorkflowTypeStep.cs +++ b/src/OrchardCore.Modules/OrchardCore.Workflows/Recipes/WorkflowTypeStep.cs @@ -7,6 +7,7 @@ using Microsoft.AspNetCore.Mvc.Infrastructure; using Microsoft.AspNetCore.Mvc.Routing; using Microsoft.Extensions.Options; +using OrchardCore.Json; using OrchardCore.Recipes.Models; using OrchardCore.Recipes.Services; using OrchardCore.Workflows.Http.Activities; @@ -29,13 +30,13 @@ public WorkflowTypeStep(IWorkflowTypeStore workflowTypeStore, ISecurityTokenService securityTokenService, IActionContextAccessor actionContextAccessor, IUrlHelperFactory urlHelperFactory, - IOptions jsonSerializerOptions) + IOptions jsonSerializerOptions) { _workflowTypeStore = workflowTypeStore; _securityTokenService = securityTokenService; _actionContextAccessor = actionContextAccessor; _urlHelperFactory = urlHelperFactory; - _jsonSerializerOptions = jsonSerializerOptions.Value; + _jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions; } public async Task ExecuteAsync(RecipeExecutionContext context) diff --git a/src/OrchardCore.Modules/OrchardCore.Workflows/Services/WorkflowManager.cs b/src/OrchardCore.Modules/OrchardCore.Workflows/Services/WorkflowManager.cs index 2e691299e54..5a7671e4991 100644 --- a/src/OrchardCore.Modules/OrchardCore.Workflows/Services/WorkflowManager.cs +++ b/src/OrchardCore.Modules/OrchardCore.Workflows/Services/WorkflowManager.cs @@ -8,6 +8,7 @@ using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; +using OrchardCore.Json; using OrchardCore.Locking.Distributed; using OrchardCore.Modules; using OrchardCore.Workflows.Activities; @@ -50,7 +51,7 @@ public WorkflowManager ILogger logger, ILogger missingActivityLogger, IStringLocalizer missingActivityLocalizer, - IOptions jsonSerializerOptions, + IOptions jsonSerializerOptions, IClock clock) { _activityLibrary = activityLibrary; @@ -63,7 +64,7 @@ public WorkflowManager _logger = logger; _missingActivityLogger = missingActivityLogger; _missingActivityLocalizer = missingActivityLocalizer; - _jsonSerializerOptions = jsonSerializerOptions.Value; + _jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions; _clock = clock; } diff --git a/src/OrchardCore.Modules/OrchardCore.Workflows/UserTasks/Drivers/UserTaskEventContentDriver.cs b/src/OrchardCore.Modules/OrchardCore.Workflows/UserTasks/Drivers/UserTaskEventContentDriver.cs index 8c0c478c5eb..a4d7ff4795b 100644 --- a/src/OrchardCore.Modules/OrchardCore.Workflows/UserTasks/Drivers/UserTaskEventContentDriver.cs +++ b/src/OrchardCore.Modules/OrchardCore.Workflows/UserTasks/Drivers/UserTaskEventContentDriver.cs @@ -14,6 +14,7 @@ using OrchardCore.DisplayManagement.ModelBinding; using OrchardCore.DisplayManagement.Notify; using OrchardCore.DisplayManagement.Views; +using OrchardCore.Json; using OrchardCore.Workflows.Models; using OrchardCore.Workflows.Services; using OrchardCore.Workflows.UserTasks.Activities; @@ -38,7 +39,7 @@ public UserTaskEventContentDriver( IWorkflowManager workflowManager, INotifier notifier, IHtmlLocalizer localizer, - IOptions jsonSerializerOptions, + IOptions jsonSerializerOptions, IHttpContextAccessor httpContextAccessor) { _workflowStore = workflowStore; @@ -46,7 +47,7 @@ public UserTaskEventContentDriver( _workflowManager = workflowManager; _notifier = notifier; _httpContextAccessor = httpContextAccessor; - _jsonSerializerOptions = jsonSerializerOptions.Value; + _jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions; H = localizer; } diff --git a/src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs b/src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs new file mode 100644 index 00000000000..6fab879b35d --- /dev/null +++ b/src/OrchardCore/OrchardCore.Abstractions/Extensions/ContentSerializerJsonOptionsConfiguration.cs @@ -0,0 +1,31 @@ +using System.Text.Json; +using System.Text.Json.Serialization; +using Microsoft.Extensions.Options; +using OrchardCore.Json; +using OrchardCore.Json.Serialization; + +namespace OrchardCore.Extensions; + +public class ContentSerializerJsonOptionsConfiguration : IConfigureOptions +{ + private readonly JsonDerivedTypesOptions _derivedTypesOptions; + + public ContentSerializerJsonOptionsConfiguration(IOptions derivedTypesOptions) + { + _derivedTypesOptions = derivedTypesOptions.Value; + } + + public void Configure(ContentSerializerJsonOptions options) + { + options.SerializerOptions.DefaultIgnoreCondition = JOptions.Base.DefaultIgnoreCondition; + options.SerializerOptions.ReferenceHandler = JOptions.Base.ReferenceHandler; + options.SerializerOptions.ReadCommentHandling = JOptions.Base.ReadCommentHandling; + options.SerializerOptions.PropertyNameCaseInsensitive = JOptions.Base.PropertyNameCaseInsensitive; + options.SerializerOptions.AllowTrailingCommas = JOptions.Base.AllowTrailingCommas; + options.SerializerOptions.WriteIndented = JOptions.Base.WriteIndented; + + options.SerializerOptions.TypeInfoResolverChain.Add(new PolymorphicJsonTypeInfoResolver(_derivedTypesOptions)); + options.SerializerOptions.Converters.Add(DynamicJsonConverter.Instance); + options.SerializerOptions.Converters.Add(PathStringJsonConverter.Instance); + } +} diff --git a/src/OrchardCore/OrchardCore.Abstractions/Extensions/JsonSerializerOptionsConfiguration.cs b/src/OrchardCore/OrchardCore.Abstractions/Extensions/JsonSerializerOptionsConfiguration.cs deleted file mode 100644 index 4461b6ba52b..00000000000 --- a/src/OrchardCore/OrchardCore.Abstractions/Extensions/JsonSerializerOptionsConfiguration.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System.Text.Json; -using System.Text.Json.Serialization; -using Microsoft.Extensions.Options; -using OrchardCore.Json; -using OrchardCore.Json.Serialization; - -namespace OrchardCore.Extensions; - -public class JsonSerializerOptionsConfiguration : IConfigureOptions -{ - private readonly JsonDerivedTypesOptions _derivedTypesOptions; - - public JsonSerializerOptionsConfiguration( - IOptions derivedTypesOptions) - { - _derivedTypesOptions = derivedTypesOptions.Value; - } - - public void Configure(JsonSerializerOptions options) - { - options.DefaultIgnoreCondition = JOptions.Base.DefaultIgnoreCondition; - options.ReferenceHandler = JOptions.Base.ReferenceHandler; - options.ReadCommentHandling = JOptions.Base.ReadCommentHandling; - options.PropertyNameCaseInsensitive = JOptions.Base.PropertyNameCaseInsensitive; - options.AllowTrailingCommas = JOptions.Base.AllowTrailingCommas; - options.WriteIndented = JOptions.Base.WriteIndented; - - options.TypeInfoResolverChain.Add(new PolymorphicJsonTypeInfoResolver(_derivedTypesOptions)); - options.Converters.Add(DynamicJsonConverter.Instance); - options.Converters.Add(PathStringJsonConverter.Instance); - } -} diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/ContentSerializerJsonOptions.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/ContentSerializerJsonOptions.cs new file mode 100644 index 00000000000..18c1fe06852 --- /dev/null +++ b/src/OrchardCore/OrchardCore.Abstractions/Json/ContentSerializerJsonOptions.cs @@ -0,0 +1,8 @@ +using System.Text.Json; + +namespace OrchardCore.Json; + +public class ContentSerializerJsonOptions +{ + public JsonSerializerOptions SerializerOptions { get; } = new JsonSerializerOptions(); +} diff --git a/src/OrchardCore/OrchardCore.Abstractions/Json/JsonOptionsConfigurations.cs b/src/OrchardCore/OrchardCore.Abstractions/Json/JsonOptionsConfigurations.cs index 48ddd95f34d..eada357bec4 100644 --- a/src/OrchardCore/OrchardCore.Abstractions/Json/JsonOptionsConfigurations.cs +++ b/src/OrchardCore/OrchardCore.Abstractions/Json/JsonOptionsConfigurations.cs @@ -9,9 +9,9 @@ public class JsonOptionsConfigurations : IConfigureOptions { private readonly JsonSerializerOptions _jsonSerializerOptions; - public JsonOptionsConfigurations(IOptions jsonSerializerOptions) + public JsonOptionsConfigurations(IOptions jsonSerializerOptions) { - _jsonSerializerOptions = jsonSerializerOptions.Value; + _jsonSerializerOptions = jsonSerializerOptions.Value.SerializerOptions; } public void Configure(JsonOptions options) diff --git a/src/OrchardCore/OrchardCore.Data.YesSql/DefaultJsonContentSerializer.cs b/src/OrchardCore/OrchardCore.Data.YesSql/DefaultJsonContentSerializer.cs index d62507cb738..f4a28f1e19d 100644 --- a/src/OrchardCore/OrchardCore.Data.YesSql/DefaultJsonContentSerializer.cs +++ b/src/OrchardCore/OrchardCore.Data.YesSql/DefaultJsonContentSerializer.cs @@ -1,6 +1,7 @@ using System; using System.Text.Json; using Microsoft.Extensions.Options; +using OrchardCore.Json; namespace YesSql.Serialization { @@ -8,9 +9,9 @@ public class DefaultJsonContentSerializer : IContentSerializer { private readonly JsonSerializerOptions _options; - public DefaultJsonContentSerializer(IOptions options) + public DefaultJsonContentSerializer(IOptions options) { - _options = options.Value; + _options = options.Value.SerializerOptions; } public DefaultJsonContentSerializer(JsonSerializerOptions options) diff --git a/src/OrchardCore/OrchardCore.Data.YesSql/OrchardCoreBuilderExtensions.cs b/src/OrchardCore/OrchardCore.Data.YesSql/OrchardCoreBuilderExtensions.cs index d30376f643f..dbf6e55417b 100644 --- a/src/OrchardCore/OrchardCore.Data.YesSql/OrchardCoreBuilderExtensions.cs +++ b/src/OrchardCore/OrchardCore.Data.YesSql/OrchardCoreBuilderExtensions.cs @@ -3,7 +3,6 @@ using System.Data; using System.IO; using System.Linq; -using System.Text.Json; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using OrchardCore.Data; @@ -13,6 +12,7 @@ using OrchardCore.Environment.Shell; using OrchardCore.Environment.Shell.Removing; using OrchardCore.Environment.Shell.Scope; +using OrchardCore.Json; using OrchardCore.Modules; using YesSql; using YesSql.Indexes; @@ -180,7 +180,7 @@ private static YesSql.Configuration GetStoreConfiguration(IServiceProvider sp, Y var tableNameFactory = sp.GetRequiredService(); var loggerFactory = sp.GetRequiredService(); - var serializerOptions = sp.GetRequiredService>(); + var serializerOptions = sp.GetRequiredService>(); var storeConfiguration = new YesSql.Configuration { @@ -189,7 +189,7 @@ private static YesSql.Configuration GetStoreConfiguration(IServiceProvider sp, Y TableNameConvention = tableNameFactory.Create(databaseTableOptions), IdentityColumnSize = Enum.Parse(databaseTableOptions.IdentityColumnSize), Logger = loggerFactory.CreateLogger("YesSql"), - ContentSerializer = new DefaultJsonContentSerializer(serializerOptions.Value) + ContentSerializer = new DefaultJsonContentSerializer(serializerOptions.Value.SerializerOptions) }; if (yesSqlOptions.IdGenerator != null) diff --git a/src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs b/src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs index 7cf8b17a56c..8c0d269faca 100644 --- a/src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs +++ b/src/OrchardCore/OrchardCore/Modules/Extensions/ServiceCollectionExtensions.cs @@ -3,7 +3,6 @@ using System.IO; using System.Linq; using System.Net.Http; -using System.Text.Json; using Microsoft.AspNetCore.Authentication; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.DataProtection; @@ -157,7 +156,7 @@ private static void AddDefaultServices(OrchardCoreBuilder builder) services.AddSingleton(); services.AddTransient, JsonOptionsConfigurations>(); - services.AddTransient, JsonSerializerOptionsConfiguration>(); + services.AddTransient, ContentSerializerJsonOptionsConfiguration>(); services.AddScoped(); services.AddSingleton(); diff --git a/test/OrchardCore.Tests/Apis/GraphQL/ContentItemsFieldTypeTests.cs b/test/OrchardCore.Tests/Apis/GraphQL/ContentItemsFieldTypeTests.cs index 34fc2f52d9f..cede4e28d36 100644 --- a/test/OrchardCore.Tests/Apis/GraphQL/ContentItemsFieldTypeTests.cs +++ b/test/OrchardCore.Tests/Apis/GraphQL/ContentItemsFieldTypeTests.cs @@ -1,4 +1,3 @@ -using System.Text.Json; using System.Text.Json.Nodes; using GraphQL; using GraphQL.Execution; @@ -12,8 +11,8 @@ using OrchardCore.ContentManagement.Records; using OrchardCore.Data; using OrchardCore.Environment.Shell; +using OrchardCore.Extensions; using OrchardCore.Json; -using OrchardCore.Json.Serialization; using YesSql.Indexes; using YesSql.Provider.Sqlite; using YesSql.Serialization; @@ -39,14 +38,16 @@ public async Task InitializeAsync() _prefix = "tp"; _prefixedStore = await StoreFactory.CreateAndInitializeAsync(new Configuration().UseSqLite(string.Format(connectionStringTemplate, _tempFilename + _prefix)).SetTablePrefix(_prefix + "_")); - var jsonDerivedTypesOptions = new JsonDerivedTypesOptions(); + var derivedOptions = new Mock>(); + derivedOptions.Setup(x => x.Value) + .Returns(new JsonDerivedTypesOptions()); - var jsonOptions = new JsonSerializerOptions(JOptions.Base); - jsonOptions.TypeInfoResolverChain.Add(new System.Text.Json.Serialization.PolymorphicJsonTypeInfoResolver(jsonDerivedTypesOptions)); - jsonOptions.Converters.Add(DynamicJsonConverter.Instance); - jsonOptions.Converters.Add(PathStringJsonConverter.Instance); + var configuration = new ContentSerializerJsonOptionsConfiguration(derivedOptions.Object); - var jsonSerializerOptions = new Mock>(); + var jsonOptions = new ContentSerializerJsonOptions(); + configuration.Configure(jsonOptions); + + var jsonSerializerOptions = new Mock>(); jsonSerializerOptions.Setup(x => x.Value) .Returns(jsonOptions); diff --git a/test/OrchardCore.Tests/OrchardCore.Tests.csproj b/test/OrchardCore.Tests/OrchardCore.Tests.csproj index aaaf2f9d06f..946db171578 100644 --- a/test/OrchardCore.Tests/OrchardCore.Tests.csproj +++ b/test/OrchardCore.Tests/OrchardCore.Tests.csproj @@ -47,6 +47,7 @@ + diff --git a/test/OrchardCore.Tests/Workflows/WorkflowManagerTests.cs b/test/OrchardCore.Tests/Workflows/WorkflowManagerTests.cs index f379bbb2198..9d513cc2356 100644 --- a/test/OrchardCore.Tests/Workflows/WorkflowManagerTests.cs +++ b/test/OrchardCore.Tests/Workflows/WorkflowManagerTests.cs @@ -1,6 +1,6 @@ -using System.Text.Json; using System.Text.Json.Nodes; using OrchardCore.DisplayManagement; +using OrchardCore.Json; using OrchardCore.Locking.Distributed; using OrchardCore.Modules; using OrchardCore.Scripting; @@ -55,7 +55,7 @@ public async Task CanExecuteSimpleWorkflow() ] }; - var workflowManager = CreateWorkflowManager(serviceProvider, new IActivity[] { addTask, writeLineTask, setOutputTask }, workflowType); + var workflowManager = CreateWorkflowManager(serviceProvider, [addTask, writeLineTask, setOutputTask], workflowType); var a = 10d; var b = 22d; var expectedSum = a + b; @@ -114,7 +114,9 @@ WorkflowType workflowType var missingActivityLocalizer = new Mock>(); var clock = new Mock(); var workflowFaultHandler = new Mock(); - var jsonOptionsMock = new Mock>(); + var jsonOptionsMock = new Mock>(); + jsonOptionsMock.Setup(x => x.Value) + .Returns(new ContentSerializerJsonOptions()); var workflowManager = new WorkflowManager( activityLibrary.Object,