diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiPluginChangeService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiPluginChangeService.java index b9fa79d6bb..3ac2aedf90 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiPluginChangeService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiPluginChangeService.java @@ -14,6 +14,9 @@ import io.metersphere.system.service.PluginLoadService; import org.pf4j.Plugin; import org.springframework.stereotype.Service; +import java.util.ArrayList; +import java.util.List; + /** * @Author: jianxing * @CreateTime: 2024-02-06 20:55 @@ -26,9 +29,11 @@ public class ApiPluginChangeService implements PluginChangeService { Plugin plugin = msPluginManager.getPlugin(pluginId).getPlugin(); try { if (plugin instanceof AbstractApiPlugin) { - // 注册序列化类 - msPluginManager.getExtensionClasses(MsTestElement.class, pluginId) - .forEach(ApiDataUtils::setResolver); + // 注册序列化类,获取最新的实现类,重新注册 + List> extensionClasses = msPluginManager.getExtensionClasses(MsTestElement.class); + List> clazzList = new ArrayList<>(extensionClasses.size()); + extensionClasses.forEach(clazzList::add); + ApiDataUtils.setResolvers(clazzList); // 注册插件元素解析器 msPluginManager.getExtensionClasses(JmeterElementConverter.class, pluginId) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugService.java index 0827468a27..32901434d9 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/debug/ApiDebugService.java @@ -187,7 +187,7 @@ public class ApiDebugService { } private ApiDebug checkResourceExist(String id) { - return ServiceUtils.checkResourceExist(apiDebugMapper.selectByPrimaryKey(id), "permission.system_api_debug.name"); + return ServiceUtils.checkResourceExist(apiDebugMapper.selectByPrimaryKey(id), "permission.api_debug.name"); } public String uploadTempFile(MultipartFile file) { diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDataUtils.java b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDataUtils.java index ee0f523072..9998c9e78c 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDataUtils.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiDataUtils.java @@ -119,4 +119,13 @@ public class ApiDataUtils { // 加入新的动态组件 objectMapper.registerSubtypes(clazz); } + + /** + * 设置动态加载的jar的Resolver + */ + public static void setResolvers(List> clazzList) { + setResolver(new LinkedList<>()); + // 加入新的动态组件 + clazzList.forEach(objectMapper::registerSubtypes); + } } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java index d98147a81c..13058b6c5f 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDebugControllerTests.java @@ -268,7 +268,6 @@ public class ApiDebugControllerTests extends BaseTest { ApiDebug copyApiDebug = BeanUtils.copyBean(new ApiDebug(), apiDebug); copyApiDebug = BeanUtils.copyBean(copyApiDebug, request); Assertions.assertEquals(apiDebug, copyApiDebug); - ApiDataUtils.setResolver(MsHTTPElement.class); Assertions.assertEquals(msHttpElement, ApiDataUtils.parseObject(new String(apiDebugBlob.getRequest()), AbstractMsTestElement.class)); return apiDebug; } @@ -343,7 +342,6 @@ public class ApiDebugControllerTests extends BaseTest { // @@请求成功 MvcResult mvcResult = this.requestGetWithOk(DEFAULT_GET, addApiDebug.getId()) .andReturn(); - ApiDataUtils.setResolver(MsHTTPElement.class); ApiDebugDTO apiDebugDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ApiDebugDTO.class); // 校验数据是否正确 ApiDebugDTO copyApiDebugDTO = BeanUtils.copyBean(new ApiDebugDTO(), apiDebugMapper.selectByPrimaryKey(addApiDebug.getId())); 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 694cbaf11d..7fa67033d8 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 @@ -282,7 +282,6 @@ public class ApiDefinitionControllerTests extends BaseTest { ApiDefinition copyApiDefinition = BeanUtils.copyBean(new ApiDefinition(), apiDefinition); BeanUtils.copyBean(copyApiDefinition, request); Assertions.assertEquals(apiDefinition, copyApiDefinition); - ApiDataUtils.setResolver(MsHTTPElement.class); if (apiDefinitionBlob != null) { Assertions.assertEquals(msHttpElement, ApiDataUtils.parseObject(new String(apiDefinitionBlob.getRequest()), AbstractMsTestElement.class)); } @@ -298,7 +297,6 @@ public class ApiDefinitionControllerTests extends BaseTest { } // @@请求成功 MvcResult mvcResult = this.requestGetWithOkAndReturn(GET + apiDefinition.getId()); - ApiDataUtils.setResolver(MsHTTPElement.class); ApiDefinitionDTO apiDefinitionDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ApiDefinitionDTO.class); // 校验数据是否正确 ApiDefinitionDTO copyApiDefinitionDTO = BeanUtils.copyBean(new ApiDefinitionDTO(), apiDefinition); @@ -750,7 +748,6 @@ public class ApiDefinitionControllerTests extends BaseTest { ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey("1001"); // @@请求成功 MvcResult mvcResult = this.requestGetWithOk(VERSION + apiDefinition.getId()).andReturn(); - ApiDataUtils.setResolver(MsHTTPElement.class); List apiDefinitionVersionDTO = getResultDataArray(mvcResult, ApiDefinitionVersionDTO.class); // 校验数据是否正确 List copyApiDefinitionVersionDTO = extApiDefinitionMapper.getApiDefinitionByRefId(apiDefinition.getRefId()); @@ -885,7 +882,6 @@ public class ApiDefinitionControllerTests extends BaseTest { request.setType(ApiDefinitionDocType.API.name()); // @@请求成功 MvcResult mvcResult = this.requestPostWithOkAndReturn(DOC, request); - ApiDataUtils.setResolver(MsHTTPElement.class); ApiDefinitionDocDTO apiDefinitionDocDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ApiDefinitionDocDTO.class); // 校验数据是否正确 ApiDefinitionDocDTO copyApiDefinitionDocDTO = new ApiDefinitionDocDTO(); @@ -911,7 +907,6 @@ public class ApiDefinitionControllerTests extends BaseTest { request.setType(ApiDefinitionDocType.MODULE.name()); request.setModuleIds(List.of("10001")); MvcResult mvcResultModule = this.requestPostWithOkAndReturn(DOC, request); - ApiDataUtils.setResolver(MsHTTPElement.class); ApiDefinitionDocDTO moduleApiDefinitionDocDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultModule).get("data")), ApiDefinitionDocDTO.class); // 校验数据是否正确 ApiDefinitionDocDTO copyModuleApiDefinitionDocDTO = new ApiDefinitionDocDTO(); @@ -943,7 +938,6 @@ public class ApiDefinitionControllerTests extends BaseTest { request.setProjectId(DEFAULT_PROJECT_ID); 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); // 校验数据是否正确 ApiDefinitionDocDTO copyAllApiDefinitionDocDTO = new ApiDefinitionDocDTO(); @@ -980,7 +974,6 @@ public class ApiDefinitionControllerTests extends BaseTest { request.setType(ApiDefinitionDocType.MODULE.name()); request.setModuleIds(List.of("1001001")); MvcResult mvcResultModule = this.requestPostWithOkAndReturn(DOC, request); - ApiDataUtils.setResolver(MsHTTPElement.class); ApiDefinitionDocDTO moduleApiDefinitionDocDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultModule).get("data")), ApiDefinitionDocDTO.class); // 校验数据是否正确 ApiDefinitionDocDTO copyModuleApiDefinitionDocDTO = new ApiDefinitionDocDTO(); @@ -1131,7 +1124,6 @@ public class ApiDefinitionControllerTests extends BaseTest { apiDefinitionDeleteRequest.setId("1004"); ApiDefinition delApiDefinition = apiDefinitionMapper.selectByPrimaryKey(apiDefinitionDeleteRequest.getId()); MvcResult mvcResult = this.requestGetWithOk(VERSION + apiDefinitionDeleteRequest.getId()).andReturn(); - ApiDataUtils.setResolver(MsHTTPElement.class); List apiDefinitionVersionDTO = getResultDataArray(mvcResult, ApiDefinitionVersionDTO.class); if (!apiDefinitionVersionDTO.isEmpty()) { this.requestPostWithOkAndReturn(DELETE, apiDefinitionDeleteRequest); diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionMockControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionMockControllerTests.java index d8af1fb935..2714daf9b5 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionMockControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiDefinitionMockControllerTests.java @@ -241,7 +241,6 @@ public class ApiDefinitionMockControllerTests extends BaseTest { ApiDefinitionMock copyApiDefinitionMock = BeanUtils.copyBean(new ApiDefinitionMock(), apiDefinitionMock); BeanUtils.copyBean(copyApiDefinitionMock, request); Assertions.assertEquals(apiDefinitionMock, copyApiDefinitionMock); - ApiDataUtils.setResolver(MsHTTPElement.class); if(apiDefinitionMockConfig != null){ Assertions.assertEquals(msHttpElement, ApiDataUtils.parseObject(new String(apiDefinitionMockConfig.getMatching()), AbstractMsTestElement.class)); } @@ -257,7 +256,6 @@ public class ApiDefinitionMockControllerTests extends BaseTest { apiDefinitionMockRequest.setApiDefinitionId(apiDefinitionMock.getApiDefinitionId()); // @@请求成功 MvcResult mvcResult = this.requestPostWithOkAndReturn(DETAIL, apiDefinitionMockRequest); - ApiDataUtils.setResolver(MsHTTPElement.class); ApiDefinitionMockDTO apiDefinitionMockDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ApiDefinitionMockDTO.class); // 校验数据是否正确 ApiDefinitionMockDTO copyApiDefinitionMockDTO = BeanUtils.copyBean(new ApiDefinitionMockDTO(), apiDefinitionMock); diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiShareControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiShareControllerTests.java index eeba26a8f9..b88209a7b3 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiShareControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiShareControllerTests.java @@ -9,7 +9,6 @@ import io.metersphere.api.dto.definition.ApiDefinitionDTO; import io.metersphere.api.dto.definition.ApiDefinitionDocDTO; import io.metersphere.api.dto.definition.ApiDefinitionDocRequest; import io.metersphere.api.dto.definition.HttpResponse; -import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.api.dto.share.ShareInfoDTO; import io.metersphere.api.mapper.ApiDefinitionBlobMapper; import io.metersphere.api.mapper.ApiDefinitionMapper; @@ -75,7 +74,6 @@ public class ApiShareControllerTests extends BaseTest { // @@请求成功 this.requestPostWithOkAndReturn(SHARE_DOC, request); MvcResult mvcResult = this.requestPostWithOkAndReturn(SHARE_DOC, request); - ApiDataUtils.setResolver(MsHTTPElement.class); ShareInfoDTO shareInfoDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ShareInfoDTO.class); // 校验数据是否正确 List shareInfos = extShareInfoMapper.selectByShareTypeAndShareApiIdWithBLOBs(ShareInfoType.SINGLE.name(), JSON.toJSONString(request).getBytes(), "zh_CN"); @@ -93,7 +91,6 @@ public class ApiShareControllerTests extends BaseTest { request.setType(ApiDefinitionDocType.MODULE.name()); request.setModuleIds(List.of("10001")); MvcResult mvcResultModule = this.requestPostWithOkAndReturn(SHARE_DOC, request); - ApiDataUtils.setResolver(MsHTTPElement.class); ShareInfoDTO shareInfoDTOModule = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultModule).get("data")), ShareInfoDTO.class); // 校验数据是否正确 List shareInfosModule = extShareInfoMapper.selectByShareTypeAndShareApiIdWithBLOBs(ShareInfoType.BATCH.name(), JSON.toJSONString(request).getBytes(), "zh_CN"); @@ -108,7 +105,6 @@ public class ApiShareControllerTests extends BaseTest { request.setProjectId(DEFAULT_PROJECT_ID); request.setType(ApiDefinitionDocType.ALL.name()); MvcResult mvcResultAll = this.requestPostWithOkAndReturn(SHARE_DOC, request); - ApiDataUtils.setResolver(MsHTTPElement.class); ShareInfoDTO allShareInfoDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultAll).get("data")), ShareInfoDTO.class); // 校验数据是否正确 @@ -129,7 +125,6 @@ public class ApiShareControllerTests extends BaseTest { ShareInfo shareInfo = shareInfoMapper.selectByPrimaryKey(shareId); ApiDefinitionDocRequest apiDefinitionDocRequest = ApiDataUtils.parseObject(new String(shareInfo.getCustomData()), ApiDefinitionDocRequest.class); MvcResult mvcResultAll = this.requestGetWithOkAndReturn(SHARE_VIEW + shareId); - ApiDataUtils.setResolver(MsHTTPElement.class); ApiDefinitionDocDTO allApiDefinitionDocDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResultAll).get("data")), ApiDefinitionDocDTO.class); // 校验数据是否正确 ApiDefinitionDocDTO copyAllApiDefinitionDocDTO = new ApiDefinitionDocDTO(); diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java index 2202ccd03d..3e0938a650 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiTestCaseControllerTests.java @@ -296,7 +296,6 @@ public class ApiTestCaseControllerTests extends BaseTest { ApiTestCase copyApiDebug = BeanUtils.copyBean(new ApiTestCase(), apiCase); BeanUtils.copyBean(copyApiDebug, request); Assertions.assertEquals(apiCase, copyApiDebug); - ApiDataUtils.setResolver(MsHTTPElement.class); Assertions.assertEquals(msHttpElement, ApiDataUtils.parseObject(new String(apiTestCaseBlob.getRequest()), AbstractMsTestElement.class)); return apiCase; } @@ -384,7 +383,6 @@ public class ApiTestCaseControllerTests extends BaseTest { // @@请求成功 MvcResult mvcResult = this.requestGetWithOk(GET + apiTestCase.getId()) .andReturn(); - ApiDataUtils.setResolver(MsHTTPElement.class); ApiTestCaseDTO apiDebugDTO = ApiDataUtils.parseObject(JSON.toJSONString(parseResponse(mvcResult).get("data")), ApiTestCaseDTO.class); // 校验数据是否正确 ApiTestCase testCase = apiTestCaseMapper.selectByPrimaryKey(apiTestCase.getId()); diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java index a13a58cf22..121b61dfca 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/MsHTTPElementTest.java @@ -44,9 +44,6 @@ import java.util.List; * @CreateTime: 2023-11-07 11:17 */ public class MsHTTPElementTest { - public MsHTTPElementTest() { - ApiDataUtils.setResolver(MsHTTPElement.class); - } @Test public void bodyTest() { diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java index d52f4110cb..7e6d934027 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/PluginSubTypeTests.java @@ -2,7 +2,6 @@ package io.metersphere.api.controller; import com.fasterxml.jackson.databind.jsontype.NamedType; import io.metersphere.api.dto.request.controller.MsLoopController; -import io.metersphere.api.dto.request.http.MsHTTPElement; import io.metersphere.api.utils.ApiDataUtils; import io.metersphere.plugin.api.dto.TestElementDTO; import io.metersphere.plugin.api.spi.AbstractMsTestElement; @@ -15,6 +14,7 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMock import org.springframework.boot.test.context.SpringBootTest; import java.io.InputStream; +import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.function.Consumer; @@ -30,7 +30,6 @@ public class PluginSubTypeTests { @Resource private PluginLoadService pluginLoadService; - @Test @Order(0) public void resolverTest() throws Exception { @@ -56,18 +55,21 @@ public class PluginSubTypeTests { public void jdbcPluginSubTypeTest() throws Exception { // 上传 jdbc 插件 baseApiPluginTestService.addJdbcPlugin(); - List> extensionClasses = - pluginLoadService.getMsPluginManager().getExtensionClasses(MsTestElement.class); - - // 注册序列化类 - extensionClasses.forEach(ApiDataUtils::setResolver); - String jdbcJson = """ { "polymorphicName": "MsJDBCElement", "test": "测试MsJDBCElement" } """; + + List> extensionClasses = + pluginLoadService.getMsPluginManager().getExtensionClasses(MsTestElement.class); + + // 注册序列化类 + List> clazzList = new ArrayList<>(extensionClasses.size()); + extensionClasses.forEach(clazzList::add); + ApiDataUtils.setResolvers(clazzList); + AbstractMsTestElement testElementDTO = ApiDataUtils.parseObject(jdbcJson, AbstractMsTestElement.class); Assertions.assertNotNull(testElementDTO); } @@ -82,8 +84,6 @@ public class PluginSubTypeTests { })); Assertions.assertTrue(isFuncSuccess((v) -> ApiDataUtils.parseObject("{"))); Assertions.assertTrue(isFuncSuccess((v) -> ApiDataUtils.parseArray(null, AbstractMsTestElement.class))); - - ApiDataUtils.setResolver(MsHTTPElement.class); // 检验 parseArray String msHttpJson = """ [{