diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionDocDTO.java b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionDocDTO.java index 6db974a59f..569fcd9264 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionDocDTO.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/dto/definition/ApiDefinitionDocDTO.java @@ -3,8 +3,6 @@ package io.metersphere.api.dto.definition; import io.swagger.v3.oas.annotations.media.Schema; import lombok.Data; -import java.util.List; - /** * @author lan */ @@ -18,6 +16,6 @@ public class ApiDefinitionDocDTO{ private String type; @Schema(description = "接口文档内容") - List docList; + ApiDefinitionDTO docInfo; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/enums/ApiDefinitionDocType.java b/backend/services/api-test/src/main/java/io/metersphere/api/enums/ApiDefinitionDocType.java new file mode 100644 index 0000000000..2c4df311af --- /dev/null +++ b/backend/services/api-test/src/main/java/io/metersphere/api/enums/ApiDefinitionDocType.java @@ -0,0 +1,10 @@ +package io.metersphere.api.enums; + +/** + * @author: LAN + * @date: 2023/11/16 10:42 + * @version: 1.0 + */ +public enum ApiDefinitionDocType { + ALL, MODULE, API +} diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java index 8ab8e0c4f9..5248869f88 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionLogService.java @@ -364,8 +364,8 @@ public class ApiDefinitionLogService { if(null != apiDefinition){ // 2. 使用Optional避免空指针异常 apiDefinitionService.handleBlob(id, apiDefinitionDTO); + BeanUtils.copyBean(apiDefinitionDTO, apiDefinition); } - BeanUtils.copyBean(apiDefinitionDTO, apiDefinition); return apiDefinitionDTO; } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java index 9383c0a92a..73142e1d96 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/definition/ApiDefinitionService.java @@ -5,6 +5,7 @@ import io.metersphere.api.controller.result.ApiResultCode; import io.metersphere.api.domain.*; import io.metersphere.api.dto.debug.ApiFileResourceUpdateRequest; import io.metersphere.api.dto.definition.*; +import io.metersphere.api.enums.ApiDefinitionDocType; import io.metersphere.api.enums.ApiReportStatus; import io.metersphere.api.mapper.*; import io.metersphere.api.service.ApiFileResourceService; @@ -671,29 +672,30 @@ public class ApiDefinitionService { ApiDefinitionDocDTO apiDefinitionDocDTO = new ApiDefinitionDocDTO(); apiDefinitionDocDTO.setType(request.getType()); // @@TODO 下载所有/一个模块接口文档时,不做分页数据量大的时候会不会有性能问题 - if ("ALL".equals(request.getType()) || "MODULE".equals(request.getType())) { + if (ApiDefinitionDocType.ALL.name().equals(request.getType()) || ApiDefinitionDocType.MODULE.name().equals(request.getType())) { List list = extApiDefinitionMapper.listDoc(request); - if(null != list){ - list.forEach(item-> handleBlob(item.getId(), item)); - apiDefinitionDocDTO.setDocTitle("ALL".equals(request.getType()) ? Translator.get(ALL_API) : getModuleTitle(list)); - apiDefinitionDocDTO.setDocList(list); + if (!list.isEmpty()) { + ApiDefinitionDTO first = list.get(0); + handleBlob(first.getId(), first); + if(ApiDefinitionDocType.ALL.name().equals(request.getType())){ + apiDefinitionDocDTO.setDocTitle(Translator.get(ALL_API)); + } else { + ApiDefinitionModule apiDefinitionModule = apiDefinitionModuleMapper.selectByPrimaryKey(first.getModuleId()); + if (StringUtils.isNotBlank(apiDefinitionModule.getName())) { + apiDefinitionDocDTO.setDocTitle(apiDefinitionModule.getName()); + } else { + throw new MSException(API_DEFINITION_MODULE_NOT_EXIST); + } + } + apiDefinitionDocDTO.setDocInfo(first); } - } else if ("API".equals(request.getType())) { + } else if (ApiDefinitionDocType.API.name().equals(request.getType())) { ApiDefinitionDTO apiDefinitionDTO = get(request.getApiId(), userId); apiDefinitionDocDTO.setDocTitle(apiDefinitionDTO.getName()); - apiDefinitionDocDTO.setDocList(Collections.singletonList(apiDefinitionDTO)); + apiDefinitionDocDTO.setDocInfo(apiDefinitionDTO); } return apiDefinitionDocDTO; } - private String getModuleTitle(List list) { - ApiDefinitionDTO first = list.stream().findFirst().orElseThrow(() -> new MSException(API_DEFINITION_NOT_EXIST)); - ApiDefinitionModule apiDefinitionModule = apiDefinitionModuleMapper.selectByPrimaryKey(first.getModuleId()); - if (StringUtils.isNotBlank(apiDefinitionModule.getName())) { - return apiDefinitionModule.getName(); - } else { - throw new MSException(API_DEFINITION_MODULE_NOT_EXIST); - } - } } 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 09cd02f30c..c81cb76387 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 @@ -3,9 +3,11 @@ package io.metersphere.api.controller; import io.metersphere.api.controller.result.ApiResultCode; import io.metersphere.api.domain.*; import io.metersphere.api.dto.definition.*; +import io.metersphere.api.enums.ApiDefinitionDocType; import io.metersphere.api.enums.ApiDefinitionStatus; import io.metersphere.api.mapper.*; import io.metersphere.api.service.ApiFileResourceService; +import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.ApiDataUtils; import io.metersphere.plugin.api.spi.AbstractMsTestElement; import io.metersphere.project.dto.filemanagement.FileInfo; @@ -39,6 +41,7 @@ import org.springframework.test.web.servlet.MvcResult; import java.nio.charset.StandardCharsets; import java.util.*; +import static io.metersphere.api.controller.result.ApiResultCode.API_DEFINITION_NOT_EXIST; import static io.metersphere.system.controller.handler.result.MsHttpResultCode.NOT_FOUND; @@ -681,7 +684,7 @@ public class ApiDefinitionControllerTests extends BaseTest { apiDefinition = apiDefinitionMapper.selectByPrimaryKey("1001"); request.setApiId(apiDefinition.getId()); request.setProjectId(DEFAULT_PROJECT_ID); - request.setType("API"); + request.setType(ApiDefinitionDocType.API.name()); // @@请求成功 MvcResult mvcResult = this.requestPostWithOkAndReturn(DOC, request); ApiDataUtils.setResolver(MsHTTPElement.class); @@ -695,11 +698,11 @@ public class ApiDefinitionControllerTests extends BaseTest { copyApiDefinitionDTO.setResponse(ApiDataUtils.parseArray(new String(apiDefinitionBlob.getResponse()), HttpResponse.class)); } copyApiDefinitionDocDTO.setDocTitle(apiDefinition.getName()); - copyApiDefinitionDocDTO.setType("API"); - copyApiDefinitionDocDTO.setDocList(Collections.singletonList(copyApiDefinitionDTO)); + copyApiDefinitionDocDTO.setType(ApiDefinitionDocType.API.name()); + copyApiDefinitionDocDTO.setDocInfo(copyApiDefinitionDTO); Assertions.assertEquals(apiDefinitionDocDTO.getType(), copyApiDefinitionDocDTO.getType()); Assertions.assertEquals(apiDefinitionDocDTO.getDocTitle(), copyApiDefinitionDocDTO.getDocTitle()); - Assertions.assertEquals(apiDefinitionDocDTO.getDocList().size(), copyApiDefinitionDocDTO.getDocList().size()); + Assertions.assertEquals(apiDefinitionDocDTO.getDocInfo().getId(), copyApiDefinitionDocDTO.getDocInfo().getId()); request.setApiId("111"); assertErrorCode(this.requestPost(DOC, request), ApiResultCode.API_DEFINITION_NOT_EXIST); @@ -707,7 +710,7 @@ public class ApiDefinitionControllerTests extends BaseTest { // @@模块查看文档 request.setApiId(null); request.setProjectId(DEFAULT_PROJECT_ID); - request.setType("MODULE"); + request.setType(ApiDefinitionDocType.MODULE.name()); request.setModuleIds(List.of("10001")); MvcResult mvcResultModule = this.requestPostWithOkAndReturn(DOC, request); ApiDataUtils.setResolver(MsHTTPElement.class); @@ -716,30 +719,29 @@ public class ApiDefinitionControllerTests extends BaseTest { ApiDefinitionDocDTO copyModuleApiDefinitionDocDTO = new ApiDefinitionDocDTO(); List list = extApiDefinitionMapper.listDoc(request); if(null != list){ - list.forEach(item-> { - ApiDefinitionBlob moduleApiDefinitionBlob = apiDefinitionBlobMapper.selectByPrimaryKey(item.getId()); - if(moduleApiDefinitionBlob != null){ - item.setRequest(ApiDataUtils.parseObject(new String(moduleApiDefinitionBlob.getRequest()), AbstractMsTestElement.class)); - item.setResponse(ApiDataUtils.parseArray(new String(moduleApiDefinitionBlob.getResponse()), HttpResponse.class)); - } - ApiDefinitionModule apiDefinitionModule = apiDefinitionModuleMapper.selectByPrimaryKey(item.getModuleId()); - if(StringUtils.isBlank(copyModuleApiDefinitionDocDTO.getDocTitle())){ - copyModuleApiDefinitionDocDTO.setDocTitle(apiDefinitionModule.getName()); - } - }); + ApiDefinitionDTO first = list.stream().findFirst().orElseThrow(() -> new MSException(API_DEFINITION_NOT_EXIST)); + ApiDefinitionBlob moduleApiDefinitionBlob = apiDefinitionBlobMapper.selectByPrimaryKey(first.getId()); + if(moduleApiDefinitionBlob != null){ + first.setRequest(ApiDataUtils.parseObject(new String(moduleApiDefinitionBlob.getRequest()), AbstractMsTestElement.class)); + first.setResponse(ApiDataUtils.parseArray(new String(moduleApiDefinitionBlob.getResponse()), HttpResponse.class)); + } + ApiDefinitionModule apiDefinitionModule = apiDefinitionModuleMapper.selectByPrimaryKey(first.getModuleId()); + if(StringUtils.isBlank(copyModuleApiDefinitionDocDTO.getDocTitle())){ + copyModuleApiDefinitionDocDTO.setDocTitle(apiDefinitionModule.getName()); + } + copyModuleApiDefinitionDocDTO.setDocInfo(first); + copyModuleApiDefinitionDocDTO.setType(ApiDefinitionDocType.MODULE.name()); } - copyModuleApiDefinitionDocDTO.setType("MODULE"); - copyModuleApiDefinitionDocDTO.setDocList(list); Assertions.assertEquals(moduleApiDefinitionDocDTO.getType(), copyModuleApiDefinitionDocDTO.getType()); Assertions.assertEquals(moduleApiDefinitionDocDTO.getDocTitle(), copyModuleApiDefinitionDocDTO.getDocTitle()); - Assertions.assertEquals(moduleApiDefinitionDocDTO.getDocList().size(), copyModuleApiDefinitionDocDTO.getDocList().size()); + Assertions.assertEquals(moduleApiDefinitionDocDTO.getDocInfo().getId(), copyModuleApiDefinitionDocDTO.getDocInfo().getId()); // @@查看全部文档 request.setApiId(null); request.setModuleIds(null); request.setProjectId(DEFAULT_PROJECT_ID); - request.setType("ALL"); + request.setType(ApiDefinitionDocType.ALL.name()); MvcResult mvcResultAll = this.requestPostWithOkAndReturn(DOC, request); ApiDataUtils.setResolver(MsHTTPElement.class); ApiDefinitionDocDTO allApiDefinitionDocDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultAll).get("data")), ApiDefinitionDocDTO.class); @@ -747,22 +749,22 @@ public class ApiDefinitionControllerTests extends BaseTest { ApiDefinitionDocDTO copyAllApiDefinitionDocDTO = new ApiDefinitionDocDTO(); List allList = extApiDefinitionMapper.listDoc(request); if(null != allList){ - allList.forEach(item-> { - ApiDefinitionBlob allApiDefinitionBlob = apiDefinitionBlobMapper.selectByPrimaryKey(item.getId()); - if(allApiDefinitionBlob != null){ - item.setRequest(ApiDataUtils.parseObject(new String(allApiDefinitionBlob.getRequest()), AbstractMsTestElement.class)); - item.setResponse(ApiDataUtils.parseArray(new String(allApiDefinitionBlob.getResponse()), HttpResponse.class)); - } - if(StringUtils.isBlank(copyAllApiDefinitionDocDTO.getDocTitle())){ - copyAllApiDefinitionDocDTO.setDocTitle(Translator.get(ALL_API)); - } - }); + ApiDefinitionDTO info = allList.stream().findFirst().orElseThrow(() -> new MSException(API_DEFINITION_NOT_EXIST)); + ApiDefinitionBlob allApiDefinitionBlob = apiDefinitionBlobMapper.selectByPrimaryKey(info.getId()); + if(allApiDefinitionBlob != null){ + info.setRequest(ApiDataUtils.parseObject(new String(allApiDefinitionBlob.getRequest()), AbstractMsTestElement.class)); + info.setResponse(ApiDataUtils.parseArray(new String(allApiDefinitionBlob.getResponse()), HttpResponse.class)); + } + if(StringUtils.isBlank(copyAllApiDefinitionDocDTO.getDocTitle())){ + copyAllApiDefinitionDocDTO.setDocTitle(Translator.get(ALL_API)); + } + copyAllApiDefinitionDocDTO.setType(ApiDefinitionDocType.ALL.name()); + copyAllApiDefinitionDocDTO.setDocInfo(info); } - copyAllApiDefinitionDocDTO.setType("ALL"); - copyAllApiDefinitionDocDTO.setDocList(allList); + Assertions.assertEquals(allApiDefinitionDocDTO.getType(), copyAllApiDefinitionDocDTO.getType()); Assertions.assertEquals(allApiDefinitionDocDTO.getDocTitle(), copyAllApiDefinitionDocDTO.getDocTitle()); - Assertions.assertEquals(allApiDefinitionDocDTO.getDocList().size(), copyAllApiDefinitionDocDTO.getDocList().size()); + Assertions.assertEquals(allApiDefinitionDocDTO.getDocInfo().getId(), copyAllApiDefinitionDocDTO.getDocInfo().getId()); // @@校验权限 requestPostPermissionTest(PermissionConstants.PROJECT_API_DEFINITION_READ, DOC, request);