-
Notifications
You must be signed in to change notification settings - Fork 5k
[Improvement-17960][API]Add null/empty/duplicate validation logic for globalParams when creating or updating a workflow #17961
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
| List<TaskDefinitionLog> taskDefinitionLogs = generateTaskDefinitionList(taskDefinitionJson); | ||
| List<WorkflowTaskRelationLog> taskRelationList = generateTaskRelationList(taskRelationJson, taskDefinitionLogs); | ||
|
|
||
|
|
@@ -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); | ||
|
|
||
|
|
@@ -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) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Directly throw
ServiceExceptionatvalidateGlobalParams, please don't use a lot of try catch here.