Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
import org.apache.dolphinscheduler.dao.repository.WorkflowDefinitionDao;
import org.apache.dolphinscheduler.dao.repository.WorkflowDefinitionLogDao;
import org.apache.dolphinscheduler.dao.utils.WorkerGroupUtils;
import org.apache.dolphinscheduler.plugin.task.api.enums.Direct;
import org.apache.dolphinscheduler.plugin.task.api.enums.SqlType;
import org.apache.dolphinscheduler.plugin.task.api.enums.TaskTimeoutStrategy;
import org.apache.dolphinscheduler.plugin.task.api.model.DependentItem;
Expand Down Expand Up @@ -291,6 +292,15 @@ public Map<String, Object> createWorkflowDefinition(User loginUser,
definition.getName(), definition.getCode());
throw new ServiceException(Status.WORKFLOW_DEFINITION_NAME_EXIST, name);
}

try {
validateGlobalParams(globalParams);
} catch (ServiceException ex) {
log.warn("Invalid globalParams: {}", ex.getMessage());
putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, ex.getMessage());
return result;
}
Comment on lines +296 to +302
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
try {
validateGlobalParams(globalParams);
} catch (ServiceException ex) {
log.warn("Invalid globalParams: {}", ex.getMessage());
putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, ex.getMessage());
return result;
}
validateGlobalParams(globalParams);

Directly throw ServiceException at validateGlobalParams, please don't use a lot of try catch here.


List<TaskDefinitionLog> taskDefinitionLogs = generateTaskDefinitionList(taskDefinitionJson);
List<WorkflowTaskRelationLog> taskRelationList = generateTaskRelationList(taskRelationJson, taskDefinitionLogs);

Expand Down Expand Up @@ -811,6 +821,15 @@ public Map<String, Object> updateWorkflowDefinition(User loginUser,
putMsg(result, Status.DESCRIPTION_TOO_LONG_ERROR);
return result;
}

try {
validateGlobalParams(globalParams);
} catch (ServiceException ex) {
log.warn("Invalid globalParams: {}", ex.getMessage());
putMsg(result, Status.REQUEST_PARAMS_NOT_VALID_ERROR, ex.getMessage());
return result;
}

List<TaskDefinitionLog> taskDefinitionLogs = generateTaskDefinitionList(taskDefinitionJson);
List<WorkflowTaskRelationLog> taskRelationList = generateTaskRelationList(taskRelationJson, taskDefinitionLogs);

Expand Down Expand Up @@ -847,6 +866,42 @@ public Map<String, Object> updateWorkflowDefinition(User loginUser,
return result;
}

/**
* Validates global parameters: non-empty keys, no duplicates, and required values for IN-type params.
*/
private void validateGlobalParams(String globalParams) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We shouldn't be writing this kind of glue code everywhere. Why not use a GlobalParamValidator to handle this task?

if (StringUtils.isBlank(globalParams)) {
return;
}

List<Property> params;
try {
params = JSONUtils.toList(globalParams, Property.class);
} catch (Exception e) {
throw new ServiceException("Invalid globalParams");
}

if (params == null || params.isEmpty()) {
return;
}

Set<String> keys = new HashSet<>();
for (Property p : params) {
if (StringUtils.isEmpty(p.getProp())) {
throw new ServiceException("Global param key cannot be empty");
}

String key = p.getProp().trim();
if (!keys.add(key)) {
throw new ServiceException("Duplicate global param key: " + key);
}

if (Direct.IN.equals(p.getDirect()) && StringUtils.isEmpty(p.getValue())) {
throw new ServiceException("IN param value required for key: " + key);
}
Comment on lines +899 to +901
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should support the value to be null.

}
}

/**
* Task want to delete whether used in other task, should throw exception when have be used.
* <p>
Expand Down