Skip to content

Commit bc74e8d

Browse files
author
Matheus Andre
committed
fix: Exception on loop after agent call
Signed-off-by: Matheus Andre <matheusandr2@gmail>
1 parent e721128 commit bc74e8d

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

experimental/lambda/src/main/java/io/serverlessworkflow/impl/executors/func/JavaForExecutorBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected WorkflowValueResolver<Collection<?>> buildCollectionFilter() {
6969
}
7070

7171
private Object collectionFilterObject(ForTaskFunction taskFunctions) {
72-
return taskFunctions.getForClass().isPresent()
72+
return taskFunctions.getForClass() != null && taskFunctions.getForClass().isPresent()
7373
? new TypedFunction(
7474
taskFunctions.getCollection(), taskFunctions.getForClass().orElseThrow())
7575
: taskFunctions.getCollection();
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2020-Present The Serverless Workflow Specification Authors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package io.serverless.workflow.impl.executors.func;
17+
18+
import static org.assertj.core.api.Assertions.assertThat;
19+
20+
import io.serverlessworkflow.api.types.Workflow;
21+
import io.serverlessworkflow.api.types.func.ForTaskFunction;
22+
import io.serverlessworkflow.fluent.func.FuncWorkflowBuilder;
23+
import io.serverlessworkflow.impl.WorkflowApplication;
24+
import java.util.Collection;
25+
import java.util.List;
26+
import java.util.concurrent.ExecutionException;
27+
import org.junit.jupiter.api.Test;
28+
29+
class ForTaskFunctionRegressionTest {
30+
31+
@Test
32+
void initializesOptionalFieldsAsEmpty() {
33+
ForTaskFunction taskFunction = new ForTaskFunction();
34+
35+
assertThat(taskFunction.getWhileClass()).isNotNull().isEmpty();
36+
assertThat(taskFunction.getItemClass()).isNotNull().isEmpty();
37+
assertThat(taskFunction.getForClass()).isNotNull().isEmpty();
38+
}
39+
40+
@Test
41+
void forLoopWithoutExplicitCollectionClassExecutesSuccessfully()
42+
throws InterruptedException, ExecutionException {
43+
try (WorkflowApplication app = WorkflowApplication.builder().build()) {
44+
Workflow workflow =
45+
FuncWorkflowBuilder.workflow("loop-without-collection-class")
46+
.tasks(
47+
tasks ->
48+
tasks.forEach(
49+
f ->
50+
f.whileC(CallTest::isEven)
51+
.collection(v -> (Collection<?>) v)
52+
.tasks(CallTest::sum)))
53+
.build();
54+
55+
var result = app.workflowDefinition(workflow).instance(List.of(2, 4, 6)).start().get();
56+
57+
assertThat(result.asNumber().orElseThrow()).isEqualTo(12);
58+
}
59+
}
60+
}

experimental/types/src/main/java/io/serverlessworkflow/api/types/func/ForTaskFunction.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public class ForTaskFunction extends ForTask {
2424

2525
private static final long serialVersionUID = 1L;
2626
private LoopPredicateIndexFilter<?, ?> whilePredicate;
27-
private Optional<Class<?>> whileClass;
28-
private Optional<Class<?>> itemClass;
29-
private Optional<Class<?>> forClass;
27+
private Optional<Class<?>> whileClass = Optional.empty();
28+
private Optional<Class<?>> itemClass = Optional.empty();
29+
private Optional<Class<?>> forClass = Optional.empty();
3030
private Function<?, Collection<?>> collection;
3131

3232
public <T, V> ForTaskFunction withWhile(LoopPredicate<T, V> whilePredicate) {

0 commit comments

Comments
 (0)