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

albumenj pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/master by this push:
     new 93188c9  Using Gson serialization can cause Exception messages to be 
lost (#7680)
93188c9 is described below

commit 93188c99ea58ee7559a283640cc197fa4cf322c8
Author: 张志勇 <[email protected]>
AuthorDate: Tue May 11 10:13:13 2021 +0800

    Using Gson serialization can cause Exception messages to be lost (#7680)
---
 .../common/serialize/gson/ExceptionWrapper.java    | 42 ++++++++++++++++++++++
 .../common/serialize/gson/GsonJsonObjectInput.java |  6 ++++
 .../serialize/gson/GsonJsonObjectOutput.java       |  8 +++++
 .../serialize/gson/GsonJsonObjectOutputTest.java   | 20 +++++++++++
 4 files changed, 76 insertions(+)

diff --git 
a/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/ExceptionWrapper.java
 
b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/ExceptionWrapper.java
new file mode 100644
index 0000000..c27a8f2
--- /dev/null
+++ 
b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/ExceptionWrapper.java
@@ -0,0 +1,42 @@
+/*
+ * 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.dubbo.common.serialize.gson;
+
+public class ExceptionWrapper {
+
+
+    private final Object exception;
+
+    private final String clazz;
+
+
+    public ExceptionWrapper(Object exception, String clazz) {
+        this.exception = exception;
+        this.clazz = clazz;
+    }
+
+    public Object getException() {
+        return exception;
+    }
+
+
+    public String getClazz() {
+        return clazz;
+    }
+
+}
diff --git 
a/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectInput.java
 
b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectInput.java
index f6bbd13..cdf0b6a 100644
--- 
a/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectInput.java
+++ 
b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectInput.java
@@ -118,4 +118,10 @@ public class GsonJsonObjectInput implements ObjectInput {
         String json = readLine();
         return gson.fromJson(json, cls);
     }
+
+    @Override
+    public Throwable readThrowable() throws IOException, 
ClassNotFoundException {
+        ExceptionWrapper obj = readObject(ExceptionWrapper.class);
+        return gson.fromJson(obj.getException().toString(), (Type) 
Class.forName(obj.getClazz()));
+    }
 }
diff --git 
a/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutput.java
 
b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutput.java
index 41155da..0580d58 100644
--- 
a/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutput.java
+++ 
b/dubbo-serialization/dubbo-serialization-gson/src/main/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutput.java
@@ -100,6 +100,14 @@ public class GsonJsonObjectOutput implements ObjectOutput {
         json = null;
     }
 
+
+    @Override
+    public void writeThrowable(Object obj) throws IOException {
+        String clazz = obj.getClass().getName();
+        ExceptionWrapper bo = new ExceptionWrapper(obj, clazz);
+        this.writeObject(bo);
+    }
+
     @Override
     public void flushBuffer() throws IOException {
         writer.flush();
diff --git 
a/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutputTest.java
 
b/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutputTest.java
index 0ff2633..90f2d2c 100644
--- 
a/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutputTest.java
+++ 
b/dubbo-serialization/dubbo-serialization-gson/src/test/java/org/apache/dubbo/common/serialize/gson/GsonJsonObjectOutputTest.java
@@ -135,6 +135,26 @@ public class GsonJsonObjectOutputTest {
         assertThat(readObjectForImage, is(image));
     }
 
+    public class BizException extends RuntimeException {
+
+        public BizException(String message) {
+            super(message);
+        }
+
+    }
+
+
+    @Test
+    public void testWriteThrowable() throws IOException, 
ClassNotFoundException {
+        BizException exception = new BizException("biz_exception");
+        this.gsonJsonObjectOutput.writeThrowable(exception);
+        this.flushToInput();
+        Throwable ex = this.gsonJsonObjectInput.readThrowable();
+        assertThat(ex.getMessage(), is("biz_exception"));
+        assertThat(ex.getClass(), is(BizException.class));
+
+    }
+
     private void flushToInput() throws IOException {
         this.gsonJsonObjectOutput.flushBuffer();
         this.byteArrayInputStream = new 
ByteArrayInputStream(byteArrayOutputStream.toByteArray());

Reply via email to