diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/SwaggerApiExportResult.java b/backend/src/main/java/io/metersphere/api/dto/definition/SwaggerApiExportResult.java index 627a369318..c6fe156686 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/SwaggerApiExportResult.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/SwaggerApiExportResult.java @@ -14,9 +14,9 @@ import java.util.List; public class SwaggerApiExportResult extends ApiExportResult{ private String openapi; private SwaggerInfo info; - private String externalDocs; + private JSONObject externalDocs; private List servers; private List tags; private JSONObject paths; // Map, Object 里放 Operation 对象 - private List components; + private JSONObject components; } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java index 8ad99168f6..5e96e31159 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/Swagger3Parser.java @@ -231,6 +231,9 @@ public class Swagger3Parser extends SwaggerAbstractParser { MediaType mediaType = content.get(contentType); if (mediaType == null) { Set contentTypes = content.keySet(); + if(contentTypes.size() == 0) { // 防止空指针 + return; + } contentType = contentTypes.iterator().next(); if (StringUtils.isBlank(contentType)) { return; @@ -410,26 +413,34 @@ public class Swagger3Parser extends SwaggerAbstractParser { result.setInfo(new SwaggerInfo()); result.setServers(new ArrayList<>()); result.setTags(new ArrayList<>()); - result.setComponents(new ArrayList<>()); + result.setComponents(new JSONObject()); + result.setExternalDocs(new JSONObject()); JSONObject paths = new JSONObject(); - JSONObject swaggerPath = new JSONObject(); for(ApiDefinitionWithBLOBs apiDefinition : apiDefinitionList) { SwaggerApiInfo swaggerApiInfo = new SwaggerApiInfo(); // {tags:, summary:, description:, parameters:} swaggerApiInfo.setSummary(apiDefinition.getName()); // 设置导入后的模块名 (根据 api 的 moduleID 查库获得所属模块,作为导出的模块名) ApiModuleService apiModuleService = CommonBeanFactory.getBean(ApiModuleService.class); - String moduleName = apiModuleService.getNode(apiDefinition.getModuleId()).getName(); + String moduleName = ""; + if(apiDefinition.getModuleId() != null) { // module_id 可能为空 + moduleName = apiModuleService.getNode(apiDefinition.getModuleId()).getName(); + } swaggerApiInfo.setTags(Arrays.asList(moduleName)); // 设置请求体 JSONObject requestObject = JSON.parseObject(apiDefinition.getRequest()); // 将api的request属性转换成JSON对象以便获得参数 JSONObject requestBody = buildRequestBody(requestObject); swaggerApiInfo.setRequestBody(requestBody); + // 设置响应体 + swaggerApiInfo.setResponses(new JSONObject()); // 设置请求参数列表 List paramsList = buildParameters(requestObject); swaggerApiInfo.setParameters(paramsList); - swaggerPath.put(apiDefinition.getMethod().toLowerCase(), JSON.parseObject(JSON.toJSONString(swaggerApiInfo))); // 设置api的请求类型和api定义、参数 - paths.put(apiDefinition.getPath(), swaggerPath); + JSONObject methodDetail = JSON.parseObject(JSON.toJSONString(swaggerApiInfo)); + if(paths.getJSONObject(apiDefinition.getPath()) == null) { + paths.put(apiDefinition.getPath(), new JSONObject()); + } // 一个路径下有多个发方法,如post,get,因此是一个 JSONObject 类型 + paths.getJSONObject(apiDefinition.getPath()).put(apiDefinition.getMethod().toLowerCase(), methodDetail); } result.setPaths(paths); return result; @@ -477,7 +488,9 @@ public class Swagger3Parser extends SwaggerAbstractParser { schema.put("format", null); typeName.put("schema", schema); JSONObject content = new JSONObject(); - content.put(typeMap.get(type), typeName); + if (type != null && StringUtils.isNotBlank(type)) { + content.put(typeMap.get(type), typeName); + } requestBody.put("content", content); return requestBody; } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/parse/swagger/SwaggerApiInfo.java b/backend/src/main/java/io/metersphere/api/dto/definition/parse/swagger/SwaggerApiInfo.java index bc22f85037..443f01545f 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/parse/swagger/SwaggerApiInfo.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/parse/swagger/SwaggerApiInfo.java @@ -14,4 +14,5 @@ public class SwaggerApiInfo { private String summary; // 对应 API 的名字 private List parameters; // 对应 API 的请求参数 private JSONObject requestBody; + private JSONObject responses; } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java index b4325f33ff..58b712a08e 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java @@ -140,7 +140,7 @@ public class MsScenario extends MsTestElement { if (arguments != null) { tree.add(config.valueSupposeMock(arguments)); } - this.addCsvDataSet(tree, variables); + this.addCsvDataSet(tree, variables,config); this.addCounter(tree, variables); this.addRandom(tree, variables); if (CollectionUtils.isNotEmpty(this.headers)) { diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java index 55fdb3b511..1c80bbe352 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java @@ -201,7 +201,7 @@ public abstract class MsTestElement { return null; } - protected void addCsvDataSet(HashTree tree, List variables) { + protected void addCsvDataSet(HashTree tree, List variables,ParameterConfig config) { if (CollectionUtils.isNotEmpty(variables)) { List list = variables.stream().filter(ScenarioVariable::isCSVValid).collect(Collectors.toList()); if (CollectionUtils.isNotEmpty(list)) { @@ -213,7 +213,7 @@ public abstract class MsTestElement { csvDataSet.setName(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName()); csvDataSet.setProperty("fileEncoding", StringUtils.isEmpty(item.getEncoding()) ? "UTF-8" : item.getEncoding()); if (CollectionUtils.isNotEmpty(item.getFiles())) { - if (new File(BODY_FILE_DIR + "/" + item.getFiles().get(0).getId() + "_" + item.getFiles().get(0).getName()).exists()) { + if (!config.isOperating() && new File(BODY_FILE_DIR + "/" + item.getFiles().get(0).getId() + "_" + item.getFiles().get(0).getName()).exists()) { MSException.throwException(StringUtils.isEmpty(item.getName()) ? "CSVDataSet" : item.getName() + ":[ CSV文件不存在 ]"); } csvDataSet.setProperty("filename", BODY_FILE_DIR + "/" + item.getFiles().get(0).getId() + "_" + item.getFiles().get(0).getName()); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/controller/MsLoopController.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/controller/MsLoopController.java index 30543eeb31..07c1ff4fe0 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/controller/MsLoopController.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/controller/MsLoopController.java @@ -56,7 +56,7 @@ public class MsLoopController extends MsTestElement { } final HashTree groupTree = controller(tree); if (CollectionUtils.isNotEmpty(config.getVariables())) { - this.addCsvDataSet(groupTree, config.getVariables()); + this.addCsvDataSet(groupTree, config.getVariables(),config); this.addCounter(groupTree, config.getVariables()); this.addRandom(groupTree, config.getVariables()); } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java index 710ab4b672..060fbd3215 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/unknown/MsJmeterElement.java @@ -58,7 +58,7 @@ public class MsJmeterElement extends MsTestElement { LogUtil.info(((ThreadGroup) scriptWrapper).getName() + "是被禁用线程组不加入执行"); } else { // CSV数据检查文件路径是否还存在 - if (scriptWrapper instanceof CSVDataSet) { + if (!config.isOperating() && scriptWrapper instanceof CSVDataSet) { String path = ((CSVDataSet) scriptWrapper).getPropertyAsString("filename"); if (!new File(path).exists()) { MSException.throwException(StringUtils.isEmpty(((CSVDataSet) scriptWrapper).getName()) ? "CSVDataSet" : ((CSVDataSet) scriptWrapper).getName() + ":[ CSV文件不存在 ]"); diff --git a/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue b/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue index 907bd6b4cc..f0bce8b17b 100644 --- a/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue +++ b/frontend/src/business/components/api/automation/scenario/ApiScenarioList.vue @@ -1,6 +1,6 @@