diff --git a/common/src/main/java/com/netflix/conductor/common/metadata/workflow/WorkflowTask.java b/common/src/main/java/com/netflix/conductor/common/metadata/workflow/WorkflowTask.java index 2e42e7319..17ad257a4 100644 --- a/common/src/main/java/com/netflix/conductor/common/metadata/workflow/WorkflowTask.java +++ b/common/src/main/java/com/netflix/conductor/common/metadata/workflow/WorkflowTask.java @@ -530,9 +530,17 @@ public TaskDef getTaskDefinition() { } /** - * @param taskDefinition Task definition + * Sets the TaskDef for this instance. If the passed TaskDef does not have a name, this method + * will set it to the name of the current instance. + * + *

NOTE: This method mutates the passed TaskDef object. + * + * @param taskDefinition The TaskDef to set. It may be modified by this method. */ public void setTaskDefinition(TaskDef taskDefinition) { + if (taskDefinition != null && taskDefinition.getName() == null) { + taskDefinition.setName(this.name); + } this.taskDefinition = taskDefinition; } diff --git a/common/src/test/java/com/netflix/conductor/common/workflow/WorkflowTaskTest.java b/common/src/test/java/com/netflix/conductor/common/workflow/WorkflowTaskTest.java index 12a8aa399..ad0e03221 100644 --- a/common/src/test/java/com/netflix/conductor/common/workflow/WorkflowTaskTest.java +++ b/common/src/test/java/com/netflix/conductor/common/workflow/WorkflowTaskTest.java @@ -18,6 +18,7 @@ import org.junit.Test; +import com.netflix.conductor.common.metadata.tasks.TaskDef; import com.netflix.conductor.common.metadata.tasks.TaskType; import com.netflix.conductor.common.metadata.workflow.WorkflowTask; @@ -29,6 +30,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; public class WorkflowTaskTest { @@ -76,4 +78,24 @@ public void testWorkflowTaskName() { validationErrors.contains( "WorkflowTask taskReferenceName name cannot be empty or null")); } + + @Test + public void testSetTaskDefinition() { + WorkflowTask workflowTask = new WorkflowTask(); + TaskDef taskDef = new TaskDef(); + + // Case 1: taskDefinition is not null and taskDefinition.getName() is null + taskDef.setName(null); + workflowTask.setTaskDefinition(taskDef); + assertEquals(workflowTask.getName(), taskDef.getName()); + + // Case 2: taskDefinition is not null and taskDefinition.getName() is not null + taskDef.setName("existingName"); + workflowTask.setTaskDefinition(taskDef); + assertEquals("existingName", taskDef.getName()); + + // Case 3: taskDefinition is null + workflowTask.setTaskDefinition(null); + assertNull(workflowTask.getTaskDefinition()); + } }