-
Notifications
You must be signed in to change notification settings - Fork 3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix serialization of subclasses #92
Merged
albertchae
merged 3 commits into
main
from
poc-UnrecognizedPropertyException-anyparentclass-java-class
Nov 11, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
...o/src/main/java/com/inngest/springbootdemo/testfunctions/DeserializeSubclassFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"; | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
.../main/java/com/inngest/springbootdemo/testfunctions/TryCatchGenericExceptionFunction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.inngest.springbootdemo.testfunctions; | ||
|
||
import com.inngest.*; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class TryCatchGenericExceptionFunction extends InngestFunction { | ||
@NotNull | ||
@Override | ||
public InngestFunctionConfigBuilder config(InngestFunctionConfigBuilder builder) { | ||
return builder | ||
.id("try-catch-deserialize-exception-function") | ||
.name("Try Catch Deserialize Exception Function") | ||
.triggerEvent("test/try.catch.deserialize.exception") | ||
.retries(0); | ||
} | ||
|
||
@Override | ||
public String execute(FunctionContext ctx, Step step) { | ||
try { | ||
step.run("fail-step", () -> { | ||
throw new CustomException("Something fatally went wrong"); | ||
}, String.class); | ||
} catch (Exception originalException) { | ||
Exception e = step.run("handle-error", () -> originalException, Exception.class); | ||
return e.getMessage(); | ||
} | ||
|
||
return "An error should have been thrown and this message should not be returned"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...ng-boot-demo/src/test/java/com/inngest/springbootdemo/DeserializationIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Object> 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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package com.inngest | |
|
||
import com.fasterxml.jackson.databind.JsonNode | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.databind.node.ObjectNode | ||
import java.security.MessageDigest | ||
|
||
class StateNotFound : Throwable("State not found for id") | ||
|
@@ -60,8 +61,7 @@ class State( | |
val stepResult = node.path("steps").get(hashedId) ?: throw StateNotFound() | ||
|
||
if (stepResult.has(fieldName)) { | ||
val dataNode = stepResult.get(fieldName) | ||
return mapper.treeToValue(dataNode, type) | ||
return deserializeStepData(stepResult.get(fieldName), type) | ||
} else if (stepResult.has("error")) { | ||
val error = mapper.treeToValue(stepResult.get("error"), StepError::class.java) | ||
throw error | ||
|
@@ -71,4 +71,19 @@ class State( | |
// TODO - Check the state is actually null | ||
return null | ||
} | ||
|
||
private fun <T> deserializeStepData( | ||
serializedStepData: JsonNode?, | ||
type: Class<T>, | ||
): T? { | ||
val mapper = ObjectMapper() | ||
if (serializedStepData == null || !serializedStepData.isObject || !serializedStepData.has("class")) { | ||
// null and primitives can be deserialized directly | ||
return mapper.treeToValue(serializedStepData, type) | ||
} | ||
|
||
val writeableJson = serializedStepData as ObjectNode | ||
val className = writeableJson.remove("class").asText() | ||
return mapper.treeToValue(writeableJson, Class.forName(className)) as T | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to figure out how to handle the case where this will fail because the class no longer exists (or renamed) or has been changed enough since serialization that this won't deserialize |
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
converting an object to a JSON string only to read that back in to get a JSON node is silly, but replacing this with
val writeableJson = mapper.convertValue(stepData, ObjectNode::class.java)
resulted in a bunch of test failures, so we decided to punt on figuring this out to a follow up
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good - I think I may have had code like this in an early PoC. There wasn't a great pattern for this in Java and the JSON libraries from what I could tell that didn't result in duplicate parsing/serialization work. I'm find with leaving this for a future improvement.