diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index a953e7f296..bea42c576f 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -1220,7 +1220,8 @@ public class ApiAutomationService { return apiScenarioMapper.selectByExampleWithBLOBs(example); } - private void _importCreate(List sameRequest, ApiScenarioMapper batchMapper, ApiScenarioWithBLOBs scenarioWithBLOBs, ApiTestImportRequest apiTestImportRequest) { + private void _importCreate(List sameRequest, ApiScenarioMapper batchMapper, ExtApiScenarioMapper extApiScenarioMapper, + ApiScenarioWithBLOBs scenarioWithBLOBs, ApiTestImportRequest apiTestImportRequest) { String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); if (CollectionUtils.isEmpty(sameRequest)) { scenarioWithBLOBs.setId(UUID.randomUUID().toString()); @@ -1234,6 +1235,7 @@ public class ApiAutomationService { } else { scenarioWithBLOBs.setVersionId(defaultVersion); } + scenarioWithBLOBs.setLatest(true); batchMapper.insert(scenarioWithBLOBs); apiScenarioReferenceIdService.saveByApiScenario(scenarioWithBLOBs); } else { @@ -1265,10 +1267,13 @@ public class ApiAutomationService { batchMapper.updateByPrimaryKeyWithBLOBs(scenarioWithBLOBs); } apiScenarioReferenceIdService.saveByApiScenario(scenarioWithBLOBs); + extApiScenarioMapper.clearLatestVersion(scenarioWithBLOBs.getRefId()); + extApiScenarioMapper.addLatestVersion(scenarioWithBLOBs.getRefId()); } } - private ApiScenarioWithBLOBs importCreate(ApiScenarioWithBLOBs request, ApiScenarioMapper batchMapper, ApiTestImportRequest apiTestImportRequest) { + private ApiScenarioWithBLOBs importCreate(ApiScenarioWithBLOBs request, ApiScenarioMapper batchMapper, ExtApiScenarioMapper extApiScenarioMapper, + ApiTestImportRequest apiTestImportRequest) { final ApiScenarioWithBLOBs scenarioWithBLOBs = new ApiScenarioWithBLOBs(); BeanUtils.copyBean(scenarioWithBLOBs, request); scenarioWithBLOBs.setCreateTime(System.currentTimeMillis()); @@ -1317,7 +1322,7 @@ public class ApiAutomationService { } if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) { - _importCreate(sameRequest, batchMapper, scenarioWithBLOBs, apiTestImportRequest); + _importCreate(sameRequest, batchMapper, extApiScenarioMapper, scenarioWithBLOBs, apiTestImportRequest); } else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) { if (CollectionUtils.isEmpty(sameRequest)) { List useUrl = this.parseUrl(scenarioWithBLOBs); @@ -1331,6 +1336,7 @@ public class ApiAutomationService { String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); scenarioWithBLOBs.setVersionId(defaultVersion); } + scenarioWithBLOBs.setLatest(true); batchMapper.insert(scenarioWithBLOBs); // 存储依赖关系 @@ -1342,7 +1348,7 @@ public class ApiAutomationService { } } else { - _importCreate(sameRequest, batchMapper, scenarioWithBLOBs, apiTestImportRequest); + _importCreate(sameRequest, batchMapper, extApiScenarioMapper, scenarioWithBLOBs, apiTestImportRequest); } return scenarioWithBLOBs; } @@ -1379,9 +1385,7 @@ public class ApiAutomationService { item.setId(UUID.randomUUID().toString()); } // 导入之后刷新latest - ApiScenarioWithBLOBs result = importCreate(item, batchMapper, request); - extApiScenarioMapper.clearLatestVersion(result.getRefId()); - extApiScenarioMapper.addLatestVersion(result.getRefId()); + importCreate(item, batchMapper, extApiScenarioMapper, request); if (i % 300 == 0) { sqlSession.flushStatements(); } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 9deea6cd14..2b10139a9a 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -713,7 +713,8 @@ public class ApiDefinitionService { } private ApiDefinition importCreate(ApiDefinitionWithBLOBs apiDefinition, ApiDefinitionMapper batchMapper, - ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List cases, List mocks, + ApiTestCaseMapper apiTestCaseMapper, ExtApiDefinitionMapper extApiDefinitionMapper, + ApiTestImportRequest apiTestImportRequest, List cases, List mocks, Boolean repeatable) { SaveApiDefinitionRequest saveReq = new SaveApiDefinitionRequest(); BeanUtils.copyBean(saveReq, apiDefinition); @@ -740,7 +741,7 @@ public class ApiDefinitionService { sameRequest = getSameRequestById(apiDefinition.getId(), apiTestImportRequest.getProjectId()); } if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) { - _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest, cases, mocks); + _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, extApiDefinitionMapper, apiTestImportRequest, cases, mocks); } else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) { if (CollectionUtils.isEmpty(sameRequest)) { //postman 可能含有前置脚本,接口定义去掉脚本 @@ -754,6 +755,7 @@ public class ApiDefinitionService { String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); apiDefinition.setVersionId(defaultVersion); } + apiDefinition.setLatest(true); // 新增的接口 latest = true batchMapper.insert(apiDefinition); String requestStr = setImportHashTree(apiDefinition); @@ -770,7 +772,7 @@ public class ApiDefinitionService { mocks.clear(); } } else { - _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest, cases, mocks); + _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, extApiDefinitionMapper, apiTestImportRequest, cases, mocks); } return apiDefinition; @@ -797,10 +799,12 @@ public class ApiDefinitionService { } private void _importCreate(List sameRequest, ApiDefinitionMapper batchMapper, ApiDefinitionWithBLOBs apiDefinition, - ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List cases, List mocks) { + ApiTestCaseMapper apiTestCaseMapper, ExtApiDefinitionMapper extApiDefinitionMapper, + ApiTestImportRequest apiTestImportRequest, List cases, List mocks) { String originId = apiDefinition.getId(); String defaultVersion = extProjectVersionMapper.getDefaultVersion(apiTestImportRequest.getProjectId()); - if (CollectionUtils.isEmpty(sameRequest)) { + + if (CollectionUtils.isEmpty(sameRequest)) { // 没有这个接口 新增 apiDefinition.setId(UUID.randomUUID().toString()); apiDefinition.setRefId(apiDefinition.getId()); if (StringUtils.isNotEmpty(apiTestImportRequest.getVersionId())) { @@ -808,6 +812,7 @@ public class ApiDefinitionService { } else { apiDefinition.setVersionId(defaultVersion); } + apiDefinition.setLatest(true); // 新增接口 latest = true // case 设置版本 cases.forEach(c -> { c.setVersionId(apiDefinition.getVersionId()); @@ -827,8 +832,7 @@ public class ApiDefinitionService { batchMapper.insert(apiDefinition); } - } else { - //如果存在则修改 + } else { //如果存在则修改 if (StringUtils.isEmpty(apiTestImportRequest.getUpdateVersionId())) { apiTestImportRequest.setUpdateVersionId(defaultVersion); } @@ -881,8 +885,8 @@ public class ApiDefinitionService { apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); } } - - + extApiDefinitionMapper.clearLatestVersion(apiDefinition.getRefId()); + extApiDefinitionMapper.addLatestVersion(apiDefinition.getRefId()); } } @@ -1101,7 +1105,7 @@ public class ApiDefinitionService { ApiDefinitionImport apiImport = null; try { apiImport = (ApiDefinitionImport) Objects.requireNonNull(runService).parse(file == null ? null : file.getInputStream(), request); - if(apiImport.getMocks() == null){ + if (apiImport.getMocks() == null) { apiImport.setMocks(new ArrayList<>()); } } catch (Exception e) { @@ -1173,7 +1177,6 @@ public class ApiDefinitionService { } for (int i = 0; i < data.size(); i++) { ApiDefinitionWithBLOBs item = data.get(i); - ApiDefinition result; this.setModule(item); if (item.getName().length() > 255) { item.setName(item.getName().substring(0, 255)); @@ -1184,23 +1187,20 @@ public class ApiDefinitionService { String apiId = item.getId(); EsbApiParamsWithBLOBs model = apiImport.getEsbApiParamsMap().get(apiId); request.setModeId("fullCoverage");//标准版ESB数据导入不区分是否覆盖,默认都为覆盖 - result = importCreate(item, batchMapper, apiTestCaseMapper, request, apiImport.getCases(), apiImport.getMocks(), project.getRepeatable()); + importCreate(item, batchMapper, apiTestCaseMapper, extApiDefinitionMapper, request, apiImport.getCases(), apiImport.getMocks(), project.getRepeatable()); if (model != null) { apiImport.getEsbApiParamsMap().remove(apiId); model.setResourceId(item.getId()); apiImport.getEsbApiParamsMap().put(item.getId(), model); } } else { - result = importCreate(item, batchMapper, apiTestCaseMapper, request, apiImport.getCases(), apiImport.getMocks(), project.getRepeatable()); + importCreate(item, batchMapper, apiTestCaseMapper, extApiDefinitionMapper, request, apiImport.getCases(), apiImport.getMocks(), project.getRepeatable()); } - // 导入之后刷新latest - extApiDefinitionMapper.clearLatestVersion(result.getRefId()); - extApiDefinitionMapper.addLatestVersion(result.getRefId()); if (i % 300 == 0) { sqlSession.flushStatements(); } - sqlSession.flushStatements(); } + sqlSession.flushStatements(); //判断EsbData是否需要存储 if (apiImport.getEsbApiParamsMap() != null && apiImport.getEsbApiParamsMap().size() > 0) { EsbApiParamsMapper esbApiParamsMapper = sqlSession.getMapper(EsbApiParamsMapper.class);