fix(接口测试): 不同文件导入加格式判断以及swagger文件导入的问题

--user=郭雨琦
--bug=1014627
【接口测试】接口定义导入,在ms格式下不选择覆盖模式点导入,然后切换har格式没提示格式错误
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014627
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014636
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014632
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014637
This commit is contained in:
guoyuqi 2022-07-05 14:38:36 +08:00 committed by fit2-zhao
parent bf431e8c3c
commit b84c73212e
7 changed files with 62 additions and 116 deletions

View File

@ -46,7 +46,7 @@ public class HarParser extends HarAbstractParser {
MSException.throwException(e.getMessage());
LogUtil.error(e.getMessage(), e);
}
if (ObjectUtils.isEmpty(har)) {
if (ObjectUtils.isEmpty(har) || har.log == null) {
MSException.throwException("解析失败,请确认选择的是 Har 格式!");
}
ApiDefinitionImport definitionImport = new ApiDefinitionImport();

View File

@ -11,6 +11,7 @@ import io.metersphere.api.parse.MsAbstractParser;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.commons.constants.ApiImportPlatform;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils;
@ -27,23 +28,14 @@ import java.util.stream.Collectors;
public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
/* private ApiModule selectModule;
private String selectModulePath;*/
@Override
public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) {
String testStr = getApiTestStr(source);
JSONObject testObject = JSONObject.parseObject(testStr, Feature.DisableSpecialKeyDetect);
this.projectId = request.getProjectId();
/*if (StringUtils.isNotBlank(request.getModuleId())) {
this.selectModule = ApiDefinitionImportUtil.getSelectModule(request.getModuleId());
if (this.selectModule != null) {
this.selectModulePath = ApiDefinitionImportUtil.getSelectModulePath(this.selectModule.getName(), this.selectModule.getParentId());
}
}*/
if (this.projectId == null) {
MSException.throwException("wrong format");
}
if (testObject.get("projectName") != null || testObject.get("projectId") != null) {// metersphere 格式导入
return parseMsFormat(testStr, request);
} else { // chrome 插件录制格式导入
@ -58,16 +50,10 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
protected List<ApiDefinitionWithBLOBs> parsePluginFormat(JSONObject testObject, ApiTestImportRequest importRequest, Boolean isCreateModule) {
List<ApiDefinitionWithBLOBs> results = new ArrayList<>();
testObject.keySet().forEach(tag -> {
/*String moduleId = null;
if (isCreateModule) {
moduleId = ApiDefinitionImportUtil.buildModule(this.selectModule, tag, this.projectId).getId();
}*/
List<MsHTTPSamplerProxy> msHTTPSamplerProxies = parseMsHTTPSamplerProxy(testObject, tag, false);
for (MsHTTPSamplerProxy msHTTPSamplerProxy : msHTTPSamplerProxies) {
ApiDefinitionWithBLOBs apiDefinition = buildApiDefinition(msHTTPSamplerProxy.getId(), msHTTPSamplerProxy.getName(), msHTTPSamplerProxy.getPath(), msHTTPSamplerProxy.getMethod(), importRequest);
/*
apiDefinition.setModuleId(moduleId);
*/
apiDefinition.setProjectId(this.projectId);
apiDefinition.setRequest(JSONObject.toJSONString(msHTTPSamplerProxy));
apiDefinition.setName(apiDefinition.getPath() + " [" + apiDefinition.getMethod() + "]");
@ -93,19 +79,7 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
caseList.add(item);
});
}
/*
Set<String> moduleIdSet = apiDefinitionImport.getData().stream()
.map(ApiDefinitionWithBLOBs::getModuleId).collect(Collectors.toSet());*/
/* Map<String, NodeTree> nodeMap = null;
List<NodeTree> nodeTree = apiDefinitionImport.getNodeTree();
if (CollectionUtils.isNotEmpty(nodeTree)) {
cutDownTree(nodeTree, moduleIdSet);
ApiDefinitionImportUtil.createNodeTree(nodeTree, projectId, importRequest.getModuleId());
nodeMap = getNodeMap(nodeTree);
}*/
/*Map<String, NodeTree> finalNodeMap = nodeMap;*/
apiDefinitionImport.getData().forEach(apiDefinition -> {
parseApiDefinition(apiDefinition, importRequest, caseMap);
});
@ -122,17 +96,6 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
private void parseApiDefinition(ApiDefinitionWithBLOBs apiDefinition, ApiTestImportRequest importRequest,
Map<String, List<ApiTestCaseWithBLOBs>> caseMap) {
String originId = apiDefinition.getId();
/*if (nodeMap != null && nodeMap.get(apiDefinition.getModuleId()) != null) {
NodeTree nodeTree = nodeMap.get(apiDefinition.getModuleId());
apiDefinition.setModuleId(nodeTree.getNewId());
apiDefinition.setModulePath(nodeTree.getPath());
} else {
if (StringUtils.isBlank(apiDefinition.getModulePath())) {
apiDefinition.setModuleId(null);
}
// 旧版本未导出模块
parseModule(apiDefinition.getModulePath(), importRequest, apiDefinition);
}*/
apiDefinition.setProjectId(this.projectId);
JSONObject requestObj = this.parseObject(apiDefinition.getRequest(), apiDefinition.getProjectId());
@ -195,33 +158,4 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
}
}
/*
private void parseModule(String modulePath, ApiTestImportRequest importRequest, ApiDefinitionWithBLOBs apiDefinition) {
if (StringUtils.isEmpty(modulePath)) {
return;
}
if (modulePath.startsWith("/")) {
modulePath = modulePath.substring(1, modulePath.length());
}
if (modulePath.endsWith("/")) {
modulePath = modulePath.substring(0, modulePath.length() - 1);
}
List<String> modules = Arrays.asList(modulePath.split("/"));
ApiModule parent = this.selectModule;
Iterator<String> iterator = modules.iterator();
while (iterator.hasNext()) {
String item = iterator.next();
parent = ApiDefinitionImportUtil.buildModule(parent, item, this.projectId, importRequest.getUserId());
if (!iterator.hasNext()) {
apiDefinition.setModuleId(parent.getId());
String path = apiDefinition.getModulePath() == null ? "" : apiDefinition.getModulePath();
if (StringUtils.isNotBlank(this.selectModulePath)) {
apiDefinition.setModulePath(this.selectModulePath + path);
} else if (StringUtils.isBlank(importRequest.getModuleId())) {
apiDefinition.setModulePath("/未规划接口" + path);
}
}
}
}
*/
}

View File

@ -1,6 +1,7 @@
package io.metersphere.api.dto.definition.parse;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import io.metersphere.api.dto.ApiTestImportRequest;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
@ -14,6 +15,7 @@ import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.Project;
import io.metersphere.base.mapper.ProjectMapper;
import io.metersphere.commons.constants.ProjectApplicationType;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.dto.ProjectConfig;
@ -25,24 +27,25 @@ import java.util.*;
public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefinitionImport> {
/*private ApiModule selectModule;
private String selectModulePath;*/
@Override
public ApiDefinitionImport parse(InputStream source, ApiTestImportRequest request) {
String testStr = getApiTestStr(source);
this.projectId = request.getProjectId();
JSONObject jsonObject = JSON.parseObject(testStr);
Object info = jsonObject.get("info");
if (info == null) {
MSException.throwException("wrong format");
} else {
JSONObject jsonObject1 = JSON.parseObject(info.toString());
if (jsonObject1.get("_postman_id") == null) {
MSException.throwException("wrong format");
}
}
PostmanCollection postmanCollection = JSON.parseObject(testStr, PostmanCollection.class, Feature.DisableSpecialKeyDetect);
List<PostmanKeyValue> variables = postmanCollection.getVariable();
ApiDefinitionImport apiImport = new ApiDefinitionImport();
List<ApiDefinitionWithBLOBs> results = new ArrayList<>();
/*this.selectModule = ApiDefinitionImportUtil.getSelectModule(request.getModuleId());
if (this.selectModule != null) {
this.selectModulePath = ApiDefinitionImportUtil.getSelectModulePath(this.selectModule.getName(), this.selectModule.getParentId());
}
ApiModule apiModule = ApiDefinitionImportUtil.buildModule(this.selectModule, postmanCollection.getInfo().getName(), this.projectId);*/
String modulePath = null;
if (StringUtils.isNotBlank(postmanCollection.getInfo().getName())) {
modulePath = "/" + postmanCollection.getInfo().getName();
@ -68,8 +71,7 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
for (PostmanItem item : items) {
List<PostmanItem> childItems = item.getItem();
if (childItems != null) {
/*ApiModule module = null;
module = ApiDefinitionImportUtil.buildModule(parentModule, item.getName(), this.projectId);*/
if (StringUtils.isNotBlank(modulePath) && StringUtils.isNotBlank(item.getName())) {
modulePath = modulePath + "/" + item.getName();
}
@ -82,14 +84,7 @@ public class PostmanDefinitionParser extends PostmanAbstractParserParser<ApiDefi
request.setPath(msHTTPSamplerProxy.getPath());
request.setRequest(JSON.toJSONString(msHTTPSamplerProxy));
request.setResponse(JSON.toJSONString(response));
/*if (parentModule != null) {
request.setModuleId(parentModule.getId());
if (StringUtils.isNotBlank(this.selectModulePath)) {
request.setModulePath(this.selectModulePath + "/" + path);
} else {
request.setModulePath("/" + path);
}
}*/
if (StringUtils.isNotBlank(modulePath)) {
request.setModulePath(modulePath);
}

View File

@ -46,6 +46,12 @@ public class Swagger2Parser extends SwaggerAbstractParser {
} else {
sourceStr = getApiTestStr(source); // 导入的二进制文件转换为 String
JSONObject jsonObject = JSONObject.parseObject(sourceStr);
if (jsonObject.get("swagger") == null || jsonObject.get("swagger") == "null" || jsonObject.get("swagger") == " ") {
if (jsonObject.get("openapi") == null || jsonObject.get("openapi") == "null" || jsonObject.get("openapi") == " ") {
MSException.throwException("wrong format");
}
}
swagger = new SwaggerParser().readWithInfo(sourceStr, false).getSwagger();
}
if (swagger == null || swagger.getSwagger() == null) { // 不是 2.0 版本则尝试转换 3.0
@ -114,15 +120,6 @@ public class Swagger2Parser extends SwaggerAbstractParser {
List<ApiDefinitionWithBLOBs> results = new ArrayList<>();
/*ApiModule selectModule = null;
String selectModulePath = null;
if (StringUtils.isNotBlank(importRequest.getModuleId())) {
selectModule = ApiDefinitionImportUtil.getSelectModule(importRequest.getModuleId());
if (selectModule != null) {
selectModulePath = ApiDefinitionImportUtil.getSelectModulePath(selectModule.getName(), selectModule.getParentId());
}
}*/
String basePath = swagger.getBasePath();
for (String pathName : pathNames) {
Path path = paths.get(pathName);
@ -141,9 +138,7 @@ public class Swagger2Parser extends SwaggerAbstractParser {
}
apiDefinition.setRequest(JSON.toJSONString(request));
apiDefinition.setResponse(JSON.toJSONString(parseResponse(operation, operation.getResponses())));
/*
buildModule(selectModule, apiDefinition, operation.getTags(), selectModulePath);
*/
buildModulePath(apiDefinition, operation.getTags());
if (operation.isDeprecated() != null && operation.isDeprecated()) {
apiDefinition.setTags("[\"Deleted\"]");
@ -163,6 +158,9 @@ public class Swagger2Parser extends SwaggerAbstractParser {
}
private String getModulePath(List<String> tagTree, StringBuilder modulePath) {
if (tagTree == null) {
return "";
}
for (String s : tagTree) {
if (s.contains("/")) {
String[] split = s.split("/");

View File

@ -1328,9 +1328,24 @@ public class ApiDefinitionService {
public ApiDefinitionImport apiTestImport(MultipartFile file, ApiTestImportRequest request) {
//通过platform获取对应的导入解析类型
String originalFilename = file.getOriginalFilename();
String suffixName = originalFilename.substring(originalFilename.indexOf("."));
if (suffixName.equalsIgnoreCase("jmx")) {
if (!request.getPlatform().equalsIgnoreCase("JMeter")) {
MSException.throwException("文件格式不符合要求");
}
}
if (suffixName.equalsIgnoreCase("har")) {
if (!request.getPlatform().equalsIgnoreCase("Har")) {
MSException.throwException("文件格式不符合要求");
}
}
ApiImportParser runService = ApiDefinitionImportParserFactory.getApiImportParser(request.getPlatform());
ApiDefinitionImport apiImport = null;
if (StringUtils.isNotBlank(request.getSwaggerUrl())) {
if (!request.getPlatform().equalsIgnoreCase("Swagger2")) {
MSException.throwException("文件格式不符合要求");
}
if (!UrlTestUtils.testUrlWithTimeOut(request.getSwaggerUrl(), 30000)) {
MSException.throwException(Translator.get("connection_timeout"));
}
@ -1350,7 +1365,11 @@ public class ApiDefinitionService {
if (StringUtils.contains(returnThrowException, "模块树最大深度为")) {
MSException.throwException(returnThrowException);
} else {
MSException.throwException(Translator.get("parse_data_error"));
if (returnThrowException.equals("wrong format")) {
MSException.throwException("文件格式不符合要求");
} else {
MSException.throwException(Translator.get("parse_data_error"));
}
}
// 发送通知
if (StringUtils.equals(request.getType(), "schedule")) {
@ -1708,11 +1727,6 @@ public class ApiDefinitionService {
(query) -> extApiDefinitionMapper.selectIds(query));
this.removeToGc(request.getIds());
// ApiDefinitionExampleWithOperation example = new ApiDefinitionExampleWithOperation();
// example.createCriteria().andIdIn(request.getIds());
// example.setOperator(SessionUtils.getUserId());
// example.setOperationTime(System.currentTimeMillis());
// extApiDefinitionMapper.removeToGcByExample(example);
}
public Pager<List<ApiDefinitionResult>> listRelevance(ApiDefinitionRequest request, int goPage, int pageSize) {

View File

@ -89,13 +89,6 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
Map<String, List<String>> filters = new LinkedHashMap<>();
filters.put("status", list);
request.setFilters(filters);
// apiModules.forEach(node -> {
// List<String> moduleIds = new ArrayList<>();
// moduleIds = this.nodeList(apiModules, node.getId(), moduleIds);
// moduleIds.add(node.getId());
// request.setModuleIds(moduleIds);
// node.setCaseNum(extApiDefinitionMapper.moduleCount(request));
// });
//优化 所有统计SQL一次查询出来
List<String> allModuleIdList = new ArrayList<>();
@ -786,6 +779,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
apiDefinitionWithBLOBs.setVersionId(v.getVersionId());
apiDefinitionWithBLOBs.setModuleId(v.getModuleId());
apiDefinitionWithBLOBs.setModulePath(v.getModulePath());
apiDefinitionWithBLOBs.setNum(v.getNum());
apiDefinitionWithBLOBs.setStatus(v.getStatus());
toUpdateList.add(apiDefinitionWithBLOBs);
}
});
@ -801,6 +796,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
if (apiDefinitionWithBLOBs != null) {
apiDefinitionWithBLOBs.setId(v.getId());
apiDefinitionWithBLOBs.setVersionId(v.getVersionId());
apiDefinitionWithBLOBs.setNum(v.getNum());
apiDefinitionWithBLOBs.setStatus(v.getStatus());
toUpdateList.add(apiDefinitionWithBLOBs);
}
});
@ -964,6 +961,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
api.setOrder(definitionWithBLOBs.getOrder());
api.setRefId(apiDefinitionWithBLOBs.getRefId());
api.setLatest(apiDefinitionWithBLOBs.getLatest());
api.setNum(definitionWithBLOBs.getNum());
api.setStatus(definitionWithBLOBs.getStatus());
coverApiList.add(api);
}
optionData.remove(apiDefinitionWithBLOBs);
@ -981,6 +980,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
ApiDefinitionWithBLOBs api = new ApiDefinitionWithBLOBs();
BeanUtils.copyBean(api, apiDefinitionWithBLOBs);
api.setId(definitionWithBLOBs.getId());
api.setNum(definitionWithBLOBs.getNum());
api.setStatus(definitionWithBLOBs.getStatus());
api.setVersionId(definitionWithBLOBs.getVersionId());
api.setModuleId(definitionWithBLOBs.getModuleId());
api.setModulePath(definitionWithBLOBs.getModulePath());

View File

@ -621,6 +621,8 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
apiScenarioWithBLOBs.setVersionId(v.getVersionId());
apiScenarioWithBLOBs.setApiScenarioModuleId(v.getApiScenarioModuleId());
apiScenarioWithBLOBs.setModulePath(v.getModulePath());
apiScenarioWithBLOBs.setNum(v.getNum());
apiScenarioWithBLOBs.setStatus(v.getStatus());
toUpdateList.add(apiScenarioWithBLOBs);
}
});
@ -640,6 +642,8 @@ public class ApiScenarioModuleService extends NodeTreeService<ApiScenarioModuleD
if (apiScenarioWithBLOBs != null) {
apiScenarioWithBLOBs.setId(v.getId());
apiScenarioWithBLOBs.setVersionId(v.getVersionId());
apiScenarioWithBLOBs.setNum(v.getNum());
apiScenarioWithBLOBs.setStatus(v.getStatus());
toUpdateList.add(apiScenarioWithBLOBs);
}
});