You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@seatunnel.apache.org by do...@apache.org on 2026/04/26 14:57:32 UTC

(seatunnel) branch dev updated: [Fix][Core] Fix JsonUtils.parseObject encoding issue causing UTF-8 pa… (#10809)

This is an automated email from the ASF dual-hosted git repository.

dybyte pushed a commit to branch dev
in repository https://gitbox.apache.org/repos/asf/seatunnel.git


The following commit(s) were added to refs/heads/dev by this push:
     new a1e6a8cc98 [Fix][Core] Fix JsonUtils.parseObject encoding issue causing UTF-8 pa… (#10809)
a1e6a8cc98 is described below

commit a1e6a8cc98c6b03332c536a45b1e4cfee6a613bc
Author: cosmosni <ni...@cmss.chinamobile.com>
AuthorDate: Sun Apr 26 22:57:26 2026 +0800

    [Fix][Core] Fix JsonUtils.parseObject encoding issue causing UTF-8 pa… (#10809)
---
 .../apache/seatunnel/common/utils/JsonUtils.java   |  7 +-
 .../seatunnel/common/utils/JsonUtilsTest.java      | 78 ++++++++++++++++++++++
 .../elasticsearch/client/EsRestClient.java         | 26 ++++----
 3 files changed, 97 insertions(+), 14 deletions(-)

diff --git a/seatunnel-common/src/main/java/org/apache/seatunnel/common/utils/JsonUtils.java b/seatunnel-common/src/main/java/org/apache/seatunnel/common/utils/JsonUtils.java
index 6dbeae7bc9..4b7c2d5ce3 100644
--- a/seatunnel-common/src/main/java/org/apache/seatunnel/common/utils/JsonUtils.java
+++ b/seatunnel-common/src/main/java/org/apache/seatunnel/common/utils/JsonUtils.java
@@ -39,6 +39,7 @@ import org.apache.seatunnel.shade.org.apache.commons.lang3.StringUtils;
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.LinkedHashMap;
@@ -264,7 +265,7 @@ public class JsonUtils {
     }
 
     public static ObjectNode parseObject(String text) {
-        return parseObject(text.getBytes());
+        return parseObject(text.getBytes(StandardCharsets.UTF_8));
     }
 
     public static ObjectNode parseObject(byte[] content) {
@@ -272,7 +273,9 @@ public class JsonUtils {
             return (ObjectNode) OBJECT_MAPPER.readTree(content);
         } catch (IOException e) {
             throw new RuntimeException(
-                    "String json deserialization exception." + new String(content), e);
+                    "String json deserialization exception."
+                            + new String(content, StandardCharsets.UTF_8),
+                    e);
         }
     }
 
diff --git a/seatunnel-common/src/test/java/org/apache/seatunnel/common/utils/JsonUtilsTest.java b/seatunnel-common/src/test/java/org/apache/seatunnel/common/utils/JsonUtilsTest.java
new file mode 100644
index 0000000000..bea0e0d000
--- /dev/null
+++ b/seatunnel-common/src/test/java/org/apache/seatunnel/common/utils/JsonUtilsTest.java
@@ -0,0 +1,78 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.seatunnel.common.utils;
+
+import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.JsonNode;
+import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.seatunnel.shade.com.fasterxml.jackson.databind.node.ObjectNode;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+
+public class JsonUtilsTest {
+
+    private static final String CHINESE_JSON =
+            "{\"company_name\":\"乐\",\"industry\":\"互联网\",\"status\":\"在营\"}";
+
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+    /**
+     * Simulate the old broken code path inside parseObject(String): text.getBytes() on a GBK
+     * platform produces GBK bytes, then OBJECT_MAPPER.readTree(gbkBytes) fails with "Invalid UTF-8
+     * middle byte".
+     */
+    @Test
+    public void testOldGetBytesWithGBKFails() throws Exception {
+        // Simulate: String json = text.getBytes() where platform default is GBK
+        byte[] gbkBytes = CHINESE_JSON.getBytes(Charset.forName("GBK"));
+
+        // This is what parseObject(byte[]) does internally: OBJECT_MAPPER.readTree(content)
+        Assertions.assertThrows(
+                Exception.class,
+                () -> OBJECT_MAPPER.readTree(gbkBytes),
+                "GBK bytes should fail when Jackson parses as UTF-8, simulating the old bug");
+    }
+
+    /**
+     * Verify the fixed code path: text.getBytes(StandardCharsets.UTF_8) always produces valid UTF-8
+     * bytes, regardless of platform default charset.
+     */
+    @Test
+    public void testFixedGetBytesWithUTF8Succeeds() throws Exception {
+        // This is the fix: text.getBytes(StandardCharsets.UTF_8)
+        byte[] utf8Bytes = CHINESE_JSON.getBytes(StandardCharsets.UTF_8);
+
+        // OBJECT_MAPPER.readTree(utf8Bytes) parses as UTF-8 → succeeds
+        JsonNode node = OBJECT_MAPPER.readTree(utf8Bytes);
+        Assertions.assertEquals("乐", node.get("company_name").asText());
+        Assertions.assertEquals("互联网", node.get("industry").asText());
+        Assertions.assertEquals("在营", node.get("status").asText());
+    }
+
+    /** End-to-end test: JsonUtils.parseObject(String) with Chinese characters works correctly. */
+    @Test
+    public void testParseObjectWithChineseCharacters() {
+        ObjectNode result = JsonUtils.parseObject(CHINESE_JSON);
+        Assertions.assertEquals("乐", result.get("company_name").asText());
+        Assertions.assertEquals("互联网", result.get("industry").asText());
+        Assertions.assertEquals("在营", result.get("status").asText());
+    }
+}
diff --git a/seatunnel-connectors-v2/connector-elasticsearch/src/main/java/org/apache/seatunnel/connectors/seatunnel/elasticsearch/client/EsRestClient.java b/seatunnel-connectors-v2/connector-elasticsearch/src/main/java/org/apache/seatunnel/connectors/seatunnel/elasticsearch/client/EsRestClient.java
index 911f001402..21b3c0eae6 100644
--- a/seatunnel-connectors-v2/connector-elasticsearch/src/main/java/org/apache/seatunnel/connectors/seatunnel/elasticsearch/client/EsRestClient.java
+++ b/seatunnel-connectors-v2/connector-elasticsearch/src/main/java/org/apache/seatunnel/connectors/seatunnel/elasticsearch/client/EsRestClient.java
@@ -53,6 +53,7 @@ import lombok.extern.slf4j.Slf4j;
 
 import java.io.Closeable;
 import java.io.IOException;
+import java.nio.charset.StandardCharsets;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
@@ -130,7 +131,7 @@ public class EsRestClient implements Closeable {
                         "bulk es Response is null");
             }
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-                String entity = EntityUtils.toString(response.getEntity());
+                String entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                 JsonNode json = OBJECT_MAPPER.readTree(entity);
                 int took = json.get("took").asInt();
                 boolean errors = json.get("errors").asBoolean();
@@ -157,7 +158,7 @@ public class EsRestClient implements Closeable {
         Request request = new Request("GET", "/");
         try {
             Response response = restClient.performRequest(request);
-            String result = EntityUtils.toString(response.getEntity());
+            String result = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
             JsonNode jsonNode = OBJECT_MAPPER.readTree(result);
             JsonNode versionNode = jsonNode.get("version");
             return ElasticsearchClusterInfo.builder()
@@ -316,7 +317,7 @@ public class EsRestClient implements Closeable {
                 return false;
             }
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-                String entity = EntityUtils.toString(response.getEntity());
+                String entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                 JsonNode jsonNode = JsonUtils.parseObject(entity);
                 boolean succeeded = jsonNode.get("succeeded").asBoolean();
                 return succeeded;
@@ -346,7 +347,7 @@ public class EsRestClient implements Closeable {
                         "POST " + endpoint + " response null");
             }
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-                String entity = EntityUtils.toString(response.getEntity());
+                String entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                 ObjectNode responseJson = JsonUtils.parseObject(entity);
                 return getDocsFromSqlResponse(responseJson, columnNodes);
             } else {
@@ -375,7 +376,7 @@ public class EsRestClient implements Closeable {
                         "POST " + endpoint + " response null");
             }
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-                String entity = EntityUtils.toString(response.getEntity());
+                String entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                 ObjectNode responseJson = JsonUtils.parseObject(entity);
 
                 JsonNode shards = responseJson.get("_shards");
@@ -502,7 +503,7 @@ public class EsRestClient implements Closeable {
                         "GET " + endpoint + " response null");
             }
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-                String entity = EntityUtils.toString(response.getEntity());
+                String entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                 return JsonUtils.toList(entity, IndexDocsCount.class);
             } else {
                 throw new ElasticsearchConnectorException(
@@ -528,7 +529,7 @@ public class EsRestClient implements Closeable {
                         "GET " + endpoint + " response null");
             }
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-                String entity = EntityUtils.toString(response.getEntity());
+                String entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                 return JsonUtils.toList(entity, Map.class).stream()
                         .map(map -> map.get("index").toString())
                         .collect(Collectors.toList());
@@ -655,7 +656,7 @@ public class EsRestClient implements Closeable {
                                 "GET %s response status code=%d",
                                 endpoint, response.getStatusLine().getStatusCode()));
             }
