Skip to content

Commit

Permalink
Update InputJsonConverter to deserialize complex value types into the…
Browse files Browse the repository at this point in the history
…ir original type (elsa-workflows#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.
  • Loading branch information
sfmskywalker committed Feb 5, 2024
1 parent d511b04 commit fb2f0f0
Show file tree
Hide file tree
Showing 58 changed files with 109 additions and 147 deletions.
1 change: 0 additions & 1 deletion Elsa.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public override Connection Read(ref Utf8JsonReader reader, Type typeToConvert, J
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!;
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,17 @@ public override Input<T> Read(ref Utf8JsonReader reader, Type typeToConvert, Jso

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!;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Mediator.Contracts;
using Elsa.Workflows.Notifications;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
using Elsa.Workflows.Notifications;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Elsa.Expressions.Models;
using Elsa.Extensions;
using Elsa.Workflows;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows;
using Xunit;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Threading.Tasks;
using Elsa.Extensions;
using Elsa.Workflows;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Threading.Tasks;
using Elsa.Common.Models;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Threading.Tasks;
using Elsa.Common.Models;
using Elsa.Testing.Shared;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System;
using Elsa.Workflows.IncidentStrategies;

namespace Elsa.IntegrationTests.Scenarios.Incidents.Statics;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Linq;
using Elsa.Scheduling.Activities;
using Elsa.Workflows;
using Elsa.Workflows.Activities;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Testing.Shared;
using Xunit;
using Xunit.Abstractions;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Threading.Tasks;
using Elsa.Testing.Shared;
using Elsa.Workflows.Activities;
using Elsa.Workflows.Contracts;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Collections.Generic;
using Elsa.Extensions;

namespace Elsa.IntegrationTests.Serialization.Polymorphism;
Expand Down
Loading

0 comments on commit fb2f0f0

Please sign in to comment.