From 0d5037e7c37d7903d9172f165b348058f1ddbf88 Mon Sep 17 00:00:00 2001 From: kyoty Date: Sun, 13 Jun 2021 11:43:53 +0800 Subject: [PATCH] [Fix-5483] [Bug][API] Can't view variables in the page of Process Instance (#5631) --- .../service/impl/ProcessInstanceServiceImpl.java | 8 +++----- .../dolphinscheduler/common/utils/JSONUtils.java | 16 ++++++++++++++++ .../common/utils/JSONUtilsTest.java | 8 ++++++++ 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessInstanceServiceImpl.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessInstanceServiceImpl.java index 1a2841126f..a86a00b008 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessInstanceServiceImpl.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/service/impl/ProcessInstanceServiceImpl.java @@ -660,10 +660,9 @@ public class ProcessInstanceServiceImpl extends BaseServiceImpl implements Proce for (TaskInstance taskInstance : taskInstanceList) { TaskDefinitionLog taskDefinitionLog = taskDefinitionLogMapper.queryByDefinitionCodeAndVersion( taskInstance.getTaskCode(), taskInstance.getTaskDefinitionVersion()); - String parameter = taskDefinitionLog.getTaskParams(); - Map map = JSONUtils.toMap(parameter); - String localParams = map.get(LOCAL_PARAMS); - if (localParams != null && !localParams.isEmpty()) { + + String localParams = JSONUtils.getNodeString(taskDefinitionLog.getTaskParams(), LOCAL_PARAMS); + if (StringUtils.isNotEmpty(localParams)) { localParams = ParameterUtils.convertParameterPlaceholders(localParams, timeParams); List localParamsList = JSONUtils.toList(localParams, Property.class); @@ -674,7 +673,6 @@ public class ProcessInstanceServiceImpl extends BaseServiceImpl implements Proce localUserDefParams.put(taskDefinitionLog.getName(), localParamsMap); } } - } return localUserDefParams; } diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java index b8c949b80c..9e929e36bf 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/utils/JSONUtils.java @@ -31,6 +31,7 @@ import java.util.List; import java.util.Map; import java.util.TimeZone; +import com.fasterxml.jackson.core.JsonProcessingException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -220,6 +221,21 @@ public class JSONUtils { return parseObject(json, new TypeReference>() {}); } + /** + * from the key-value generated json to get the str value no matter the real type of value + * @param json the json str + * @param nodeName key + * @return the str value of key + */ + public static String getNodeString(String json, String nodeName) { + try { + JsonNode rootNode = objectMapper.readTree(json); + return rootNode.has(nodeName) ? rootNode.get(nodeName).toString() : ""; + } catch (JsonProcessingException e) { + return ""; + } + } + /** * json to map * diff --git a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java index af12d5a625..d9398f81d5 100644 --- a/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java +++ b/dolphinscheduler-common/src/test/java/org/apache/dolphinscheduler/common/utils/JSONUtilsTest.java @@ -146,6 +146,14 @@ public class JSONUtilsTest { Assert.assertNull(JSONUtils.parseObject("foo", String.class)); } + @Test + public void testNodeString() { + Assert.assertEquals("", JSONUtils.getNodeString("", "key")); + Assert.assertEquals("", JSONUtils.getNodeString("abc", "key")); + Assert.assertEquals("", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "key")); + Assert.assertEquals("\"foo\"", JSONUtils.getNodeString("{\"bar\":\"foo\"}", "bar")); + } + @Test public void testJsonByteArray() { String str = "foo";