summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEwan Higgs <ewan_higgs@yahoo.co.uk>2019-09-20 17:15:04 +0200
committerDuru Can Celasun <dcelasun@apache.org>2019-09-20 16:15:04 +0100
commitb3745eea10cde93957882df56ef21f05f5e3cf6b (patch)
tree8d0323d9f66d8a4a36104a64576b4ade3412a05f
parent77d96c18c3729bf3faeadff67e57e7e429f1d3cd (diff)
downloadthrift-b3745eea10cde93957882df56ef21f05f5e3cf6b.tar.gz
THRIFT-4932: Using a default string on a binary field results in invalid Java code.
Client: Java Patch: Ewan Higgs This closes #1875.
-rw-r--r--compiler/cpp/src/thrift/generate/t_java_generator.cc6
-rw-r--r--lib/java/gradle/generateTestThrift.gradle1
-rw-r--r--test/JavaBinaryDefault.thrift25
-rw-r--r--test/JavaTypes.thrift4
-rwxr-xr-xtest/Makefile.am1
5 files changed, 36 insertions, 1 deletions
diff --git a/compiler/cpp/src/thrift/generate/t_java_generator.cc b/compiler/cpp/src/thrift/generate/t_java_generator.cc
index 2fb1f1aeb..7254e12b1 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 2b5373997..121bf537d 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 000000000..5517802c7
--- /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 8c733ada8..555334082 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 f6b867cf1..c2fbbd459 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 \