This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Workflow/Task Summary Input/Output Json Serialization (#2128)
* Added configuration for SummaryInputOutputJSONSerialization and updated Workflow/TaskSummary classes and the server's ModulesProvider to leverage the configuration, which allows the summary input/output strings to be serialized as JSON strings as opposed to the default configuration of using Java Map toString() * Updated Task/Workflow Summary Input/Output Json String configuration management and added in tests for the additional features as well as a regression test for expected default behavior * Minor whitespace fix in WorkflowSummary * Another tiny whitespace tweak for WorkflowSummary * Minor whitespace changes to SummaryUtil
- Loading branch information
1 parent
a88e274
commit ac987e0
Showing
9 changed files
with
156 additions
and
12 deletions.
There are no files selected for viewing
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
52 changes: 52 additions & 0 deletions
52
common/src/main/java/com/netflix/conductor/common/utils/SummaryUtil.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,52 @@ | ||
/** | ||
* Copyright 2021 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.netflix.conductor.common.utils; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import java.util.Map; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
public class SummaryUtil { | ||
private static final Logger logger = LoggerFactory.getLogger(SummaryUtil.class); | ||
private static final ObjectMapper objectMapper = new JsonMapperProvider().get(); | ||
private static boolean isSummaryInputOutputJsonSerializationEnabled = false; | ||
|
||
/** | ||
* Serializes the Workflow or Task's Input/Output object by Java's toString (default), or by | ||
* a Json ObjectMapper (@see Configuration.isSummaryInputOutputJsonSerializationEnabled) | ||
* @param object the Input or Output Object to serialize | ||
* @return the serialized string of the Input or Output object | ||
*/ | ||
public static String serializeInputOutput(Map<String, Object> object) { | ||
if (isSummaryInputOutputJsonSerializationEnabled == false) { | ||
return object.toString(); | ||
} | ||
|
||
try { | ||
return objectMapper.writeValueAsString(object); | ||
} catch (JsonProcessingException e) { | ||
logger.error("The provided value ({}) could not be serialized as Json", object.toString(), e); | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
/** | ||
* @param isEnabled (default) false for Java toString, true for Json serialized string | ||
*/ | ||
public static void setSummaryInputOutputJsonSerializationEnabled(boolean isEnabled) { | ||
isSummaryInputOutputJsonSerializationEnabled = isEnabled; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
common/src/test/java/com/netflix/conductor/common/utils/SummaryUtilTest.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,61 @@ | ||
/** | ||
* Copyright 2021 Netflix, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on | ||
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations under the License. | ||
*/ | ||
|
||
package com.netflix.conductor.common.utils; | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class SummaryUtilTest { | ||
private Map<String, Object> testObject; | ||
|
||
public SummaryUtilTest() { | ||
Map<String, Object> child = new HashMap<String, Object>(); | ||
child.put("testStr", "childTestStr"); | ||
|
||
Map<String, Object> obj = new HashMap<String, Object>(); | ||
obj.put("testStr", "stringValue"); | ||
obj.put("testArray", new ArrayList<Integer>(Arrays.asList(1,2,3))); | ||
obj.put("testObj", child); | ||
obj.put("testNull", null); | ||
|
||
this.testObject = obj; | ||
} | ||
|
||
@Test | ||
public void testSerializeInputOutput_defaultToString() throws Exception { | ||
SummaryUtil.setSummaryInputOutputJsonSerializationEnabled(false); | ||
String serialized = SummaryUtil.serializeInputOutput(this.testObject); | ||
|
||
assertEquals("The Java.toString() Serialization should match the serialized Test Object", | ||
this.testObject.toString(), serialized); | ||
} | ||
|
||
@Test | ||
public void testSerializeInputOutput_jsonSerializationEnabled() throws Exception { | ||
ObjectMapper objectMapper = new JsonMapperProvider().get(); | ||
SummaryUtil.setSummaryInputOutputJsonSerializationEnabled(true); | ||
String serialized = SummaryUtil.serializeInputOutput(this.testObject); | ||
|
||
assertEquals("The ObjectMapper Json Serialization should match the serialized Test Object", | ||
objectMapper.writeValueAsString(this.testObject), serialized); | ||
} | ||
|
||
} |
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
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