Skip to content

Commit

Permalink
Refactor GetWorkflowDefinitionId in ActivityExtensions
Browse files Browse the repository at this point in the history
Expanded the GetWorkflowDefinitionId method in ActivityExtensions.cs to check for multiple properties before determining whether an activity is a workflow definition activity or not. This ensures a more rigorous validation process.
  • Loading branch information
sfmskywalker committed Jan 26, 2024
1 parent d693cb7 commit 15e5e58
Showing 1 changed file with 18 additions and 3 deletions.
21 changes: 18 additions & 3 deletions src/clients/Elsa.Api.Client/Extensions/ActivityExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -145,19 +145,34 @@ public static void SetDisplayText(this JsonObject activity, string? value)
/// Sets the root activity in the specified activity.
/// </summary>
public static void SetRoot(this JsonObject container, JsonObject root) => container.SetProperty(root, "root");

/// <summary>
/// Gets the root activity in the specified activity.
/// </summary>
public static string? GetWorkflowDefinitionId(this JsonObject activity) => activity.GetProperty<string?>("workflowDefinitionId");
public static string? GetWorkflowDefinitionId(this JsonObject activity)
{
// Determine if this activity represents a workflow definition activity based on the presence of the following properties.
var workflowDefinitionIdNode = activity.GetProperty("workflowDefinitionId");
var workflowDefinitionVersionId = activity.GetProperty("workflowDefinitionVersionId");
var latestAvailablePublishedVersion = activity.GetProperty("latestAvailablePublishedVersion");
var latestAvailablePublishedVersionId = activity.GetProperty("latestAvailablePublishedVersionId");

if (workflowDefinitionIdNode == null || workflowDefinitionVersionId == null || latestAvailablePublishedVersion == null || latestAvailablePublishedVersionId == null)
return null; // Not a workflow definition activity.

if (workflowDefinitionIdNode is JsonValue value)
return value.ToString();

return null; // Not a workflow definition activity.
}

/// <summary>
/// Determines whether the given activity is a workflow definition activity.
/// </summary>
/// <param name="activity">The JsonObject representing the activity.</param>
/// <returns>Returns true if the activity is a workflow definition activity; otherwise, false.</returns>
public static bool GetIsWorkflowDefinitionActivity(this JsonObject activity) => activity.ContainsKey("workflowDefinitionId") && activity.ContainsKey("workflowDefinitionVersionId");

/// <summary>
/// Gets a value indicating whether the specified activity can trigger the workflow.
/// </summary>
Expand Down

0 comments on commit 15e5e58

Please sign in to comment.