Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register JsonSerializerOptions in the IoC Container #15328

Merged
merged 15 commits into from
Feb 21, 2024
Merged
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.AdminMenu.Services;
using OrchardCore.Deployment;
using OrchardCore.Json;

namespace OrchardCore.AdminMenu.Deployment
{
Expand All @@ -14,15 +12,10 @@ public class AdminMenuDeploymentSource : IDeploymentSource
private readonly IAdminMenuService _adminMenuService;
private readonly JsonSerializerOptions _serializationOptions;

public AdminMenuDeploymentSource(IAdminMenuService adminMenuService, IOptions<JsonDerivedTypesOptions> derivedTypesOptions)
public AdminMenuDeploymentSource(IAdminMenuService adminMenuService, IOptions<JsonSerializerOptions> serializationOptions)
{
_adminMenuService = adminMenuService;

// The recipe step contains polymorphic types which need to be resolved
_serializationOptions = new()
{
TypeInfoResolver = new PolymorphicJsonTypeInfoResolver(derivedTypesOptions.Value)
};
_serializationOptions = serializationOptions.Value;
}

public async Task ProcessDeploymentStepAsync(DeploymentStep step, DeploymentPlanResult result)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.AdminMenu.Services;
using OrchardCore.Json;
using OrchardCore.Recipes.Models;
using OrchardCore.Recipes.Services;

Expand All @@ -20,15 +18,14 @@ public class AdminMenuStep : IRecipeStepHandler
private readonly IAdminMenuService _adminMenuService;
private readonly JsonSerializerOptions _serializationOptions;

public AdminMenuStep(IAdminMenuService adminMenuService, IOptions<JsonDerivedTypesOptions> derivedTypesOptions)
public AdminMenuStep(
IAdminMenuService adminMenuService,
IOptions<JsonSerializerOptions> serializationOptions)
{
_adminMenuService = adminMenuService;

// The recipe step contains polymorphic types (menu items) which need to be resolved
_serializationOptions = new()
{
TypeInfoResolver = new PolymorphicJsonTypeInfoResolver(derivedTypesOptions.Value)
};
_serializationOptions = serializationOptions.Value;
}

public async Task ExecuteAsync(RecipeExecutionContext context)
Expand All @@ -38,7 +35,7 @@ public async Task ExecuteAsync(RecipeExecutionContext context)
return;
}

var model = context.Step.ToObject<AdminMenuStepModel>();
var model = context.Step.ToObject<AdminMenuStepModel>(_serializationOptions);

foreach (var token in model.Data.Cast<JsonObject>())
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Settings;
using System.Threading.Tasks;
Expand Down Expand Up @@ -36,6 +37,7 @@ public class AutoroutePartHandler : ContentPartHandler<AutoroutePart>
private readonly ISiteService _siteService;
private readonly ITagCache _tagCache;
private readonly ISession _session;
private readonly JsonSerializerOptions _jsonSerializerOptions;
private readonly IServiceProvider _serviceProvider;
protected readonly IStringLocalizer S;

