Skip to content

Commit 0e4ef15

Browse files
authored
Support newer JSON history format (#2001)
1 parent b182d78 commit 0e4ef15

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

temporal-sdk/src/main/java/io/temporal/common/WorkflowExecutionHistory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ public static WorkflowExecutionHistory fromJson(String serialized, String workfl
8383
}
8484

8585
/**
86-
* @return full json that can be used for replay and which is compatible with tctl
86+
* @param prettyPrint Whether to pretty print the JSON.
87+
* @return Full json that can be used for replay.
8788
*/
8889
public String toJson(boolean prettyPrint) {
8990
return super.toJson(prettyPrint);

temporal-sdk/src/main/java/io/temporal/internal/common/HistoryJsonUtils.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,15 @@ public static String protoJsonToHistoryFormatJson(String protoJson) {
6565
}
6666

6767
public static String historyFormatJsonToProtoJson(String historyFormatJson) {
68-
return convertEnumValues(historyFormatJson, ProtoEnumNameUtils::simplifiedToUniqueName);
68+
return convertEnumValues(
69+
historyFormatJson,
70+
(enumName, prefix) -> {
71+
// Only convert if the enum name isn't already converted
72+
if (enumName.indexOf('_') >= 0) {
73+
return enumName;
74+
}
75+
return ProtoEnumNameUtils.simplifiedToUniqueName(enumName, prefix);
76+
});
6977
}
7078

7179
private static String convertEnumValues(

temporal-sdk/src/main/java/io/temporal/internal/common/WorkflowExecutionHistory.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,33 @@ private static void checkHistory(History history) {
8484
}
8585

8686
/**
87-
* @return full json that can be used for replay and which is compatible with tctl
87+
* @param prettyPrint Whether to pretty print the JSON.
88+
* @return Full json that can be used for replay.
8889
*/
8990
public String toJson(boolean prettyPrint) {
91+
return toJson(prettyPrint, false);
92+
}
93+
94+
/**
95+
* @param prettyPrint Whether to pretty print the JSON.
96+
* @param legacyFormat If true, will use the older-style protobuf enum formatting as done by
97+
* Temporal tooling in the past. This is not recommended.
98+
* @return Full JSON that can be used for replay.
99+
*/
100+
public String toJson(boolean prettyPrint, boolean legacyFormat) {
90101
JsonFormat.Printer printer = JsonFormat.printer();
91102
try {
92103
String protoJson = printer.print(history);
93-
String historyFormatJson = HistoryJsonUtils.protoJsonToHistoryFormatJson(protoJson);
104+
if (legacyFormat) {
105+
protoJson = HistoryJsonUtils.protoJsonToHistoryFormatJson(protoJson);
106+
}
94107

95108
if (prettyPrint) {
96109
@SuppressWarnings("deprecation")
97-
JsonElement je = GSON_PARSER.parse(historyFormatJson);
110+
JsonElement je = GSON_PARSER.parse(protoJson);
98111
return GSON_PRETTY_PRINTER.toJson(je);
99112
} else {
100-
return historyFormatJson;
113+
return protoJson;
101114
}
102115
} catch (InvalidProtocolBufferException e) {
103116
throw new DataConverterException(e);

temporal-sdk/src/test/java/io/temporal/internal/common/WorkflowExecutionHistoryTest.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
package io.temporal.internal.common;
2222

2323
import static java.nio.charset.StandardCharsets.UTF_8;
24-
import static org.junit.Assert.assertEquals;
24+
import static org.junit.Assert.*;
2525

2626
import com.google.common.io.CharStreams;
2727
import io.temporal.common.WorkflowExecutionHistory;
@@ -62,6 +62,7 @@ public void deserializeAndSerializeBackComplexHistory() throws IOException {
6262
}
6363

6464
public void deserializeAndSerializeBack(String resourceName) throws IOException {
65+
// Load legacy-format history
6566
ClassLoader classLoader = WorkflowExecutionUtils.class.getClassLoader();
6667
URL resource = classLoader.getResource(resourceName);
6768
String historyUrl = resource.getFile();
@@ -70,10 +71,30 @@ public void deserializeAndSerializeBack(String resourceName) throws IOException
7071
try (Reader reader = Files.newBufferedReader(historyFile.toPath(), UTF_8)) {
7172
originalSerializedJsonHistory = CharStreams.toString(reader);
7273
}
74+
originalSerializedJsonHistory = originalSerializedJsonHistory.replace("\r\n", "\n");
75+
76+
// Confirm original history is legacy format
77+
assertTrue(
78+
originalSerializedJsonHistory.contains("\"eventType\": \"WorkflowExecutionStarted\""));
79+
assertFalse(
80+
originalSerializedJsonHistory.contains(
81+
"\"eventType\": \"EVENT_TYPE_WORKFLOW_EXECUTION_STARTED\""));
7382

7483
WorkflowExecutionHistory history = WorkflowHistoryLoader.readHistoryFromResource(resourceName);
7584

76-
String serializedHistory = history.toJson(true);
77-
assertEquals(originalSerializedJsonHistory, serializedHistory);
85+
// Confirm serialized to old matches char-for-char
86+
assertEquals(originalSerializedJsonHistory, history.toJson(true, true));
87+
88+
// Confirm can convert to new-format
89+
String newFormatSerializedHistory = history.toJson(true);
90+
assertFalse(newFormatSerializedHistory.contains("\"eventType\": \"WorkflowExecutionStarted\""));
91+
assertTrue(
92+
newFormatSerializedHistory.contains(
93+
"\"eventType\": \"EVENT_TYPE_WORKFLOW_EXECUTION_STARTED\""));
94+
95+
// And that new format is parsed correctly
96+
WorkflowExecutionHistory newFormatHistory =
97+
WorkflowExecutionHistory.fromJson(newFormatSerializedHistory);
98+
assertEquals(history.getHistory(), newFormatHistory.getHistory());
7899
}
79100
}

0 commit comments

Comments
 (0)