Browse Source

[Fix-5483] [Bug][API] Can't view variables in the page of Process Instance (#5631)

kyoty 3 years ago
parent
commit
0d5037e7c3

+ 3 - 5
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<String, String> 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<Property> localParamsList = JSONUtils.toList(localParams, Property.class);
 
@@ -674,7 +673,6 @@ public class ProcessInstanceServiceImpl extends BaseServiceImpl implements Proce
                     localUserDefParams.put(taskDefinitionLog.getName(), localParamsMap);
                 }
             }
-
         }
         return localUserDefParams;
     }

+ 16 - 0
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<Map<String, String>>() {});
     }
 
+    /**
+     * 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
      *

+ 8 - 0
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";