From 19ba5fc71485d417f281828f8074a8ff5945fc8f Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 31 Aug 2023 09:49:31 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E5=A4=8D=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plugin/platform/api/AbstractPlatform.java | 4 ++-- .../io/metersphere/plugin/platform/api/BaseClient.java | 6 +++--- .../plugin/sdk/util/{JSON.java => JSONUtils.java} | 7 ++++++- .../sdk/src/main/java/io/metersphere/sdk/util/JSON.java | 9 +++++++++ .../controller/ServiceIntegrationControllerTests.java | 2 +- 5 files changed, 21 insertions(+), 7 deletions(-) rename backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/{JSON.java => JSONUtils.java} (91%) diff --git a/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/AbstractPlatform.java b/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/AbstractPlatform.java index d62cef52a0..c6b5aeef7c 100644 --- a/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/AbstractPlatform.java +++ b/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/AbstractPlatform.java @@ -1,7 +1,7 @@ package io.metersphere.plugin.platform.api; import io.metersphere.plugin.platform.dto.PlatformRequest; -import io.metersphere.plugin.sdk.util.JSON; +import io.metersphere.plugin.sdk.util.JSONUtils; import io.metersphere.plugin.sdk.util.MSPluginException; import org.apache.commons.lang3.StringUtils; @@ -16,7 +16,7 @@ public abstract class AbstractPlatform implements Platform { if (StringUtils.isBlank(integrationConfig)) { throw new MSPluginException("服务集成配置为空"); } - return JSON.parseObject(integrationConfig, clazz); + return JSONUtils.parseObject(integrationConfig, clazz); } public String getPluginId() { diff --git a/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/BaseClient.java b/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/BaseClient.java index a1d652bddd..253220a21d 100644 --- a/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/BaseClient.java +++ b/backend/framework/plugin/metersphere-platform-plugin-sdk/src/main/java/io/metersphere/plugin/platform/api/BaseClient.java @@ -2,7 +2,7 @@ package io.metersphere.plugin.platform.api; import io.metersphere.plugin.platform.utils.EnvProxySelector; import io.metersphere.plugin.platform.utils.PluginCodingUtils; -import io.metersphere.plugin.sdk.util.JSON; +import io.metersphere.plugin.sdk.util.JSONUtils; import io.metersphere.plugin.sdk.util.MSPluginException; import io.metersphere.plugin.sdk.util.PluginLogUtils; import org.apache.commons.lang3.StringUtils; @@ -87,11 +87,11 @@ public abstract class BaseClient { } protected Object getResultForList(Class clazz, ResponseEntity response) { - return Arrays.asList(JSON.parseArray(getResult(response), clazz).toArray()); + return Arrays.asList(JSONUtils.parseArray(getResult(response), clazz).toArray()); } protected Object getResultForObject(Class clazz, ResponseEntity response) { - return JSON.parseObject(getResult(response), clazz); + return JSONUtils.parseObject(getResult(response), clazz); } public void validateProxyUrl(String url, String... path) { diff --git a/backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/JSON.java b/backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/JSONUtils.java similarity index 91% rename from backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/JSON.java rename to backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/JSONUtils.java index 42c084595f..c42c8a2593 100644 --- a/backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/JSON.java +++ b/backend/framework/plugin/metersphere-plugin-sdk/src/main/java/io/metersphere/plugin/sdk/util/JSONUtils.java @@ -3,6 +3,7 @@ package io.metersphere.plugin.sdk.util; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.StreamReadConstraints; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; @@ -16,9 +17,10 @@ import java.io.InputStream; import java.util.List; import java.util.Map; -public class JSON { +public class JSONUtils { private static final ObjectMapper objectMapper = new ObjectMapper(); private static final TypeFactory typeFactory = objectMapper.getTypeFactory(); + public static final int DEFAULT_MAX_STRING_LEN = 20_000_000_0; static { objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); @@ -27,6 +29,9 @@ public class JSON { // 如果一个对象中没有任何的属性,那么在序列化的时候就会报错 objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + // 设置JSON处理字符长度限制 + objectMapper.getFactory() + .setStreamReadConstraints(StreamReadConstraints.builder().maxStringLength(DEFAULT_MAX_STRING_LEN).build()); } public static String toJSONString(Object value) { diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java index 31c8d72355..b4d5d77bc6 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/util/JSON.java @@ -3,6 +3,7 @@ package io.metersphere.sdk.util; import com.fasterxml.jackson.annotation.JsonAutoDetect; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.core.StreamReadConstraints; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.DeserializationFeature; import com.fasterxml.jackson.databind.JavaType; @@ -10,6 +11,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.databind.type.TypeFactory; +import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule; import java.io.IOException; import java.io.InputStream; @@ -19,6 +21,7 @@ import java.util.Map; public class JSON { private static final ObjectMapper objectMapper = new ObjectMapper(); private static final TypeFactory typeFactory = objectMapper.getTypeFactory(); + public static final int DEFAULT_MAX_STRING_LEN = 20_000_000_0; static { objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); @@ -27,6 +30,12 @@ public class JSON { // 如果一个对象中没有任何的属性,那么在序列化的时候就会报错 objectMapper.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS); objectMapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true); + // 设置JSON处理字符长度限制 + objectMapper.getFactory() + .setStreamReadConstraints(StreamReadConstraints.builder().maxStringLength(DEFAULT_MAX_STRING_LEN).build()); + + // 处理时间格式 + objectMapper.registerModule(new JavaTimeModule()); } public static String toJSONString(Object value) { diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/ServiceIntegrationControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/ServiceIntegrationControllerTests.java index 6b88c0d006..474b415771 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/ServiceIntegrationControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/ServiceIntegrationControllerTests.java @@ -1,11 +1,11 @@ package io.metersphere.system.controller; import io.metersphere.plugin.platform.api.AbstractPlatformPlugin; -import io.metersphere.plugin.sdk.util.JSON; import io.metersphere.sdk.base.BaseTest; import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.log.constants.OperationLogType; import io.metersphere.sdk.service.PluginLoadService; +import io.metersphere.sdk.util.JSON; import io.metersphere.system.controller.param.ServiceIntegrationUpdateRequestDefinition; import io.metersphere.system.domain.Organization; import io.metersphere.system.domain.Plugin;