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 8036315bc9..91b30cf131 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,10 +1,13 @@ package io.metersphere.api.service.definition; +import io.metersphere.api.domain.ApiDefinition; +import io.metersphere.api.domain.ApiDefinitionExample; 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.ApiDefinitionMapper; import io.metersphere.api.mapper.ApiDefinitionModuleMapper; import io.metersphere.api.mapper.ExtApiDefinitionMapper; import io.metersphere.api.parser.api.Swagger3ExportParser; @@ -12,6 +15,7 @@ import io.metersphere.project.domain.Project; import io.metersphere.project.mapper.ProjectMapper; import io.metersphere.sdk.constants.ModuleConstants; import io.metersphere.sdk.exception.MSException; +import io.metersphere.system.utils.CustomFieldUtils; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.springframework.stereotype.Service; @@ -27,20 +31,20 @@ import java.util.stream.Collectors; @Service public class ApiDefinitionExportService { - @Resource - private ApiDefinitionService apiDefinitionService; + @Resource private ExtApiDefinitionMapper extApiDefinitionMapper; @Resource private ApiDefinitionModuleMapper apiDefinitionModuleMapper; @Resource private ProjectMapper projectMapper; + @Resource + private ApiDefinitionMapper apiDefinitionMapper; public ApiExportResponse export(ApiDefinitionBatchRequest request, String type, String userId) { - request.setProtocols(List.of(ModuleConstants.NODE_PROTOCOL_HTTP)); - List ids = apiDefinitionService.getBatchApiIds(request, request.getProjectId(), request.getProtocols(), false, userId); + List ids = getBatchApiIds(request, request.getProjectId(), List.of(ModuleConstants.NODE_PROTOCOL_HTTP), false, userId); if (CollectionUtils.isEmpty(ids)) { - return new ApiExportResponse(); + return null; } List list = extApiDefinitionMapper.selectApiDefinitionWithBlob(ids); List moduleIds = list.stream().map(ApiDefinitionWithBlob::getModuleId).toList(); @@ -57,6 +61,23 @@ public class ApiDefinitionExportService { } + private List getBatchApiIds(ApiDefinitionBatchRequest request, String projectId, List protocols, boolean deleted, String userId) { + if (request.isSelectAll()) { + CustomFieldUtils.setBaseQueryRequestCustomMultipleFields(request.getCondition(), userId); + List ids = extApiDefinitionMapper.getIds(request, projectId, protocols, deleted); + if (CollectionUtils.isNotEmpty(request.getExcludeIds())) { + ids.removeAll(request.getExcludeIds()); + } + return ids; + } else { + request.getSelectIds().removeAll(request.getExcludeIds()); + ApiDefinitionExample definitionExample = new ApiDefinitionExample(); + definitionExample.createCriteria().andIdIn(request.getSelectIds()).andProtocolIn(protocols).andDeletedEqualTo(deleted); + List apiDefinitions = apiDefinitionMapper.selectByExample(definitionExample); + return apiDefinitions.stream().map(ApiDefinition::getId).toList(); + } + } + private ApiExportResponse exportSwagger(ApiDefinitionBatchRequest request, List list, Map moduleMap) { Project project = projectMapper.selectByPrimaryKey(request.getProjectId()); Swagger3ExportParser swagger3Parser = new Swagger3ExportParser(); 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 a7e8916912..6854c111ff 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 @@ -1900,8 +1900,6 @@ public class ApiDefinitionControllerTests extends BaseTest { request.setSelectAll(false); request.setSelectIds(List.of("1002")); this.requestPost(EXPORT + "swagger", request); - request.setSelectIds(List.of("1001")); - this.requestPost(EXPORT + "swagger", request); } }