-            String entity = EntityUtils.toString(response.getEntity());
+            String entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
             log.info(String.format("GET %s respnse=%s", endpoint, entity));
             ObjectNode responseJson = JsonUtils.parseObject(entity);
             for (Iterator<JsonNode> it = responseJson.elements(); it.hasNext(); ) {
@@ -847,7 +848,8 @@ public class EsRestClient implements Closeable {
                                 "PUT %s response status code=%d, response=%s",
                                 endpoint,
                                 response.getStatusLine().getStatusCode(),
-                                EntityUtils.toString(response.getEntity())));
+                                EntityUtils.toString(
+                                        response.getEntity(), StandardCharsets.UTF_8)));
             }
         } catch (IOException ex) {
             throw new ElasticsearchConnectorException(
@@ -876,7 +878,7 @@ public class EsRestClient implements Closeable {
                         "POST " + endpoint + " response null");
             }
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-                String entity = EntityUtils.toString(response.getEntity());
+                String entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                 JsonNode jsonNode = JsonUtils.parseObject(entity);
                 return jsonNode.get("id").asText();
             } else {
@@ -912,7 +914,7 @@ public class EsRestClient implements Closeable {
                         "DELETE " + endpoint + " response null");
             }
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-                String entity = EntityUtils.toString(response.getEntity());
+                String entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                 JsonNode jsonNode = JsonUtils.parseObject(entity);
                 return jsonNode.get("succeeded").asBoolean();
             } else {
@@ -1009,7 +1011,7 @@ public class EsRestClient implements Closeable {
                         "POST " + endpoint + " response null");
             }
             if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
-                String entity = EntityUtils.toString(response.getEntity());
+                String entity = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
                 return parsePointInTimeResponse(entity, pitId);
             } else {
                 throw new ElasticsearchConnectorException(