You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by dc...@apache.org on 2019/09/20 15:15:13 UTC

[thrift] branch master updated: THRIFT-4932: Using a default string on a binary field results in invalid Java code.

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

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


The following commit(s) were added to refs/heads/master by this push:
     new b3745ee  THRIFT-4932: Using a default string on a binary field results in invalid Java code.
b3745ee is described below

commit b3745eea10cde93957882df56ef21f05f5e3cf6b
Author: Ewan Higgs <ew...@yahoo.co.uk>
AuthorDate: Fri Sep 20 17:15:04 2019 +0200

    THRIFT-4932: Using a default string on a binary field results in invalid Java code.
    
    Client: Java
    Patch: Ewan Higgs
    
    This closes #1875.
---
 .../cpp/src/thrift/generate/t_java_generator.cc    |  6 +++++-
 lib/java/gradle/generateTestThrift.gradle          |  1 +
 test/JavaBinaryDefault.thrift                      | 25 ++++++++++++++++++++++
 test/JavaTypes.thrift                              |  4 ++++
 test/Makefile.am                                   |  1 +
 5 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/compiler/cpp/src/thrift/generate/t_java_generator.cc b/compiler/cpp/src/thrift/generate/t_java_generator.cc
index 2fb1f1a..7254e12 100644
--- a/compiler/cpp/src/thrift/generate/t_java_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_java_generator.cc
@@ -731,7 +731,11 @@ string t_java_generator::render_const_value(ostream& out, t_type* type, t_const_
     t_base_type::t_base tbase = ((t_base_type*)type)->get_base();
     switch (tbase) {
     case t_base_type::TYPE_STRING:
-      render << '"' << get_escaped_string(value) << '"';
+      if (((t_base_type*)type)->is_binary()) {
+        render << "java.nio.ByteBuffer.wrap(\"" << get_escaped_string(value) << "\".getBytes())";
+      } else {
+        render << '"' << get_escaped_string(value) << '"';
+      }
       break;
     case t_base_type::TYPE_BOOL:
       render << ((value->get_integer() > 0) ? "true" : "false");
diff --git a/lib/java/gradle/generateTestThrift.gradle b/lib/java/gradle/generateTestThrift.gradle
index 2b53739..121bf53 100644
--- a/lib/java/gradle/generateTestThrift.gradle
+++ b/lib/java/gradle/generateTestThrift.gradle
@@ -80,6 +80,7 @@ task generateJava(group: 'Build') {
     thriftCompile(it, 'ManyOptionals.thrift')
     thriftCompile(it, 'JavaDeepCopyTest.thrift')
     thriftCompile(it, 'EnumContainersTest.thrift')
+    thriftCompile(it, 'JavaBinaryDefault.thrift')
 }
 
 task generateBeanJava(group: 'Build') {
diff --git a/test/JavaBinaryDefault.thrift b/test/JavaBinaryDefault.thrift
new file mode 100644
index 0000000..5517802
--- /dev/null
+++ b/test/JavaBinaryDefault.thrift
@@ -0,0 +1,25 @@
+/*
+ * 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.
+ */
+
+namespace java thrift.test
+
+struct StringAndBinary {
+  1: optional string strval = ""
+  2: optional binary binval = ""
+}
diff --git a/test/JavaTypes.thrift b/test/JavaTypes.thrift
index 8c733ad..5553340 100644
--- a/test/JavaTypes.thrift
+++ b/test/JavaTypes.thrift
@@ -27,6 +27,10 @@ struct String {
   1: string val
 }
 
+struct Binary {
+  1: binary val
+}
+
 struct Boolean {
   1: bool val
 }
diff --git a/test/Makefile.am b/test/Makefile.am
index f6b867c..c2fbbd4 100755
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -158,6 +158,7 @@ EXTRA_DIST = \
 	Include.thrift \
 	Int64Test.thrift \
 	JavaBeansTest.thrift \
+	JavaBinaryDefault.thrift \
 	JavaDeepCopyTest.thrift \
 	JavaTypes.thrift \
 	JsDeepConstructorTest.thrift \