Expand All @@ -49,6 +51,7 @@ public AutoroutePartHandler(
ISiteService siteService,
ITagCache tagCache,
ISession session,
IOptions<JsonSerializerOptions> jsonSerializerOptions,
IServiceProvider serviceProvider,
IStringLocalizer<AutoroutePartHandler> stringLocalizer)
{
Expand All @@ -59,6 +62,7 @@ public AutoroutePartHandler(
_siteService = siteService;
_tagCache = tagCache;
_session = session;
_jsonSerializerOptions = jsonSerializerOptions.Value;
_serviceProvider = serviceProvider;
S = stringLocalizer;
}
Expand Down Expand Up @@ -202,7 +206,7 @@ private async Task CheckContainedHomeRouteAsync(string containerContentItemId, C

foreach (var jItem in jItems.Cast<JsonObject>())
{
var contentItem = jItem.ToObject<ContentItem>();
var contentItem = jItem.ToObject<ContentItem>(_jsonSerializerOptions);
var handlerAspect = await _contentManager.PopulateAspectAsync<RouteHandlerAspect>(contentItem);

if (!handlerAspect.Disabled)
Expand Down Expand Up @@ -250,7 +254,7 @@ private async Task PopulateContainedContentItemRoutesAsync(List<AutorouteEntry>

foreach (var jItem in jItems.Cast<JsonObject>())
{
var contentItem = jItem.ToObject<ContentItem>();
var contentItem = jItem.ToObject<ContentItem>(_jsonSerializerOptions);
var handlerAspect = await _contentManager.PopulateAspectAsync<RouteHandlerAspect>(contentItem);

if (!handlerAspect.Disabled)
Expand Down Expand Up @@ -282,7 +286,7 @@ private async Task ValidateContainedContentItemRoutesAsync(List<AutorouteEntry>

foreach (var jItem in jItems.Cast<JsonObject>())
{
var contentItem = jItem.ToObject<ContentItem>();
var contentItem = jItem.ToObject<ContentItem>(_jsonSerializerOptions);
var containedAutoroutePart = contentItem.As<AutoroutePart>();

// This is only relevant if the content items have an autoroute part as we adjust the part value as required to guarantee a unique route.
Expand Down
10 changes: 8 additions & 2 deletions src/OrchardCore.Modules/OrchardCore.Autoroute/Migrations.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.Autoroute.Core.Indexes;
using OrchardCore.Autoroute.Models;
using OrchardCore.ContentManagement.Metadata;
Expand All @@ -11,10 +13,14 @@ namespace OrchardCore.Autoroute
public class Migrations : DataMigration
{
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public Migrations(IContentDefinitionManager contentDefinitionManager)
public Migrations(
IContentDefinitionManager contentDefinitionManager,
IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_contentDefinitionManager = contentDefinitionManager;
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public async Task<int> CreateAsync()
Expand All @@ -40,7 +46,7 @@ await SchemaBuilder.CreateMapIndexTableAsync<AutoroutePartIndex>(table => table
// This code can be removed in a later version.
public async Task<int> UpdateFrom1Async()
{
await _contentDefinitionManager.MigratePartSettingsAsync<AutoroutePart, AutoroutePartSettings>();
await _contentDefinitionManager.MigratePartSettingsAsync<AutoroutePart, AutoroutePartSettings>(_jsonSerializerOptions);

return 2;
}
Expand Down
5 changes: 4 additions & 1 deletion src/OrchardCore.Modules/OrchardCore.Autoroute/Startup.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
using System;
using System.Text.Json;
using Fluid;
using Fluid.Values;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Routing;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using OrchardCore.Autoroute.Core.Indexes;
using OrchardCore.Autoroute.Core.Services;
using OrchardCore.Autoroute.Drivers;
Expand Down Expand Up @@ -59,7 +61,8 @@ public override void ConfigureServices(IServiceCollection services)

if (found)
{
return FluidValue.Create(await contentManager.GetAsync(entry.ContentItemId, entry.JsonPath), context.Options);
var options = context.Services.GetService<IOptions<JsonSerializerOptions>>();
return FluidValue.Create(await contentManager.GetAsync(entry.ContentItemId, entry.JsonPath, options.Value), context.Options);
}

return NilValue.Instance;
Expand Down
37 changes: 22 additions & 15 deletions src/OrchardCore.Modules/OrchardCore.ContentFields/Migrations.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Linq;
using System.Text.Json;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentFields.Settings;
using OrchardCore.ContentManagement.Metadata;
Expand All @@ -13,11 +15,16 @@ public class Migrations : DataMigration
{
private readonly IContentDefinitionManager _contentDefinitionManager;
private readonly ShellDescriptor _shellDescriptor;
private readonly JsonSerializerOptions _jsonSerializerOptions;

public Migrations(IContentDefinitionManager contentDefinitionManager, ShellDescriptor shellDescriptor)
public Migrations(
IContentDefinitionManager contentDefinitionManager,
ShellDescriptor shellDescriptor,
IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_contentDefinitionManager = contentDefinitionManager;
_shellDescriptor = shellDescriptor;
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

// New installations don't need to be upgraded, but because there is no initial migration record,
Expand All @@ -37,42 +44,42 @@ public async Task<int> CreateAsync()
private async Task UpgradeAsync()
{
// Boolean field
await _contentDefinitionManager.MigrateFieldSettingsAsync<BooleanField, BooleanFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<BooleanField, BooleanFieldSettings>(_jsonSerializerOptions);

// Content picker field
await _contentDefinitionManager.MigrateFieldSettingsAsync<ContentPickerField, ContentPickerFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<ContentPickerField, ContentPickerFieldSettings>(_jsonSerializerOptions);

// Date field
await _contentDefinitionManager.MigrateFieldSettingsAsync<DateField, DateFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<DateField, DateFieldSettings>(_jsonSerializerOptions);

// Date time field
await _contentDefinitionManager.MigrateFieldSettingsAsync<DateTimeField, DateTimeFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<DateTimeField, DateTimeFieldSettings>(_jsonSerializerOptions);

// Html field
await _contentDefinitionManager.MigrateFieldSettingsAsync<HtmlField, HtmlFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<HtmlField, HtmlFieldSettings>(_jsonSerializerOptions);

// Link field
await _contentDefinitionManager.MigrateFieldSettingsAsync<LinkField, LinkFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<LinkField, LinkFieldSettings>(_jsonSerializerOptions);

// Localization set content picker field
await _contentDefinitionManager.MigrateFieldSettingsAsync<LocalizationSetContentPickerField, LocalizationSetContentPickerFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<LocalizationSetContentPickerField, LocalizationSetContentPickerFieldSettings>(_jsonSerializerOptions);

// MultiText field
await _contentDefinitionManager.MigrateFieldSettingsAsync<MultiTextField, MultiTextFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<MultiTextField, MultiTextFieldSettings>(_jsonSerializerOptions);

// Numeric field
await _contentDefinitionManager.MigrateFieldSettingsAsync<NumericField, NumericFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<NumericField, NumericFieldSettings>(_jsonSerializerOptions);

// Text field
await _contentDefinitionManager.MigrateFieldSettingsAsync<TextField, TextFieldHeaderDisplaySettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<TextField, TextFieldPredefinedListEditorSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<TextField, TextFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<TextField, TextFieldHeaderDisplaySettings>(_jsonSerializerOptions);
await _contentDefinitionManager.MigrateFieldSettingsAsync<TextField, TextFieldPredefinedListEditorSettings>(_jsonSerializerOptions);
await _contentDefinitionManager.MigrateFieldSettingsAsync<TextField, TextFieldSettings>(_jsonSerializerOptions);

// Time field
await _contentDefinitionManager.MigrateFieldSettingsAsync<TimeField, TimeFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<TimeField, TimeFieldSettings>(_jsonSerializerOptions);

// YouTube field
await _contentDefinitionManager.MigrateFieldSettingsAsync<YoutubeField, YoutubeFieldSettings>();
await _contentDefinitionManager.MigrateFieldSettingsAsync<YoutubeField, YoutubeFieldSettings>(_jsonSerializerOptions);

// Keep in sync the upgrade process.
await UpdateFrom1Async();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
Expand All @@ -9,11 +11,18 @@ namespace OrchardCore.ContentFields.Settings
{
public class BooleanFieldSettingsDriver : ContentPartFieldDefinitionDisplayDriver<BooleanField>
{
private readonly JsonSerializerOptions _jsonSerializerOptions;

public BooleanFieldSettingsDriver(IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefinition)
{
return Initialize<BooleanFieldSettings>("BooleanFieldSettings_Edit", model =>
{
var settings = partFieldDefinition.Settings.ToObject<BooleanFieldSettings>();
var settings = partFieldDefinition.Settings.ToObject<BooleanFieldSettings>(_jsonSerializerOptions);

model.Hint = settings.Hint;
model.Label = settings.Label;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
Expand All @@ -9,11 +11,18 @@ namespace OrchardCore.ContentFields.Settings
{
public class DateFieldSettingsDriver : ContentPartFieldDefinitionDisplayDriver<DateField>
{
private readonly JsonSerializerOptions _jsonSerializerOptions;

public DateFieldSettingsDriver(IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefinition)
{
return Initialize<DateFieldSettings>("DateFieldSettings_Edit", model =>
{
var settings = partFieldDefinition.Settings.ToObject<DateFieldSettings>();
var settings = partFieldDefinition.Settings.ToObject<DateFieldSettings>(_jsonSerializerOptions);

model.Hint = settings.Hint;
model.Required = settings.Required;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
Expand All @@ -9,11 +11,18 @@ namespace OrchardCore.ContentFields.Settings
{
public class DateTimeFieldSettingsDriver : ContentPartFieldDefinitionDisplayDriver<DateTimeField>
{
private readonly JsonSerializerOptions _jsonSerializerOptions;

public DateTimeFieldSettingsDriver(IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefinition)
{
return Initialize<DateTimeFieldSettings>("DateTimeFieldSettings_Edit", model =>
{
var settings = partFieldDefinition.Settings.ToObject<DateTimeFieldSettings>();
var settings = partFieldDefinition.Settings.ToObject<DateTimeFieldSettings>(_jsonSerializerOptions);

model.Hint = settings.Hint;
model.Required = settings.Required;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Microsoft.Extensions.Options;
using OrchardCore.ContentFields.Fields;
using OrchardCore.ContentManagement.Metadata.Models;
using OrchardCore.ContentTypes.Editors;
Expand All @@ -9,11 +11,18 @@ namespace OrchardCore.ContentFields.Settings
{
public class LinkFieldSettingsDriver : ContentPartFieldDefinitionDisplayDriver<LinkField>
{
private readonly JsonSerializerOptions _jsonSerializerOptions;

public LinkFieldSettingsDriver(IOptions<JsonSerializerOptions> jsonSerializerOptions)
{
_jsonSerializerOptions = jsonSerializerOptions.Value;
}

public override IDisplayResult Edit(ContentPartFieldDefinition partFieldDefinition)
{
return Initialize<LinkFieldSettings>("LinkFieldSettings_Edit", model =>
{
var settings = partFieldDefinition.Settings.ToObject<LinkFieldSettings>();
var settings = partFieldDefinition.Settings.ToObject<LinkFieldSettings>(_jsonSerializerOptions);

model.Hint = settings.Hint;
model.HintLinkText = settings.HintLinkText;
Expand Down
Loading
Loading