forked from elsa-workflows/elsa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix cyclic input argument evaluation (elsa-workflows#3755)
* Prevent stack overflow when evaluating input arguments * Shuffle test folders
- Loading branch information
1 parent
67efc51
commit 1e407a9
Showing
35 changed files
with
138 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
src/modules/Elsa.JavaScript/Handlers/WorkflowDefinitionActivityJavaScriptHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using Elsa.Expressions.Models; | ||
using Elsa.Expressions.Services; | ||
using Elsa.Extensions; | ||
using Elsa.JavaScript.Notifications; | ||
using Elsa.Mediator.Services; | ||
using Elsa.Workflows.Core.Models; | ||
using Elsa.Workflows.Management.Activities; | ||
using Elsa.Workflows.Management.Extensions; | ||
using Elsa.Workflows.Management.Services; | ||
using Humanizer; | ||
using JetBrains.Annotations; | ||
using Jint; | ||
|
||
namespace Elsa.JavaScript.Handlers; | ||
|
||
/// <summary> | ||
/// Configures the JavaScript engine with workflow input getters. | ||
/// </summary> | ||
[PublicAPI] | ||
public class WorkflowDefinitionActivityJavaScriptHandler : INotificationHandler<EvaluatingJavaScript> | ||
{ | ||
private readonly IActivityRegistry _activityRegistry; | ||
private readonly IExpressionEvaluator _expressionEvaluator; | ||
|
||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
public WorkflowDefinitionActivityJavaScriptHandler(IActivityRegistry activityRegistry, IExpressionEvaluator expressionEvaluator) | ||
{ | ||
_activityRegistry = activityRegistry; | ||
_expressionEvaluator = expressionEvaluator; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task HandleAsync(EvaluatingJavaScript notification, CancellationToken cancellationToken) | ||
{ | ||
var engine = notification.Engine; | ||
var context = notification.Context; | ||
|
||
// If we are already evaluating inputs, then we're in a circular evaluation loop. In this case, we should not attempt to evaluate the inputs. | ||
if(context.TransientProperties.TryGetValue("EvaluatingInputs", out var evaluatingInputs) && (bool)evaluatingInputs) | ||
return; | ||
|
||
// To prevent a circular evaluation loop, set a flag on the context to indicate that we're currently evaluating the inputs. | ||
context.TransientProperties["EvaluatingInputs"] = true; | ||
|
||
// Create input getters. | ||
await CreateInputAccessorsAsync(engine, context); | ||
} | ||
|
||
private async Task CreateInputAccessorsAsync(Engine engine, ExpressionExecutionContext context) | ||
{ | ||
var workflowDefinitionActivity = GetFirstWorkflowDefinitionActivity(context); | ||
|
||
if (workflowDefinitionActivity == null) | ||
return; | ||
|
||
var descriptor = _activityRegistry.Find(workflowDefinitionActivity.Type, workflowDefinitionActivity.Version)!; | ||
var inputDefinitions = descriptor.Inputs; | ||
|
||
foreach (var inputDefinition in inputDefinitions) | ||
{ | ||
var inputPascalName = inputDefinition.Name.Pascalize(); | ||
var input = workflowDefinitionActivity.SyntheticProperties.TryGetValue(inputDefinition.Name, out var inputValue) ? (Input?)inputValue : default; | ||
var evaluatedExpression = input != null ? await _expressionEvaluator.EvaluateAsync(input, context) : input; | ||
|
||
engine.SetValue($"get{inputPascalName}", (Func<object?>)(() => evaluatedExpression)); | ||
} | ||
} | ||
|
||
private static WorkflowDefinitionActivity? GetFirstWorkflowDefinitionActivity(ExpressionExecutionContext context) => | ||
context.GetActivityExecutionContext().GetFirstWorkflowDefinitionActivity(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Elsa.Expressions.Services; | ||
using Elsa.JavaScript.Handlers; | ||
using Elsa.Workflows.Management.Services; | ||
using Moq; | ||
using Xunit; | ||
|
||
namespace Elsa.JavaScript.UnitTests; | ||
|
||
public class WorkflowDefinitionActivityJavaScriptHandlerTests | ||
{ | ||
private readonly WorkflowDefinitionActivityJavaScriptHandler _handler; | ||
|
||
public WorkflowDefinitionActivityJavaScriptHandlerTests() | ||
{ | ||
var activityRegistryMock = new Mock<IActivityRegistry>(); | ||
var expressionEvaluatorMock = new Mock<IExpressionEvaluator>(); | ||
_handler = new WorkflowDefinitionActivityJavaScriptHandler(activityRegistryMock.Object, expressionEvaluatorMock.Object); | ||
} | ||
|
||
[Fact] | ||
public void Test1() | ||
{ | ||
|
||
} | ||
|
||
} |