diff --git a/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/DeserializeSubclassFunction.java b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/DeserializeSubclassFunction.java new file mode 100644 index 0000000..36489f2 --- /dev/null +++ b/inngest-spring-boot-demo/src/main/java/com/inngest/springbootdemo/testfunctions/DeserializeSubclassFunction.java @@ -0,0 +1,49 @@ +package com.inngest.springbootdemo.testfunctions; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.inngest.FunctionContext; +import com.inngest.InngestFunction; +import com.inngest.InngestFunctionConfigBuilder; +import com.inngest.Step; +import org.jetbrains.annotations.NotNull; + +class Dog { + @JsonProperty("legs") + public int legs; + + public Dog(@JsonProperty("legs") int legs) { + this.legs = legs; + } +} + +class Corgi extends Dog { + @JsonProperty("stumpy") + public boolean stumpy; + + public Corgi(@JsonProperty("legs") int legs, @JsonProperty("stumpy") boolean stumpy) { + super(legs); + + this.stumpy = stumpy; + } +} + +public class DeserializeSubclassFunction extends InngestFunction { + @NotNull + @Override + public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { + return builder + .id("DeserializeSubclassFunction") + .name("Deserialize subclass function") + .triggerEvent("test/deserialize.subclass") + .retries(0); + } + + @Override + public String execute(FunctionContext ctx, Step step) { + Dog corgi = step.run("get-corgi", () -> new Corgi(4, true), Dog.class); + + assert(((Corgi) corgi).stumpy == true); + + return "Successfully cast Corgi"; + } +} diff --git a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java index c4cf95f..795b9eb 100644 --- a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java +++ b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DemoTestConfiguration.java @@ -22,6 +22,7 @@ protected HashMap functions() { addInngestFunction(functions, new RetriableErrorFunction()); addInngestFunction(functions, new ZeroRetriesFunction()); addInngestFunction(functions, new InvokeFailureFunction()); + addInngestFunction(functions, new DeserializeSubclassFunction()); addInngestFunction(functions, new TryCatchGenericExceptionFunction()); addInngestFunction(functions, new TryCatchRunFunction()); addInngestFunction(functions, new ThrottledFunction()); diff --git a/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DeserializationIntegrationTest.java b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DeserializationIntegrationTest.java new file mode 100644 index 0000000..5fa6f83 --- /dev/null +++ b/inngest-spring-boot-demo/src/test/java/com/inngest/springbootdemo/DeserializationIntegrationTest.java @@ -0,0 +1,42 @@ +package com.inngest.springbootdemo; + +import com.inngest.Inngest; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.parallel.Execution; +import org.junit.jupiter.api.parallel.ExecutionMode; +import org.springframework.beans.factory.annotation.Autowired; + +import java.util.LinkedHashMap; + +import static org.junit.jupiter.api.Assertions.*; + +@IntegrationTest +@Execution(ExecutionMode.CONCURRENT) +class DeserializationIntegrationTest { + @Autowired + private DevServerComponent devServer; + + static int sleepTime = 5000; + + @Autowired + private Inngest client; + + @Test + void testShouldDeserializeSubclassCorrectly() throws Exception { + String eventId = InngestFunctionTestHelpers.sendEvent(client, "test/deserialize.subclass").getIds()[0]; + + Thread.sleep(sleepTime); + + RunEntry run = devServer.runsByEvent(eventId).first(); + Object output = run.getOutput(); + if (output instanceof LinkedHashMap) { + fail("Run threw an exception serialized into a LinkedHashMap:" + output); + } + String outputString = (String) output; + + assertEquals("Completed", run.getStatus() ); + assertNotNull(run.getEnded_at()); + + assertEquals("Successfully cast Corgi", outputString); + } +}