Update InputJsonConverter to deserialize complex value types into their original type (#4878)

* Remove unused using directives in test classes

The using directives for System, System.Collections.Generic, System.IO, and others were not necessary in many of the test classes. These unused directives have been removed to enhance code readability and maintainability.

* Update source port retrieval in ConnectionJsonConverter

The source port retrieval code in ConnectionJsonConverter.cs has been updated. It now uses TryGetProperty instead of GetProperty, enabling it to handle cases where the "port" property may not exist. This enhances error handling and resilience in the activities module.

* Refactor InputJsonConverter for proper variable expressions handling

The InputJsonConverter in the Elsa.Workflows.Serialization has been expanded for efficient handling of variable expressions during the JSON conversion process. The refactor ensures appropriate extraction and assignment of values to variables. This adds robustness to the serialization process, maintaining variable types after serialization.

* Add tests for variable expressions serialization

A new `Tests.cs` file has been added under the `Elsa.IntegrationTests/Serialization/VariableExpressions` directory. This file contains tests for ensuring the serialization of variable expressions works correctly. A corresponding `SampleWorkflow` file has also been established, providing the workflows to be used in the tests.

* Refactor workflow builder extension class

The WorkflowDefinitionBuilderExtensions class has been deleted and replaced with WorkflowBuilderExtensions in Elsa.Workflows.Core. This new class retains similar functionality but includes more detailed comments and dynamic member access capabilities in its method definition.
This commit is contained in:
Sipke Schoorstra 2024-02-05 21:39:49 +01:00 committed by GitHub
parent d511b04cee
commit fb2f0f0271
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
58 changed files with 109 additions and 147 deletions

View file

@ -23,7 +23,6 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "solution", "solution", "{7D
icon.png = icon.png
NuGet.Config = NuGet.Config
README.md = README.md
update-migrations.sh = update-migrations.sh
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "docs", "docs", "{0354F050-3992-4DD4-B0EE-5FBA04AC72B6}"

View file

@ -31,7 +31,7 @@ public class ConnectionJsonConverter : JsonConverter<Connection>
var targetElement = doc.RootElement.GetProperty("target");
var sourceId = sourceElement.GetProperty("activity").GetString()!;
var targetId = targetElement.TryGetProperty("activity", out var targetIdValue) ? targetIdValue.GetString() : default;
var sourcePort = sourceElement.GetProperty("port").GetString()!;
var sourcePort = sourceElement.TryGetProperty("port", out var sourcePortValue) ? sourcePortValue.GetString() : default;
var targetPort = targetElement.TryGetProperty("port", out var targetPortValue) ? targetPortValue.GetString() : default;
var sourceActivity = _activities.TryGetValue(sourceId, out var s) ? s : default!;

View file

@ -0,0 +1,26 @@
using System.Diagnostics.CodeAnalysis;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;
// ReSharper disable once CheckNamespace
namespace Elsa.Extensions;
/// <summary>
/// Contains extensions for <see cref="IWorkflowBuilder"/>.
/// </summary>
public static class WorkflowBuilderExtensions
{
/// <summary>
/// Builds a workflow asynchronously.
/// </summary>
/// <typeparam name="T">The type of the workflow.</typeparam>
/// <param name="builder">The <see cref="IWorkflowBuilder"/> instance to build the workflow.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>A task representing the asynchronous operation that returns the built <see cref="Workflow"/>.</returns>
public static Task<Workflow> BuildWorkflowAsync<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T>(
this IWorkflowBuilder builder,
CancellationToken cancellationToken = default) where T : IWorkflow
{
return builder.BuildWorkflowAsync(Activator.CreateInstance<T>(), cancellationToken);
}
}

View file

@ -1,10 +0,0 @@
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;
// ReSharper disable once CheckNamespace
namespace Elsa.Extensions;
public static class WorkflowDefinitionBuilderExtensions
{
public static Task<Workflow> BuildWorkflowAsync<T>(this IWorkflowBuilder builder, CancellationToken cancellationToken = default) where T : IWorkflow => builder.BuildWorkflowAsync(Activator.CreateInstance<T>(), cancellationToken);
}

View file

@ -37,10 +37,17 @@ public class InputJsonConverter<T> : JsonConverter<Input<T>>
var expressionElement = doc.RootElement.TryGetProperty("expression", out var expressionElementValue) ? expressionElementValue : default;
var expressionTypeNameElement = expressionElement.ValueKind != JsonValueKind.Undefined ? expressionElement.TryGetProperty("type", out var expressionTypeNameElementValue) ? expressionTypeNameElementValue : default : default;
var expressionTypeName = expressionTypeNameElement.ValueKind != JsonValueKind.Undefined ? expressionTypeNameElement.GetString() ?? "Literal" : default;
var expressionDescriptor = expressionTypeName != null ? _expressionDescriptorRegistry.Find(expressionTypeName) : default;
var expression = expressionElement.ValueKind == JsonValueKind.Object ? expressionElement.Deserialize<Expression>(options) : new Expression(expressionTypeName!, null);
var expressionTypeName = expressionTypeNameElement.ValueKind != JsonValueKind.Undefined ? expressionTypeNameElement.GetString() ?? "Literal" : "Literal";
var expressionDescriptor = _expressionDescriptorRegistry.Find(expressionTypeName);
var memoryBlockReference = expressionDescriptor?.MemoryBlockReferenceFactory();
var memoryBlockReferenceType = memoryBlockReference?.GetType();
var expressionValueElement = expressionElement.TryGetProperty("value", out var expressionElementValueValue) ? expressionElementValueValue : default;
var expressionValue = expressionValueElement.ValueKind == JsonValueKind.String
? expressionValueElement.GetString()
: expressionValueElement.ValueKind != JsonValueKind.Undefined && memoryBlockReferenceType != null
? expressionValueElement.Deserialize(memoryBlockReferenceType, options)!
: default;
var expression = new Expression(expressionTypeName, expressionValue);
if (memoryBlockReference == null)
return default!;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.IntegrationTests.Activities.Workflows;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;

View file

@ -1,4 +1,3 @@
using System.Collections.Generic;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows;
using Elsa.Workflows.Contracts;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
using Microsoft.Extensions.DependencyInjection;

View file

@ -1,4 +1,3 @@
using System.Collections.Generic;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;

View file

@ -1,4 +1,3 @@
using System;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
using Microsoft.Extensions.DependencyInjection;

View file

@ -1,4 +1,3 @@
using System;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
using Microsoft.Extensions.DependencyInjection;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;

View file

@ -1,5 +1,3 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Mediator.Contracts;
using Elsa.Workflows.Notifications;

View file

@ -1,4 +1,3 @@
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
using Elsa.Workflows.Notifications;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
using Microsoft.Extensions.DependencyInjection;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.IntegrationTests.Scenarios.BlockingAndBreaking.Workflows;
using Elsa.Testing.Shared;

View file

@ -1,4 +1,3 @@
using System;
using Elsa.Expressions.Models;
using Elsa.Extensions;
using Elsa.Workflows;

View file

@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Elsa.IntegrationTests.Scenarios.CanExecute.Activities;
using Elsa.IntegrationTests.Scenarios.CanExecute.Workflows;
using Elsa.Testing.Shared;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows;
using Xunit;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;

View file

@ -1,7 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Testing.Shared;
using Xunit;
using Xunit.Abstractions;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows;
using Xunit;

View file

@ -1,6 +1,4 @@
using System;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Testing.Shared;
using Elsa.Workflows;
using Xunit;
using Xunit.Abstractions;

View file

@ -1,4 +1,3 @@
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Workflows;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.IntegrationTests.Scenarios.FlowchartNextActivity.Workflows;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.IntegrationTests.Scenarios.ImplicitJoins.Workflows;
using Elsa.Testing.Shared;
using Elsa.Workflows;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.IntegrationTests.Scenarios.ImplicitJoins.Workflows;
using Elsa.Testing.Shared;
using Elsa.Workflows;

View file

@ -1,5 +1,3 @@
using System;
using System.Threading.Tasks;
using Elsa.Common.Models;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;

View file

@ -1,5 +1,3 @@
using System;
using System.Threading.Tasks;
using Elsa.Common.Models;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;

View file

@ -1,7 +1,4 @@
using System;
using Elsa.Testing.Shared;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Xunit;
using Xunit.Abstractions;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.IntegrationTests.Scenarios.Incidents.Statics;
using Elsa.IntegrationTests.Scenarios.Incidents.Workflows;
using Elsa.Testing.Shared;

View file

@ -1,4 +1,3 @@
using System;
using Elsa.Workflows.IncidentStrategies;
namespace Elsa.IntegrationTests.Scenarios.Incidents.Statics;

View file

@ -1,6 +1,4 @@
using System;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
using Elsa.Workflows.State;
using Microsoft.Extensions.DependencyInjection;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.IntegrationTests.Scenarios.JsonObjectToObjectRemainsJsonObject.Workflows;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;

View file

@ -1,6 +1,4 @@
using System;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Testing.Shared;
using Elsa.Workflows;
using Xunit;
using Xunit.Abstractions;

View file

@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Testing.Shared;
using Xunit;
using Xunit.Abstractions;

View file

@ -1,7 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Testing.Shared;
using Xunit;
using Xunit.Abstractions;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
using Microsoft.Extensions.DependencyInjection;

View file

@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.IntegrationTests.Scenarios.SetGetVariablesFromActivities.Workflows;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;

View file

@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Elsa.IntegrationTests.Scenarios.WorkflowCancellation.Workflows;
using Elsa.Mediator.HostedServices;
using Elsa.Mediator.Options;

View file

@ -1,7 +1,3 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.IntegrationTests.Scenarios.WorkflowCancellation.Workflows;
using Elsa.Mediator.HostedServices;

View file

@ -1,5 +1,3 @@
using System;
using System.Linq;
using Elsa.Scheduling.Activities;
using Elsa.Workflows;
using Elsa.Workflows.Activities;

View file

@ -1,6 +1,4 @@
using System;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Testing.Shared;
using Xunit;
using Xunit.Abstractions;

View file

@ -1,5 +1,3 @@
using System;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;

View file

@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Nodes;
using Elsa.Testing.Shared;

View file

@ -1,4 +1,3 @@
using System.Collections.Generic;
using Elsa.Extensions;
namespace Elsa.IntegrationTests.Serialization.Polymorphism;

View file

@ -1,6 +1,4 @@
using System.Collections.Generic;
using System.Dynamic;
using System.IO;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Text.Json.Serialization.Metadata;

View file

@ -1,7 +1,5 @@
using Elsa.Testing.Shared;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Text.Json.Nodes;
using Elsa.Workflows.Contracts;
using Xunit;

View file

@ -0,0 +1,40 @@
using Elsa.Extensions;
using Elsa.Testing.Shared;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Activities.Flowchart.Activities;
using Elsa.Workflows.Contracts;
using Elsa.Workflows.Management.Contracts;
using Elsa.Workflows.Memory;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Xunit.Abstractions;
namespace Elsa.IntegrationTests.Serialization.VariableExpressions;
/// <summary>
/// Contains tests for variable expressions serialization.
/// </summary>
public class Tests
{
private readonly IWorkflowSerializer _workflowSerializer;
private readonly IWorkflowBuilder _workflowBuilder;
public Tests(ITestOutputHelper testOutputHelper)
{
var serviceProvider = new TestApplicationBuilder(testOutputHelper).Build();
_workflowSerializer = serviceProvider.GetRequiredService<IWorkflowSerializer>();
IWorkflowBuilderFactory workflowBuilderFactory = serviceProvider.GetRequiredService<IWorkflowBuilderFactory>();
_workflowBuilder = workflowBuilderFactory.CreateBuilder();
}
[Fact(DisplayName = "Variable types remain intact after serialization")]
public async Task Test1()
{
var workflow = await _workflowBuilder.BuildWorkflowAsync<SampleWorkflow>();
var serialized = _workflowSerializer.Serialize(workflow);
var deserializedWorkflow = _workflowSerializer.Deserialize(serialized);
var rehydratedWriteLine = (WriteLine)((Flowchart)deserializedWorkflow.Root).Activities.ElementAt(0);
Assert.IsType<Variable<string>>(rehydratedWriteLine.Text.Expression!.Value);
}
}

View file

@ -0,0 +1,24 @@
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Activities.Flowchart.Activities;
using Elsa.Workflows.Contracts;
namespace Elsa.IntegrationTests.Serialization.VariableExpressions;
class SampleWorkflow : WorkflowBase
{
protected override void Build(IWorkflowBuilder workflow)
{
var variable1 = workflow.WithVariable<string>("Some Value");
var writeLine1 = new WriteLine(variable1);
workflow.Root = new Flowchart
{
Activities =
{
writeLine1
},
Start = writeLine1
};
}
}

View file

@ -1,4 +1,3 @@
using System.Collections.Generic;
using Elsa.Workflows.Memory;
namespace Elsa.IntegrationTests.Serialization.VariableTypes;

View file

@ -1,5 +1,4 @@
using System.Collections.ObjectModel;
using System.Linq;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
using Elsa.Workflows.Memory;