Skip to content

Commit

Permalink
Validate with generic parameter types for workflow init (#2318)
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns authored Nov 14, 2024
1 parent 7ab0f6c commit 4cee4e0
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public static Optional<Constructor<?>> 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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -403,4 +425,21 @@ public WorkflowWithConstructorParameters(Integer i) {}
@Override
public void i() {}
}

public static class WorkflowWithGenericInput implements POJOWorkflowInterfaceMetadataTest.K {
@WorkflowInit
public WorkflowWithGenericInput(Map<String, EncodedValuesTest.Pair> input) {}

@Override
public void f(Map<String, EncodedValuesTest.Pair> input) {}
}

public static class WorkflowWithGenericInputMismatchedInit
implements POJOWorkflowInterfaceMetadataTest.K {
@WorkflowInit
public WorkflowWithGenericInputMismatchedInit(Map<String, Object> input) {}

@Override
public void f(Map<String, EncodedValuesTest.Pair> input) {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -271,6 +272,12 @@ public interface I {
void i();
}

@WorkflowInterface
public interface K {
@WorkflowMethod
void f(Map<String, EncodedValuesTest.Pair> input);
}

public interface DE extends D, E {}

@WorkflowInterface
Expand Down

0 comments on commit 4cee4e0

Please sign in to comment.