Skip to content

Commit 4cee4e0

Browse files
Validate with generic parameter types for workflow init (#2318)
1 parent 7ab0f6c commit 4cee4e0

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

temporal-sdk/src/main/java/io/temporal/internal/common/env/ReflectionUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public static Optional<Constructor<?>> getWorkflowInitConstructor(
5858
throw new IllegalArgumentException(
5959
"Constructor with @WorkflowInit annotation must be public: " + clazz.getName());
6060
}
61-
if (!Arrays.equals(ctor.getParameterTypes(), workflowMethod.get(0).getParameterTypes())) {
61+
if (!Arrays.equals(
62+
ctor.getGenericParameterTypes(), workflowMethod.get(0).getGenericParameterTypes())) {
6263
throw new IllegalArgumentException(
6364
"Constructor annotated with @WorkflowInit must have the same parameters as the workflow method: "
6465
+ clazz.getName());

temporal-sdk/src/test/java/io/temporal/common/metadata/POJOWorkflowImplMetadataTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,11 @@
2323
import static org.junit.Assert.*;
2424

2525
import com.google.common.collect.ImmutableSet;
26+
import io.temporal.common.converter.EncodedValuesTest;
2627
import io.temporal.workflow.WorkflowInit;
2728
import java.util.HashSet;
2829
import java.util.List;
30+
import java.util.Map;
2931
import java.util.Set;
3032
import org.junit.Assert;
3133
import org.junit.Test;
@@ -229,6 +231,26 @@ public void testWorkflowWithConstructor() {
229231
Assert.assertNull(meta.getWorkflowInit());
230232
}
231233

234+
@Test
235+
public void testWorkflowWithGenericInput() {
236+
POJOWorkflowImplMetadata meta =
237+
POJOWorkflowImplMetadata.newInstance(WorkflowWithGenericInput.class);
238+
Assert.assertNotNull(meta.getWorkflowInit());
239+
}
240+
241+
@Test
242+
public void testWorkflowWithGenericInputMismatchedInit() {
243+
try {
244+
POJOWorkflowImplMetadata.newInstance(WorkflowWithGenericInputMismatchedInit.class);
245+
Assert.fail();
246+
} catch (IllegalArgumentException e) {
247+
assertTrue(
248+
e.getMessage()
249+
.contains(
250+
"Constructor annotated with @WorkflowInit must have the same parameters as the workflow method:"));
251+
}
252+
}
253+
232254
@Test
233255
public void testWorkflowWithConstructorArgsNoInit() {
234256
try {
@@ -403,4 +425,21 @@ public WorkflowWithConstructorParameters(Integer i) {}
403425
@Override
404426
public void i() {}
405427
}
428+
429+
public static class WorkflowWithGenericInput implements POJOWorkflowInterfaceMetadataTest.K {
430+
@WorkflowInit
431+
public WorkflowWithGenericInput(Map<String, EncodedValuesTest.Pair> input) {}
432+
433+
@Override
434+
public void f(Map<String, EncodedValuesTest.Pair> input) {}
435+
}
436+
437+
public static class WorkflowWithGenericInputMismatchedInit
438+
implements POJOWorkflowInterfaceMetadataTest.K {
439+
@WorkflowInit
440+
public WorkflowWithGenericInputMismatchedInit(Map<String, Object> input) {}
441+
442+
@Override
443+
public void f(Map<String, EncodedValuesTest.Pair> input) {}
444+
}
406445
}

temporal-sdk/src/test/java/io/temporal/common/metadata/POJOWorkflowInterfaceMetadataTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import static org.junit.Assert.*;
2424

25+
import io.temporal.common.converter.EncodedValuesTest;
2526
import io.temporal.common.metadata.testclasses.WorkflowInterfaceWithOneWorkflowMethod;
2627
import io.temporal.worker.Worker;
2728
import io.temporal.workflow.*;
@@ -271,6 +272,12 @@ public interface I {
271272
void i();
272273
}
273274

275+
@WorkflowInterface
276+
public interface K {
277+
@WorkflowMethod
278+
void f(Map<String, EncodedValuesTest.Pair> input);
279+
}
280+
274281
public interface DE extends D, E {}
275282

276283
@WorkflowInterface

0 commit comments

Comments
 (0)