Skip to content

Commit

Permalink
Fix workflow implementation in springboot failing if no default const…
Browse files Browse the repository at this point in the history
…ructor is present (temporalio#2300)
  • Loading branch information
Quinn-With-Two-Ns authored Oct 29, 2024
1 parent 2ded985 commit 0b192d3
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,10 +77,18 @@ public int hashCode() {

/**
* Create POJOWorkflowImplMetadata for a workflow implementation class. The object must implement
* at least one workflow method.
* at least one workflow method. Validates the implementation can be registered.
*/
public static POJOWorkflowImplMetadata newInstance(Class<?> implClass) {
return new POJOWorkflowImplMetadata(implClass, false);
return new POJOWorkflowImplMetadata(implClass, false, true);
}

/**
* Create POJOWorkflowImplMetadata for a workflow implementation class. The object must implement
* at least one workflow method. Does not validate the implementation can be registered.
*/
public static POJOWorkflowImplMetadata newInstanceForWorkflowFactory(Class<?> implClass) {
return new POJOWorkflowImplMetadata(implClass, false, false);
}

/**
Expand All @@ -89,10 +97,11 @@ public static POJOWorkflowImplMetadata newInstance(Class<?> implClass) {
* signal methods.
*/
public static POJOWorkflowImplMetadata newListenerInstance(Class<?> implClass) {
return new POJOWorkflowImplMetadata(implClass, true);
return new POJOWorkflowImplMetadata(implClass, true, false);
}

private POJOWorkflowImplMetadata(Class<?> implClass, boolean listener) {
private POJOWorkflowImplMetadata(
Class<?> implClass, boolean listener, boolean validateConstructor) {
if (implClass.isInterface()
|| implClass.isPrimitive()
|| implClass.isAnnotation()
Expand Down Expand Up @@ -166,7 +175,7 @@ private POJOWorkflowImplMetadata(Class<?> implClass, boolean listener) {
this.queryMethods = ImmutableList.copyOf(queryMethods.values());
this.updateMethods = ImmutableList.copyOf(updateMethods.values());
this.updateValidatorMethods = ImmutableList.copyOf(updateValidatorMethods.values());
if (!listener) {
if (!listener && validateConstructor) {
this.workflowInit =
ReflectionUtils.getConstructor(
implClass,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,23 @@ public void testWorkflowWithConstructor() {
Assert.assertNull(meta.getWorkflowInit());
}

@Test
public void testWorkflowWithConstructorArgsNoInit() {
try {
POJOWorkflowImplMetadata.newInstance(WorkflowWithConstructorParameters.class);
Assert.fail();
} catch (IllegalArgumentException e) {
assertTrue(
e.getMessage()
.contains(
"No default constructor or constructor annotated with @WorkflowInit found:"));
}
POJOWorkflowImplMetadata meta =
POJOWorkflowImplMetadata.newInstanceForWorkflowFactory(
WorkflowWithConstructorParameters.class);
Assert.assertEquals(1, meta.getWorkflowMethods().size());
}

public static class GImpl implements POJOWorkflowInterfaceMetadataTest.G {
@Override
public void g() {}
Expand Down Expand Up @@ -377,4 +394,13 @@ public WorkflowWithConstructor() {}
@Override
public void i() {}
}

public static class WorkflowWithConstructorParameters
implements POJOWorkflowInterfaceMetadataTest.I {

public WorkflowWithConstructorParameters(Integer i) {}

@Override
public void i() {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,8 @@ private void configureWorkflowImplementationAutoDiscovery(
@SuppressWarnings("unchecked")
private <T> void configureWorkflowImplementation(Worker worker, Class<?> clazz) {

POJOWorkflowImplMetadata workflowMetadata = POJOWorkflowImplMetadata.newInstance(clazz);
POJOWorkflowImplMetadata workflowMetadata =
POJOWorkflowImplMetadata.newInstanceForWorkflowFactory(clazz);
List<POJOWorkflowMethodMetadata> workflowMethods = workflowMetadata.getWorkflowMethods();
if (workflowMethods.isEmpty()) {
throw new BeanDefinitionValidationException(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,15 @@
import io.temporal.workflow.NexusServiceOptions;
import io.temporal.workflow.Workflow;
import java.time.Duration;
import org.springframework.context.ConfigurableApplicationContext;

@WorkflowImpl(workers = "mainWorker")
public class TestWorkflowImpl implements TestWorkflow {

// Test auto-wiring of the application context works, this is not indicative of a real-world use
// case as the workflow implementation should be stateless.
public TestWorkflowImpl(ConfigurableApplicationContext applicationContext) {}

@Override
public String execute(String input) {
if (input.equals("nexus")) {
Expand Down

0 comments on commit 0b192d3

Please sign in to comment.