Skip to content

Commit

Permalink
Refactor code for JSON object handling and workflow context
Browse files Browse the repository at this point in the history
This commit includes a significant rework of JSON object handling by ensuring produced JsonObject remains as JsonObject. Previously unimplemented customer and order workflow context providers have been removed. The test cases have been renamed and updated to confirm the new behavior is working as expected.
  • Loading branch information
sfmskywalker committed Dec 8, 2023
1 parent 0f57bd4 commit d154f8f
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 51 deletions.
3 changes: 0 additions & 3 deletions src/bundles/Elsa.WorkflowServer.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
using Elsa.MongoDb.Modules.Identity;
using Elsa.MongoDb.Modules.Management;
using Elsa.MongoDb.Modules.Runtime;
using Elsa.WorkflowServer.Web.WorkflowContexts;
using Microsoft.Data.Sqlite;
using Microsoft.Extensions.Options;
using Proto.Persistence.Sqlite;
Expand Down Expand Up @@ -216,8 +215,6 @@
#endif
});

services.AddWorkflowContextProvider<CustomerWorkflowContextProvider>();
services.AddWorkflowContextProvider<OrderWorkflowContextProvider>();
services.AddHealthChecks();
services.AddControllers();
services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyHeader().AllowAnyMethod().AllowAnyOrigin().WithExposedHeaders("*")));
Expand Down

This file was deleted.

This file was deleted.

9 changes: 6 additions & 3 deletions src/modules/Elsa.Expressions/Helpers/ObjectConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,12 @@ public static Result TryConvertTo(this object? value, Type targetType, ObjectCon

if (value is JsonObject jsonObject)
{
return underlyingTargetType == typeof(string)
? jsonObject.ToString()
: jsonObject.Deserialize(targetType, options);
return underlyingTargetType switch
{
{ } t when t == typeof(string) => jsonObject.ToString(),
{ } t when t != typeof(object) => jsonObject.Deserialize(targetType, options),
_ => jsonObject,
};
}

if (underlyingSourceType == typeof(string) && !underlyingTargetType.IsPrimitive && underlyingTargetType != typeof(object))
Expand Down
4 changes: 3 additions & 1 deletion src/modules/Elsa.Workflows.Core/Builders/WorkflowBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ public Variable<T> WithVariable<T>()
public Variable<T> WithVariable<T>(string name, T value)
{
var variable = WithVariable<T>();
variable.Name = name;
variable.Value = value;
return variable;
}

Expand Down Expand Up @@ -146,7 +148,7 @@ public async Task<Workflow> BuildWorkflowAsync(CancellationToken cancellationTok
workflow.ResultVariable = Result;
workflow.Result = new Output<object>(Result);
}

var graph = await _activityVisitor.VisitAsync(workflow, cancellationToken);
var nodes = graph.Flatten().ToList();

Expand Down
14 changes: 10 additions & 4 deletions src/modules/Elsa.Workflows.Core/Models/Input.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ protected Input(MemoryBlockReference memoryBlockReference, Type type) : base(mem
{
Type = type;
}

/// <inheritdoc />
protected Input(Expression? expression, MemoryBlockReference memoryBlockReference, Type type) : base(memoryBlockReference)
{
Expand All @@ -31,7 +31,8 @@ protected Input(Expression? expression, MemoryBlockReference memoryBlockReferenc
/// <summary>
/// Gets the type of the input.
/// </summary>
[JsonPropertyName("typeName")] public Type Type { get; set; }
[JsonPropertyName("typeName")]
public Type Type { get; set; }
}

/// <summary>
Expand All @@ -43,7 +44,7 @@ public class Input<T> : Input
public Input(MemoryBlockReference memoryBlockReference) : base(memoryBlockReference, typeof(T))
{
}

/// <inheritdoc />
public Input(T literal, string? id = default) : this(new Literal<T>(literal, id))
{
Expand Down Expand Up @@ -88,7 +89,7 @@ public Input(Literal<T> literal) : base(Expression.LiteralExpression(literal.Val
public Input(Literal literal) : base(Expression.LiteralExpression(literal.Value), literal, typeof(T))
{
}

/// <inheritdoc />
public Input(ObjectLiteral<T> literal) : base(Expression.LiteralExpression(literal.Value), literal, typeof(T))
{
Expand All @@ -103,4 +104,9 @@ public Input(ObjectLiteral literal) : base(Expression.LiteralExpression(literal.
public Input(Expression expression, MemoryBlockReference memoryBlockReference) : base(expression, memoryBlockReference, typeof(T))
{
}

/// <inheritdoc />
public Input(Expression expression) : this(expression, new MemoryBlockReference())
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
using Elsa.Extensions;
using Elsa.Workflows.Core;

namespace Elsa.IntegrationTests.Scenarios.JsonObjectJint.Activities;
namespace Elsa.IntegrationTests.Scenarios.JsonObjectToObjectRemainsJsonObject.Activities;

public class JsonObjectProducer : CodeActivity<JsonObject>
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using Elsa.Extensions;
using Elsa.Workflows.Core;
using Elsa.Workflows.Core.Models;

namespace Elsa.IntegrationTests.Scenarios.JsonObjectToObjectRemainsJsonObject.Activities;

public class ObjectProcessor : CodeActivity<object>
{
public Input<object> Value { get; set; } = default!;

protected override void Execute(ActivityExecutionContext context)
{
var value = Value.Get(context);
context.SetResult(value);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Elsa.IntegrationTests.Scenarios.JsonObjectJint.Workflows;
using Elsa.IntegrationTests.Scenarios.JsonObjectToObjectRemainsJsonObject.Workflows;
using Elsa.Testing.Shared;
using Elsa.Workflows.Core.Contracts;
using Microsoft.Extensions.DependencyInjection;
using Xunit;
using Xunit.Abstractions;

namespace Elsa.IntegrationTests.Scenarios.JsonObjectJint;
namespace Elsa.IntegrationTests.Scenarios.JsonObjectToObjectRemainsJsonObject;

public class JsonObjectJintTests
{
Expand All @@ -23,7 +23,7 @@ public JsonObjectJintTests(ITestOutputHelper testOutputHelper)
_workflowRunner = _services.GetRequiredService<IWorkflowRunner>();
}

[Fact(DisplayName = "Jint handles JsonObject as native JavaScript object")]
[Fact(DisplayName = "The produced JsonObject remains JsonObject and does not become JsonElement (which does not support index notation)")]
public async Task Test1()
{
await _services.PopulateRegistriesAsync();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using System.Text.Json.Nodes;
using Elsa.IntegrationTests.Scenarios.JsonObjectJint.Activities;
using Elsa.Expressions.Models;
using Elsa.IntegrationTests.Scenarios.JsonObjectToObjectRemainsJsonObject.Activities;
using Elsa.JavaScript.Models;
using Elsa.Workflows.Core;
using Elsa.Workflows.Core.Activities;
using Elsa.Workflows.Core.Contracts;

namespace Elsa.IntegrationTests.Scenarios.JsonObjectJint.Workflows;
namespace Elsa.IntegrationTests.Scenarios.JsonObjectToObjectRemainsJsonObject.Workflows;

public class TestWorkflow : WorkflowBase
{
Expand All @@ -21,6 +22,11 @@ protected override void Build(IWorkflowBuilder builder)
{
Result = new(item)
},
new ObjectProcessor
{
Value = new(JavaScriptExpression.Create("getItem()"), new MemoryBlockReference()),
Result = new(item)
},
new WriteLine(JavaScriptExpression.Create("getItem()['Foo']['Bar']"))
}
};
Expand Down

0 comments on commit d154f8f

Please sign in to comment.