fix(接口测试): 接口定义root节点模块导致导出数据为空问题

--bug=1043951 --user=王旭 【接口测试】接口定义-选中未规划模块导出接口文件内容为空 https://www.tapd.cn/55049933/s/1548295
This commit is contained in:
WangXu10 2024-07-15 16:55:03 +08:00 committed by Craftsman
parent 1609701cb1
commit 46fcef3d72
6 changed files with 35 additions and 18 deletions

View File

@ -76,6 +76,4 @@ public class ApiDefinitionWithBlob extends ApiDefinitionBlob {
@Schema(description = "删除状态", requiredMode = Schema.RequiredMode.REQUIRED)
private Boolean deleted;
@Schema(description = "模块名称")
private String moduleName;
}

View File

@ -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
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}

View File

@ -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> {
T parse(List<ApiDefinitionWithBlob> list, Project project) throws Exception;
T parse(List<ApiDefinitionWithBlob> list, Project project, Map<String, String> moduleMap) throws Exception;
}

View File

@ -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<ApiExportResponse> {
@Override
public ApiExportResponse parse(List<ApiDefinitionWithBlob> list, Project project) throws Exception {
public ApiExportResponse parse(List<ApiDefinitionWithBlob> list, Project project, Map<String, String> moduleMap) throws Exception {
SwaggerApiExportResponse response = new SwaggerApiExportResponse();
//openapi
response.setOpenapi("3.0.2");
@ -49,7 +51,13 @@ public class Swagger3ExportParser implements ExportParser<ApiExportResponse> {
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);

View File

@ -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<String> ids = apiDefinitionService.getBatchApiIds(request, request.getProjectId(), request.getProtocols(), false, userId);
if (CollectionUtils.isEmpty(ids)) {
return new ApiExportResponse();
}
List<ApiDefinitionWithBlob> list = extApiDefinitionMapper.selectApiDefinitionWithBlob(ids);
List<String> moduleIds = list.stream().map(ApiDefinitionWithBlob::getModuleId).toList();
ApiDefinitionModuleExample example = new ApiDefinitionModuleExample();
example.createCriteria().andIdIn(moduleIds);
List<ApiDefinitionModule> definitionModules = apiDefinitionModuleMapper.selectByExample(example);
Map<String, String> 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<ApiDefinitionWithBlob> list) {
private ApiExportResponse exportSwagger(ApiDefinitionBatchRequest request, List<ApiDefinitionWithBlob> list, Map<String, String> 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);
}

View File

@ -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);
}
}