From 46fcef3d72e429837b3be328013262797e1f602d Mon Sep 17 00:00:00 2001 From: WangXu10 Date: Mon, 15 Jul 2024 16:55:03 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AE=9A=E4=B9=89root=E8=8A=82=E7=82=B9?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=E5=AF=BC=E8=87=B4=E5=AF=BC=E5=87=BA=E6=95=B0?= =?UTF-8?q?=E6=8D=AE=E4=B8=BA=E7=A9=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1043951 --user=王旭 【接口测试】接口定义-选中未规划模块导出接口文件内容为空 https://www.tapd.cn/55049933/s/1548295 --- .../dto/definition/ApiDefinitionWithBlob.java | 2 -- .../api/mapper/ExtApiDefinitionMapper.xml | 4 +--- .../metersphere/api/parser/ExportParser.java | 3 ++- .../api/parser/api/Swagger3ExportParser.java | 12 ++++++++-- .../ApiDefinitionExportService.java | 22 ++++++++++++++++--- .../ApiDefinitionControllerTests.java | 10 +++------ 6 files changed, 35 insertions(+), 18 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionWithBlob.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionWithBlob.java index 64c9d07383..9f488d69d6 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionWithBlob.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionWithBlob.java @@ -76,6 +76,4 @@ public class ApiDefinitionWithBlob extends ApiDefinitionBlob { @Schema(description = "删除状态", requiredMode = Schema.RequiredMode.REQUIRED) private Boolean deleted; - @Schema(description = "模块名称") - private String moduleName; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.xml b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.xml index 0785cf658b..f073a4c9c9 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.xml +++ b/backend/services/api-test/src/main/java/io/metersphere/api/mapper/ExtApiDefinitionMapper.xml @@ -644,12 +644,10 @@ SELECT api_definition.*, api_definition_blob.request, - api_definition_blob.response, - api_definition_module.name as moduleName + api_definition_blob.response FROM api_definition INNER JOIN api_definition_blob ON api_definition.id = api_definition_blob.id - inner join api_definition_module on api_definition.module_id = api_definition_module.id where api_definition.id in #{id} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/ExportParser.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ExportParser.java index c4cd1766e8..cbf3c9b8e1 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/ExportParser.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/ExportParser.java @@ -4,7 +4,8 @@ import io.metersphere.api.dto.definition.ApiDefinitionWithBlob; import io.metersphere.project.domain.Project; import java.util.List; +import java.util.Map; public interface ExportParser { - T parse(List list, Project project) throws Exception; + T parse(List list, Project project, Map moduleMap) throws Exception; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3ExportParser.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3ExportParser.java index 0b7bf637d3..3da9a0ae93 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3ExportParser.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/api/Swagger3ExportParser.java @@ -9,7 +9,9 @@ import io.metersphere.api.utils.JSONUtil; import io.metersphere.api.utils.XMLUtil; import io.metersphere.project.constants.PropertyConstant; import io.metersphere.project.domain.Project; +import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.sdk.util.JSON; +import io.metersphere.sdk.util.Translator; import io.swagger.v3.oas.models.responses.ApiResponse; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -23,7 +25,7 @@ public class Swagger3ExportParser implements ExportParser { @Override - public ApiExportResponse parse(List list, Project project) throws Exception { + public ApiExportResponse parse(List list, Project project, Map moduleMap) throws Exception { SwaggerApiExportResponse response = new SwaggerApiExportResponse(); //openapi response.setOpenapi("3.0.2"); @@ -49,7 +51,13 @@ public class Swagger3ExportParser implements ExportParser { for (ApiDefinitionWithBlob apiDefinition : list) { SwaggerApiInfo swaggerApiInfo = new SwaggerApiInfo(); swaggerApiInfo.setSummary(apiDefinition.getName()); - swaggerApiInfo.setTags(Arrays.asList(apiDefinition.getModuleName())); + String moduleName = ""; + if (StringUtils.equals(apiDefinition.getModuleId(), ModuleConstants.DEFAULT_NODE_ID)) { + moduleName = Translator.get("api_unplanned_request"); + } else { + moduleName = moduleMap.get(apiDefinition.getModuleId()); + } + swaggerApiInfo.setTags(Arrays.asList(moduleName)); //请求体 JSONObject requestObject = JSONUtil.parseObject(new String(apiDefinition.getRequest() == null ? new byte[0] : apiDefinition.getRequest(), StandardCharsets.UTF_8)); JSONObject requestBody = buildRequestBody(requestObject, schemas); diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionExportService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionExportService.java index a91c376fb7..1d38c22928 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionExportService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionExportService.java @@ -1,17 +1,23 @@ package io.metersphere.api.service.definition; +import io.metersphere.api.domain.ApiDefinitionModule; +import io.metersphere.api.domain.ApiDefinitionModuleExample; import io.metersphere.api.dto.definition.ApiDefinitionBatchRequest; import io.metersphere.api.dto.definition.ApiDefinitionWithBlob; import io.metersphere.api.dto.export.ApiExportResponse; +import io.metersphere.api.mapper.ApiDefinitionModuleMapper; import io.metersphere.api.mapper.ExtApiDefinitionMapper; import io.metersphere.api.parser.api.Swagger3ExportParser; import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.sdk.exception.MSException; import jakarta.annotation.Resource; +import org.apache.commons.collections.CollectionUtils; import org.springframework.stereotype.Service; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; /** @@ -25,25 +31,35 @@ public class ApiDefinitionExportService { @Resource private ExtApiDefinitionMapper extApiDefinitionMapper; @Resource + private ApiDefinitionModuleMapper apiDefinitionModuleMapper; + @Resource private ProjectMapper projectMapper; public ApiExportResponse export(ApiDefinitionBatchRequest request, String type, String userId) { List ids = apiDefinitionService.getBatchApiIds(request, request.getProjectId(), request.getProtocols(), false, userId); + if (CollectionUtils.isEmpty(ids)) { + return new ApiExportResponse(); + } List list = extApiDefinitionMapper.selectApiDefinitionWithBlob(ids); + List moduleIds = list.stream().map(ApiDefinitionWithBlob::getModuleId).toList(); + ApiDefinitionModuleExample example = new ApiDefinitionModuleExample(); + example.createCriteria().andIdIn(moduleIds); + List definitionModules = apiDefinitionModuleMapper.selectByExample(example); + Map moduleMap = definitionModules.stream().collect(Collectors.toMap(ApiDefinitionModule::getId, ApiDefinitionModule::getName)); switch (type) { case "swagger": - return exportSwagger(request, list); + return exportSwagger(request, list, moduleMap); default: return new ApiExportResponse(); } } - private ApiExportResponse exportSwagger(ApiDefinitionBatchRequest request, List list) { + private ApiExportResponse exportSwagger(ApiDefinitionBatchRequest request, List list, Map moduleMap) { Project project = projectMapper.selectByPrimaryKey(request.getProjectId()); Swagger3ExportParser swagger3Parser = new Swagger3ExportParser(); try { - return swagger3Parser.parse(list, project); + return swagger3Parser.parse(list, project, moduleMap); } catch (Exception e) { throw new MSException(e); } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java index 4907f813c0..f5f65ec50e 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionControllerTests.java @@ -1886,14 +1886,10 @@ public class ApiDefinitionControllerTests extends BaseTest { request.setProjectId(DEFAULT_PROJECT_ID); request.setProtocols(List.of("HTTP")); request.setSelectAll(false); + request.setSelectIds(List.of("1002")); + this.requestPost(EXPORT + "swagger", request); request.setSelectIds(List.of("1001")); - MvcResult mvcResult = this.requestPostWithOkAndReturn(EXPORT + "swagger", request); - String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); - ResultHolder resultHolder = JSON.parseObject(returnData, ResultHolder.class); - // 返回请求正常 - Assertions.assertNotNull(resultHolder); - Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); - Assertions.assertNotNull(pageData); + this.requestPost(EXPORT + "swagger", request); } }