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 1c8db69
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.InvalidParamException;
import com.tencent.bk.job.common.validation.ExecuteTargetNotEmpty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
Expand All @@ -41,7 +40,6 @@
@ApiModel("任务执行对象信息")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Slf4j
@ExecuteTargetNotEmpty
public class TaskExecuteObjectsInfoVO {

@ApiModelProperty("主机列表")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import com.tencent.bk.job.common.constant.ErrorCode;
import com.tencent.bk.job.common.exception.InvalidParamException;
import com.tencent.bk.job.common.util.json.JsonUtils;
import com.tencent.bk.job.common.validation.ExecuteTargetNotEmpty;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
Expand All @@ -46,7 +45,6 @@
@ApiModel("主机节点信息")
@JsonInclude(JsonInclude.Include.NON_NULL)
@Slf4j
@ExecuteTargetNotEmpty
public class TaskHostNodeVO {

@ApiModelProperty("主机列表")
Expand Down
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 @@ -33,7 +33,6 @@
import lombok.extern.slf4j.Slf4j;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import java.util.Collections;
import java.util.List;

Expand All @@ -49,7 +48,6 @@ public class TaskApprovalStepVO {
private Integer approvalType;

@ApiModelProperty("审批人")
@NotNull(message = "{validation.constraints.InvalidApprovalUser_empty.message}")
@Valid
private UserRoleInfoVO approvalUser;

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 @@ -30,7 +30,6 @@
import com.tencent.bk.job.common.util.FilePathValidateUtil;
import com.tencent.bk.job.common.validation.CheckEnum;
import com.tencent.bk.job.common.validation.NotBlankField;
import com.tencent.bk.job.common.validation.ValidFilePath;
import com.tencent.bk.job.common.validation.ValidationConstants;
import com.tencent.bk.job.common.validation.ValidationGroups;
import com.tencent.bk.job.manage.api.common.constants.task.TaskFileTypeEnum;
Expand All @@ -44,7 +43,6 @@

import javax.validation.Valid;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

Expand All @@ -61,15 +59,13 @@ public class TaskFileSourceInfoVO {
private Long id;

@ApiModelProperty(value = "文件类型 1-服务器文件 2-本地文件 3-文件源文件")
@NotNull(message = "{validation.constraints.InvalidSourceFileType_empty.message}")
@CheckEnum(
enumClass = TaskFileTypeEnum.class,
message = "{validation.constraints.InvalidSourceFileType_illegal.message}"
)
private Integer fileType;

@ApiModelProperty("文件路径")
@NotEmpty(message = "{validation.constraints.InvalidSourceFileList_empty.message}")
private List<String> fileLocation;

@ApiModelProperty(value = "文件 Hash 值 仅本地文件有")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
import org.springframework.util.CollectionUtils;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import java.util.List;

@Data
Expand All @@ -47,11 +46,11 @@
public class TaskFileStepVO {

@ApiModelProperty("源文件列表")
@NotEmpty(message = "{validation.constraints.InvalidSourceFileList_empty.message}")
@Valid
private List<TaskFileSourceInfoVO> fileSourceList;

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

@ApiModelProperty("超时")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
public class TaskScriptStepVO {

@ApiModelProperty(value = "脚本类型 1-本地脚本 2-引用业务脚本 3-引用公共脚本")
@NotNull(message = "{validation.constraints.ScriptSource_empty.message}")
@Range(
min = ValidationConstants.COMMON_MIN_1,
max = ValidationConstants.COMMON_MAX_3,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,26 +36,21 @@
import com.tencent.bk.job.common.validation.NotBlankField;
import com.tencent.bk.job.common.validation.NotContainSpecialChar;
import com.tencent.bk.job.common.validation.ValidationConstants;
import com.tencent.bk.job.common.validation.ValidationGroups;
import com.tencent.bk.job.manage.api.common.constants.task.TaskStepTypeEnum;
import com.tencent.bk.job.manage.validation.provider.TaskStepGroupSequenceProvider;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.group.GroupSequenceProvider;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.groups.Default;
import java.util.List;

@Data
@ApiModel("任务步骤信息")
@Slf4j
@GroupSequenceProvider(TaskStepGroupSequenceProvider.class)
public class TaskStepVO {

@ApiModelProperty("步骤 ID 仅在更新、删除时填写")
Expand All @@ -64,8 +59,7 @@ public class TaskStepVO {
@ApiModelProperty("步骤类型 1-脚本 2-文件 3-人工确认")
@CheckEnum(
enumClass = TaskStepTypeEnum.class,
message = "{validation.constraints.InvalidTaskStepType_illegal.message}",
groups = Default.class
message = "{validation.constraints.InvalidTaskStepType_illegal.message}"
)
private Integer type;

Expand All @@ -83,26 +77,14 @@ public class TaskStepVO {
private Long templateStepId;

@ApiModelProperty("脚本步骤信息")
@NotNull(
message = "{validation.constraints.InvalidTaskScriptStep_empty.message}",
groups = ValidationGroups.TaskStep.ScriptStep.class
)
@Valid
private TaskScriptStepVO scriptStepInfo;

@ApiModelProperty("文件步骤信息")
@NotNull(
message = "{validation.constraints.InvalidTaskFileStep_empty.message}",
groups = ValidationGroups.TaskStep.FileStep.class
)
@Valid
private TaskFileStepVO fileStepInfo;

@ApiModelProperty("审批步骤信息")
@NotNull(
message = "{validation.constraints.InvalidTaskApprovalStep_empty.message}",
groups = ValidationGroups.TaskStep.ApprovalStep.class
)
@Valid
private TaskApprovalStepVO approvalStepInfo;

Expand Down

This file was deleted.

0 comments on commit 1c8db69

Please sign in to comment.