From 9802e0cda055e1e3e0ed48de5ebca9f53127d079 Mon Sep 17 00:00:00 2001 From: Quinn Klassen Date: Wed, 13 Nov 2024 10:35:47 -0800 Subject: [PATCH] Validate with generic parameter types for workflow init --- .../internal/common/env/ReflectionUtils.java | 3 +- .../POJOWorkflowImplMetadataTest.java | 39 +++++++++++++++++++ .../POJOWorkflowInterfaceMetadataTest.java | 7 ++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/temporal-sdk/src/main/java/io/temporal/internal/common/env/ReflectionUtils.java b/temporal-sdk/src/main/java/io/temporal/internal/common/env/ReflectionUtils.java index 4265ce419..b3851cf1c 100644 --- a/temporal-sdk/src/main/java/io/temporal/internal/common/env/ReflectionUtils.java +++ b/temporal-sdk/src/main/java/io/temporal/internal/common/env/ReflectionUtils.java @@ -58,7 +58,8 @@ public static Optional> getWorkflowInitConstructor( throw new IllegalArgumentException( "Constructor with @WorkflowInit annotation must be public: " + clazz.getName()); } - if (!Arrays.equals(ctor.getParameterTypes(), workflowMethod.get(0).getParameterTypes())) { + if (!Arrays.equals( + ctor.getGenericParameterTypes(), workflowMethod.get(0).getGenericParameterTypes())) { throw new IllegalArgumentException( "Constructor annotated with @WorkflowInit must have the same parameters as the workflow method: " + clazz.getName()); diff --git a/temporal-sdk/src/test/java/io/temporal/common/metadata/POJOWorkflowImplMetadataTest.java b/temporal-sdk/src/test/java/io/temporal/common/metadata/POJOWorkflowImplMetadataTest.java index 23cddd030..fc3be7705 100644 --- a/temporal-sdk/src/test/java/io/temporal/common/metadata/POJOWorkflowImplMetadataTest.java +++ b/temporal-sdk/src/test/java/io/temporal/common/metadata/POJOWorkflowImplMetadataTest.java @@ -23,9 +23,11 @@ import static org.junit.Assert.*; import com.google.common.collect.ImmutableSet; +import io.temporal.common.converter.EncodedValuesTest; import io.temporal.workflow.WorkflowInit; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import org.junit.Assert; import org.junit.Test; @@ -229,6 +231,26 @@ public void testWorkflowWithConstructor() { Assert.assertNull(meta.getWorkflowInit()); } + @Test + public void testWorkflowWithGenericInput() { + POJOWorkflowImplMetadata meta = + POJOWorkflowImplMetadata.newInstance(WorkflowWithGenericInput.class); + Assert.assertNotNull(meta.getWorkflowInit()); + } + + @Test + public void testWorkflowWithGenericInputMismatchedInit() { + try { + POJOWorkflowImplMetadata.newInstance(WorkflowWithGenericInputMismatchedInit.class); + Assert.fail(); + } catch (IllegalArgumentException e) { + assertTrue( + e.getMessage() + .contains( + "Constructor annotated with @WorkflowInit must have the same parameters as the workflow method:")); + } + } + @Test public void testWorkflowWithConstructorArgsNoInit() { try { @@ -403,4 +425,21 @@ public WorkflowWithConstructorParameters(Integer i) {} @Override public void i() {} } + + public static class WorkflowWithGenericInput implements POJOWorkflowInterfaceMetadataTest.K { + @WorkflowInit + public WorkflowWithGenericInput(Map input) {} + + @Override + public void f(Map input) {} + } + + public static class WorkflowWithGenericInputMismatchedInit + implements POJOWorkflowInterfaceMetadataTest.K { + @WorkflowInit + public WorkflowWithGenericInputMismatchedInit(Map input) {} + + @Override + public void f(Map input) {} + } } diff --git a/temporal-sdk/src/test/java/io/temporal/common/metadata/POJOWorkflowInterfaceMetadataTest.java b/temporal-sdk/src/test/java/io/temporal/common/metadata/POJOWorkflowInterfaceMetadataTest.java index 6e9e68aac..467aa684c 100644 --- a/temporal-sdk/src/test/java/io/temporal/common/metadata/POJOWorkflowInterfaceMetadataTest.java +++ b/temporal-sdk/src/test/java/io/temporal/common/metadata/POJOWorkflowInterfaceMetadataTest.java @@ -22,6 +22,7 @@ import static org.junit.Assert.*; +import io.temporal.common.converter.EncodedValuesTest; import io.temporal.common.metadata.testclasses.WorkflowInterfaceWithOneWorkflowMethod; import io.temporal.worker.Worker; import io.temporal.workflow.*; @@ -271,6 +272,12 @@ public interface I { void i(); } + @WorkflowInterface + public interface K { + @WorkflowMethod + void f(Map input); + } + public interface DE extends D, E {} @WorkflowInterface