Fix variable with generic type serialization

This commit is contained in:
Sipke Schoorstra 2023-03-09 11:02:07 +01:00
parent 93288fa557
commit c5f35c81fc
2 changed files with 36 additions and 3 deletions

View file

@ -0,0 +1,30 @@
using System.Text.Json;
using System.Text.Json.Serialization;
using Elsa.Expressions.Contracts;
using Elsa.Workflows.Core.Models;
using Microsoft.Extensions.Logging;
namespace Elsa.Workflows.Core.Serialization.Converters;
/// <summary>
/// Produces <see cref="VariableConverter"/> instances for <see cref="Variable"/> and <see cref="Variable{T}"/>.
/// </summary>
public class VariableConverterFactory : JsonConverterFactory
{
private readonly IWellKnownTypeRegistry _wellKnownTypeRegistry;
private readonly ILogger<VariableConverter> _logger;
/// <inheritdoc />
// ReSharper disable once ContextualLoggerProblem
public VariableConverterFactory(IWellKnownTypeRegistry wellKnownTypeRegistry, ILogger<VariableConverter> logger)
{
_wellKnownTypeRegistry = wellKnownTypeRegistry;
_logger = logger;
}
/// <inheritdoc />
public override bool CanConvert(Type typeToConvert) => typeof(Variable).IsAssignableFrom(typeToConvert);
/// <inheritdoc />
public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializerOptions options) => new VariableConverter(_wellKnownTypeRegistry, _logger);
}

View file

@ -2,7 +2,6 @@ using System.Text.Json;
using System.Text.Json.Serialization;
using Elsa.Workflows.Core.Contracts;
using Elsa.Workflows.Core.Serialization.Converters;
using Elsa.Workflows.Core.Services;
using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Workflows.Core.Serialization;
@ -14,14 +13,18 @@ public class CustomSerializationOptionConfigurator : ISerializationOptionsConfig
{
private readonly IServiceProvider _serviceProvider;
/// <summary>
/// Initializes a new instance of the <see cref="CustomSerializationOptionConfigurator"/> class.
/// </summary>
public CustomSerializationOptionConfigurator(IServiceProvider serviceProvider)
{
_serviceProvider = serviceProvider;
}
/// <inheritdoc />
public void Configure(JsonSerializerOptions options)
{
options.Converters.Add(Create<VariableConverter>());
options.Converters.Add(Create<VariableConverterFactory>());
}
private T Create<T>() => ActivatorUtilities.CreateInstance<T>(_serviceProvider);