Skip to content

Commit

Permalink
perf: API 校验优化 #2769
Browse files Browse the repository at this point in the history
  • Loading branch information
liuliaozhong committed Jul 21, 2024
1 parent 4ca137b commit 9f4ed8b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.hibernate.validator.group.GroupSequenceProvider;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Data
@ApiModel("执行目标")
Expand All @@ -61,18 +60,10 @@ public class TaskTargetVO {
@Deprecated
@CompatibleImplementation(name = "execute_object", deprecatedVersion = "3.9.x", type = CompatibleType.DEPLOY,
explain = "兼容 API, 发布完成后前端使用 executeObjectsInfo 参数,该参数可删除")
@NotNull(
message = "{validation.constraints.InvalidTaskTarget_HostNode_empty.message}",
groups = ValidationGroups.TaskTarget.HostNode.class
)
@Valid
private TaskHostNodeVO hostNodeInfo;

@ApiModelProperty(value = "任务执行对象信息")
@NotNull(
message = "{validation.constraints.InvalidTaskTarget_ExecuteObj_empty.message}",
groups = ValidationGroups.TaskTarget.ExecuteObject.class
)
@Valid
private TaskExecuteObjectsInfoVO executeObjectsInfo;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.tencent.bk.job.common.model.vo.TaskTargetVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;

import java.util.ArrayList;
Expand All @@ -42,7 +43,7 @@ public List<Class<?>> getValidationGroups(TaskTargetVO request) {
List<Class<?>> validationGroups = new ArrayList<>();
validationGroups.add(TaskTargetVO.class);
if (request != null) {
if (request.getVariable() != null){
if (StringUtils.isNotEmpty(request.getVariable())){
validationGroups.add(ValidationGroups.TaskTarget.Variable.class);
} else if (request.getExecuteObjectsInfo() != null){
validationGroups.add(ValidationGroups.TaskTarget.ExecuteObject.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import com.tencent.bk.job.common.validation.ValidationGroups;
import com.tencent.bk.job.execute.model.web.request.WebFastExecuteScriptRequest;
import com.tencent.bk.job.manage.api.common.constants.task.TaskScriptSourceEnum;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.spi.group.DefaultGroupSequenceProvider;

Expand All @@ -43,16 +44,19 @@ public List<Class<?>> getValidationGroups(WebFastExecuteScriptRequest request) {
List<Class<?>> validationGroups = new ArrayList<>();
validationGroups.add(WebFastExecuteScriptRequest.class);
if (request != null) {
// 脚本优先级 scriptVersionId > scriptId > scriptContent
if (request.getScriptVersionId() != null) {
validationGroups.add(ValidationGroups.Script.ScriptVersionId.class);
} else if (request.getScriptId() != null) {
validationGroups.add(ValidationGroups.Script.ScriptId.class);
} else if (request.getContent() != null) {
Integer scriptSource = request.getScriptSource();
if (scriptSource != null && TaskScriptSourceEnum.LOCAL != TaskScriptSourceEnum.valueOf(scriptSource)) {
if (request.getScriptVersionId() != null) {
validationGroups.add(ValidationGroups.Script.ScriptVersionId.class);
} else if (request.getScriptId() != null) {
validationGroups.add(ValidationGroups.Script.ScriptId.class);
} else {
validationGroups.add(ValidationGroups.Script.ScriptContent.class);
validationGroups.add(ValidationGroups.Script.ScriptType.class);
}
} else {
validationGroups.add(ValidationGroups.Script.ScriptContent.class);
validationGroups.add(ValidationGroups.Script.ScriptType.class);
} else {
validationGroups.add(ValidationGroups.Script.ScriptVersionId.class);
}
}
return validationGroups;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@
package com.tencent.bk.job.manage.model.web.vo.task;

import com.tencent.bk.job.common.model.vo.TaskTargetVO;
import com.tencent.bk.job.common.validation.ValidationConstants;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
* @since 19/11/2020 16:51
*/
Expand All @@ -37,11 +42,17 @@ public class TaskFileDestinationInfoVO {
private String path;

@ApiModelProperty("执行账号")
@NotNull(message = "{validation.constraints.AccountId_empty.message}")
@Min(
value = ValidationConstants.COMMON_MIN_1,
message = "{validation.constraints.AccountId_empty.message}"
)
private Long account;

@ApiModelProperty("执行账号名称")
private String accountName;

@ApiModelProperty("目标机器列表")
@Valid
private TaskTargetVO server;
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public class TaskFileStepVO {
private List<TaskFileSourceInfoVO> fileSourceList;

@ApiModelProperty("目标信息")
@Valid
private TaskFileDestinationInfoVO fileDestination;

@ApiModelProperty("超时")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public List<Class<?>> getValidationGroups(TaskStepVO bean){
if (bean.getType() != null && bean.getType().equals(TaskStepTypeEnum.SCRIPT.getValue())){
defaultGroupSequence.add(ValidationGroups.TaskStep.ScriptStep.class);
} else if (bean.getType() != null && bean.getType().equals(TaskStepTypeEnum.FILE.getValue())){
defaultGroupSequence.add(ValidationGroups.TaskStep.ScriptStep.class);
defaultGroupSequence.add(ValidationGroups.TaskStep.FileStep.class);
} else if (bean.getType() != null && bean.getType().equals(TaskStepTypeEnum.APPROVAL.getValue())){
defaultGroupSequence.add(ValidationGroups.TaskStep.ScriptStep.class);
defaultGroupSequence.add(ValidationGroups.TaskStep.ApprovalStep.class);
}
}
return defaultGroupSequence;
Expand Down

0 comments on commit 9f4ed8b

Please sign in to comment.