From 10f8c9d983528fe57c01a975e3e98d36fff7a4ff Mon Sep 17 00:00:00 2001 From: naziD Date: Thu, 21 Apr 2022 11:38:41 +0800 Subject: [PATCH] [Bug-9608] Serialize the task definition failed (#9622) * BugFix: serialize the task definition failed * Remove a comment Co-authored-by: lipandong --- .../dao/entity/TaskDefinition.java | 16 +++++++++---- .../dao/mapper/TaskDefinitionMapperTest.java | 24 +++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/TaskDefinition.java b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/TaskDefinition.java index 7ebc415617..58e416e5ef 100644 --- a/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/TaskDefinition.java +++ b/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/entity/TaskDefinition.java @@ -26,12 +26,12 @@ import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.plugin.task.api.model.Property; import org.apache.commons.lang.StringUtils; - +import org.apache.commons.collections4.CollectionUtils; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.stream.Collectors; import com.baomidou.mybatisplus.annotation.FieldStrategy; import com.baomidou.mybatisplus.annotation.IdType; @@ -309,9 +309,17 @@ public class TaskDefinition { public Map getTaskParamMap() { if (taskParamMap == null && StringUtils.isNotEmpty(taskParams)) { JsonNode localParams = JSONUtils.parseObject(taskParams).findValue("localParams"); - if (localParams != null) { + + //If a jsonNode is null, not only use !=null, but also it should use the isNull method to be estimated. + if (localParams != null && !localParams.isNull()) { List propList = JSONUtils.toList(localParams.toString(), Property.class); - taskParamMap = propList.stream().collect(Collectors.toMap(Property::getProp, Property::getValue)); + + if (CollectionUtils.isNotEmpty(propList)) { + taskParamMap = new HashMap<>(); + for (Property property : propList) { + taskParamMap.put(property.getProp(), property.getValue()); + } + } } } return taskParamMap; diff --git a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TaskDefinitionMapperTest.java b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TaskDefinitionMapperTest.java index 03131c0b8c..757f751dd2 100644 --- a/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TaskDefinitionMapperTest.java +++ b/dolphinscheduler-dao/src/test/java/org/apache/dolphinscheduler/dao/mapper/TaskDefinitionMapperTest.java @@ -17,6 +17,7 @@ package org.apache.dolphinscheduler.dao.mapper; +import org.apache.dolphinscheduler.common.utils.JSONUtils; import org.apache.dolphinscheduler.dao.BaseDaoTest; import org.apache.dolphinscheduler.dao.entity.DefinitionGroupByUser; import org.apache.dolphinscheduler.dao.entity.TaskDefinition; @@ -132,4 +133,27 @@ public class TaskDefinitionMapperTest extends BaseDaoTest { } + @Test + public void testNullPropertyValueOfLocalParams() { + String definitionJson = "{\"failRetryTimes\":\"0\",\"timeoutNotifyStrategy\":\"\",\"code\":\"5195043558720\",\"flag\":\"YES\",\"environmentCode\":\"-1\",\"taskDefinitionIndex\":2,\"taskPriority\":\"MEDIUM\",\"taskParams\":\"{\\\"preStatements\\\":null,\\\"postStatements\\\":null,\\\"type\\\":\\\"ADB_MYSQL\\\",\\\"database\\\":\\\"lijia\\\",\\\"sql\\\":\\\"create table nation_${random_serial_number} as select * from nation\\\",\\\"localParams\\\":[{\\\"direct\\\":2,\\\"type\\\":3,\\\"prop\\\":\\\"key\\\"}],\\\"Name\\\":\\\"create_table_as_select_nation\\\",\\\"FailRetryTimes\\\":0,\\\"dbClusterId\\\":\\\"amv-bp10o45925jpx959\\\",\\\"sendEmail\\\":false,\\\"displayRows\\\":10,\\\"limit\\\":10000,\\\"agentSource\\\":\\\"Workflow\\\",\\\"agentVersion\\\":\\\"Unkown\\\"}\",\"timeout\":\"0\",\"taskType\":\"ADB_MYSQL\",\"timeoutFlag\":\"CLOSE\",\"projectCode\":\"5191800302720\",\"name\":\"create_table_as_select_nation\",\"delayTime\":\"0\",\"workerGroup\":\"default\"}"; + TaskDefinition definition = JSONUtils.parseObject(definitionJson, TaskDefinition.class); + + Map taskParamsMap = definition.getTaskParamMap(); + if (taskParamsMap != null) { + Assert.assertNull(taskParamsMap.get("key")); + } else { + Assert.fail("Deserialize the task definition failed"); + } + + String newDefinitionJson = JSONUtils.toJsonString(definition); + Assert.assertNotNull("Serialize the task definition success", newDefinitionJson); + } + + @Test + public void testNullLocalParamsOfTaskParams() { + String definitionJson = "{\"failRetryTimes\":\"0\",\"timeoutNotifyStrategy\":\"\",\"code\":\"5195043558720\",\"flag\":\"YES\",\"environmentCode\":\"-1\",\"taskDefinitionIndex\":2,\"taskPriority\":\"MEDIUM\",\"taskParams\":\"{\\\"preStatements\\\":null,\\\"postStatements\\\":null,\\\"type\\\":\\\"ADB_MYSQL\\\",\\\"database\\\":\\\"lijia\\\",\\\"sql\\\":\\\"create table nation_${random_serial_number} as select * from nation\\\",\\\"localParams\\\":null,\\\"Name\\\":\\\"create_table_as_select_nation\\\",\\\"FailRetryTimes\\\":0,\\\"dbClusterId\\\":\\\"amv-bp10o45925jpx959\\\",\\\"sendEmail\\\":false,\\\"displayRows\\\":10,\\\"limit\\\":10000,\\\"agentSource\\\":\\\"Workflow\\\",\\\"agentVersion\\\":\\\"Unkown\\\"}\",\"timeout\":\"0\",\"taskType\":\"ADB_MYSQL\",\"timeoutFlag\":\"CLOSE\",\"projectCode\":\"5191800302720\",\"name\":\"create_table_as_select_nation\",\"delayTime\":\"0\",\"workerGroup\":\"default\"}"; + TaskDefinition definition = JSONUtils.parseObject(definitionJson, TaskDefinition.class); + + Assert.assertNull("Serialize the task definition success", definition.getTaskParamMap()); + } }