Skip to content

Commit 34b5102

Browse files
authored
Validate empty task list in DSL builder (#1594)
Throw IllegalStateException when tasks() is called but produces no tasks, catching the issue at build time instead of a runtime NoSuchElementException in TaskExecutorHelper. Signed-off-by: Matheus Cruz <matheuscruz.dev@gmail.com>
1 parent 90ead3c commit 34b5102

2 files changed

Lines changed: 17 additions & 1 deletion

File tree

fluent/spec/src/main/java/io/serverlessworkflow/fluent/spec/BaseWorkflowBuilder.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ private SELF appendDo(Consumer<DBuilder> configurer) {
136136
configurer.accept(doBuilder);
137137

138138
final List<TaskItem> newItems = doBuilder.build().getDo();
139-
if (newItems == null || newItems.isEmpty()) return self();
139+
if (newItems == null || newItems.isEmpty()) {
140+
throw new IllegalStateException(
141+
"Task list must contain at least one task. "
142+
+ "Use .tasks(d -> d.set(...)) or similar to define tasks.");
143+
}
140144

141145
final List<TaskItem> merged =
142146
new ArrayList<>(this.workflow.getDo() != null ? this.workflow.getDo() : List.of());

fluent/spec/src/test/java/io/serverlessworkflow/fluent/spec/WorkflowBuilderTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import static org.junit.jupiter.api.Assertions.assertEquals;
3434
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
3535
import static org.junit.jupiter.api.Assertions.assertNotNull;
36+
import static org.junit.jupiter.api.Assertions.assertThrows;
3637
import static org.junit.jupiter.api.Assertions.assertTrue;
3738

3839
import io.serverlessworkflow.api.types.AuthenticationPolicyUnion;
@@ -112,6 +113,17 @@ void testUseAuthenticationsBasic() {
112113
assertNotNull(union.getBasicAuthenticationPolicy(), "BasicAuthenticationPolicy should be set");
113114
}
114115

116+
@Test
117+
void testEmptyTasksThrows() {
118+
assertThrows(IllegalStateException.class, () -> WorkflowBuilder.workflow().tasks().build());
119+
}
120+
121+
@Test
122+
void testEmptyTasksConsumerThrows() {
123+
assertThrows(
124+
IllegalStateException.class, () -> WorkflowBuilder.workflow().tasks(d -> {}).build());
125+
}
126+
115127
@Test
116128
void testDoTaskSetAndForEach() {
117129
Workflow wf =

0 commit comments

Comments
 (0)