fix(接口测试): 场景中复制的API修改请求方式和路径不生效

--bug=1038772 --user=陈建星 【接口测试】场景-创建场景-添加复制的api请求-修改api的请求方式和路径不生效 https://www.tapd.cn/55049933/s/1491001
This commit is contained in:
AgAngle 2024-04-09 10:25:17 +08:00 committed by Craftsman
parent 7948c7f0a8
commit 9f0e3728e6
3 changed files with 54 additions and 23 deletions

View File

@ -2,6 +2,7 @@ package io.metersphere.api.dto;
import io.metersphere.api.dto.request.MsCommonElement;
import io.metersphere.api.dto.request.http.MsHTTPElement;
import io.metersphere.api.dto.scenario.ApiScenarioStepCommonDTO;
import lombok.Data;
import java.util.*;
@ -33,6 +34,12 @@ public class ApiScenarioParseTmpParam {
* value MsHTTPElement
*/
private Map<String, List<MsHTTPElement>> stepTypeHttpElementMap = new HashMap<>();
/**
* 步骤 Map
* key uniqueId
* value 步骤
*/
private Map<String, ApiScenarioStepCommonDTO> uniqueIdStepMap = new HashMap<>();
/**
* 场景中所有的 MsCommonElement 列表
*/

View File

@ -279,25 +279,6 @@ public class ApiCommonService {
return null;
}
/**
* 设置 MsHTTPElement 中的 method 等信息
* @param httpElements
* @param getDefinitionInfoFunc
*/
public void setApiDefinitionExecuteInfo(List<MsHTTPElement> httpElements, Function<List<String>, List<ApiDefinitionExecuteInfo>> getDefinitionInfoFunc) {
if (CollectionUtils.isNotEmpty(httpElements)) {
List<String> resourceIds = httpElements.stream().map(MsHTTPElement::getResourceId).collect(Collectors.toList());
// 获取接口模块信息
Map<String, ApiDefinitionExecuteInfo> resourceModuleMap = getApiDefinitionExecuteInfoMap(getDefinitionInfoFunc, resourceIds);
httpElements.forEach(httpElement -> {
ApiDefinitionExecuteInfo definitionExecuteInfo = resourceModuleMap.get(httpElement.getResourceId());
// httpElement 设置模块,请求方法等信息
setApiDefinitionExecuteInfo(httpElement, definitionExecuteInfo);
});
}
}
/**
* 获取资源 ID 和接口定义信息 Map
* @param getDefinitionInfoFunc

View File

@ -1513,7 +1513,7 @@ public class ApiScenarioService extends MoveNodeService {
parseStep2MsElement(msScenario, steps, tmpParam);
// 设置 HttpElement 的模块信息
setApiDefinitionExecuteInfo(tmpParam.getStepTypeHttpElementMap());
setApiDefinitionExecuteInfo(tmpParam.getUniqueIdStepMap(), tmpParam.getStepTypeHttpElementMap());
// 设置使用脚本前后置的公共脚本信息
apiCommonService.setEnableCommonScriptProcessorInfo(tmpParam.getCommonElements());
@ -1532,11 +1532,40 @@ public class ApiScenarioService extends MoveNodeService {
* 设置 HttpElement 的模块信息
* 用户环境中的模块过滤
*
* @param uniqueIdStepMap
* @param stepTypeHttpElementMap
*/
private void setApiDefinitionExecuteInfo(Map<String, List<MsHTTPElement>> stepTypeHttpElementMap) {
apiCommonService.setApiDefinitionExecuteInfo(stepTypeHttpElementMap.get(ApiScenarioStepType.API.name()), apiDefinitionService::getModuleInfoByIds);
apiCommonService.setApiDefinitionExecuteInfo(stepTypeHttpElementMap.get(ApiScenarioStepType.API_CASE.name()), apiTestCaseService::getModuleInfoByIds);
private void setApiDefinitionExecuteInfo(Map<String, ApiScenarioStepCommonDTO> uniqueIdStepMap, Map<String, List<MsHTTPElement>> stepTypeHttpElementMap) {
setApiDefinitionExecuteInfo(uniqueIdStepMap, stepTypeHttpElementMap.get(ApiScenarioStepType.API.name()), apiDefinitionService::getModuleInfoByIds);
setApiDefinitionExecuteInfo(uniqueIdStepMap, stepTypeHttpElementMap.get(ApiScenarioStepType.API_CASE.name()), apiTestCaseService::getModuleInfoByIds);
}
/**
* 设置 MsHTTPElement 中的 method 等信息
* @param httpElements
* @param getDefinitionInfoFunc
*/
public void setApiDefinitionExecuteInfo(Map<String, ApiScenarioStepCommonDTO> uniqueIdStepMap, List<MsHTTPElement> httpElements, Function<List<String>, List<ApiDefinitionExecuteInfo>> getDefinitionInfoFunc) {
if (org.apache.commons.collections.CollectionUtils.isNotEmpty(httpElements)) {
List<String> resourceIds = httpElements.stream().map(MsHTTPElement::getResourceId).collect(Collectors.toList());
// 获取接口模块信息
Map<String, ApiDefinitionExecuteInfo> resourceModuleMap = apiCommonService.getApiDefinitionExecuteInfoMap(getDefinitionInfoFunc, resourceIds);
httpElements.forEach(httpElement -> {
ApiDefinitionExecuteInfo definitionExecuteInfo = resourceModuleMap.get(httpElement.getResourceId());
String path = httpElement.getPath();
String method = httpElement.getMethod();
// httpElement 设置模块,请求方法等信息
apiCommonService.setApiDefinitionExecuteInfo(httpElement, definitionExecuteInfo);
ApiScenarioStepCommonDTO step = uniqueIdStepMap.get(httpElement.getStepId());
if (step != null && isCopyApi(step.getStepType(), step.getRefType())) {
// 复制的接口定义不使用源接口定义的path和method
httpElement.setPath(path);
httpElement.setMethod(method);
}
});
}
}
/**
@ -1669,6 +1698,8 @@ public class ApiScenarioService extends MoveNodeService {
step.setUniqueId(IDGenerator.nextStr());
}
parseParam.getUniqueIdStepMap().put(step.getUniqueId(), step);
// 将步骤详情解析生成对应的MsTestElement
AbstractMsTestElement msTestElement = stepParser.parseTestElement(step,
MapUtils.isNotEmpty(resourceDetailMap) ? resourceDetailMap.getOrDefault(step.getResourceId(), StringUtils.EMPTY) : StringUtils.EMPTY, stepDetailMap.get(step.getId()));
@ -2065,10 +2096,22 @@ public class ApiScenarioService extends MoveNodeService {
return isApi(stepType) && StringUtils.equals(refType, ApiScenarioStepRefType.REF.name());
}
/**
* 判断步骤是否是引用的接口定义
* 引用的接口定义允许修改参数值需要特殊处理
*/
private boolean isCopyApi(String stepType, String refType) {
return isApi(stepType) && isCopy(refType);
}
private boolean isApi(String stepType) {
return StringUtils.equals(stepType, ApiScenarioStepType.API.name());
}
private boolean isCopy(String refType) {
return StringUtils.equals(refType, ApiScenarioStepRefType.COPY.name());
}
private boolean isApiCase(String stepType) {
return StringUtils.equals(stepType, ApiScenarioStepType.API_CASE.name());
}