You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by je...@apache.org on 2017/06/17 16:02:21 UTC

thrift git commit: THRIFT-4231 TJSONProtocol throws unexpected non-Thrift-exception on null strings Client: C# Patch: Jens Geyer

Repository: thrift
Updated Branches:
  refs/heads/master b8ee72de5 -> c55fdb953


THRIFT-4231 TJSONProtocol throws unexpected non-Thrift-exception on null strings
Client: C#
Patch: Jens Geyer

This closes #1291


Project: http://git-wip-us.apache.org/repos/asf/thrift/repo
Commit: http://git-wip-us.apache.org/repos/asf/thrift/commit/c55fdb95
Tree: http://git-wip-us.apache.org/repos/asf/thrift/tree/c55fdb95
Diff: http://git-wip-us.apache.org/repos/asf/thrift/diff/c55fdb95

Branch: refs/heads/master
Commit: c55fdb95340417a4ba2dda41e9e872a4bcc63459
Parents: b8ee72d
Author: Jens Geyer <je...@apache.org>
Authored: Fri Jun 16 23:10:54 2017 +0200
Committer: Jens Geyer <je...@apache.org>
Committed: Sat Jun 17 17:59:46 2017 +0200

----------------------------------------------------------------------
 .../src/thrift/generate/t_csharp_generator.cc   | 41 ++++++++++++++------
 1 file changed, 30 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/thrift/blob/c55fdb95/compiler/cpp/src/thrift/generate/t_csharp_generator.cc
----------------------------------------------------------------------
diff --git a/compiler/cpp/src/thrift/generate/t_csharp_generator.cc b/compiler/cpp/src/thrift/generate/t_csharp_generator.cc
index 57c3c5b..62a3e57 100644
--- a/compiler/cpp/src/thrift/generate/t_csharp_generator.cc
+++ b/compiler/cpp/src/thrift/generate/t_csharp_generator.cc
@@ -986,7 +986,10 @@ void t_csharp_generator::generate_csharp_struct_reader(ofstream& out, t_struct*
     if (field_is_required((*f_iter))) {
       indent(out) << "if (!isset_" << (*f_iter)->get_name() << ")" << endl;
       indent_up();
-      indent(out) << "throw new TProtocolException(TProtocolException.INVALID_DATA);" << endl;
+      out << indent()
+          << "throw new TProtocolException(TProtocolException.INVALID_DATA, "
+          << "\"required field " << prop_name((*f_iter)) << " not set\");" 
+          << endl;
       indent_down();
     }
   }
@@ -1022,19 +1025,35 @@ void t_csharp_generator::generate_csharp_struct_writer(ofstream& out, t_struct*
     for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
       bool is_required = field_is_required((*f_iter));
       bool has_default = field_has_default((*f_iter));
-      if (nullable_ && !has_default && !is_required) {
-        indent(out) << "if (" << prop_name((*f_iter)) << " != null) {" << endl;
-        indent_up();
-      } else if (!is_required) {
-        bool null_allowed = type_can_be_null((*f_iter)->get_type());
+      bool null_allowed = type_can_be_null((*f_iter)->get_type());
+
+      if (is_required)
+      {
         if (null_allowed) {
-          indent(out) << "if (" << prop_name((*f_iter)) << " != null && __isset."
-                      << normalize_name((*f_iter)->get_name()) << ") {" << endl;
-          indent_up();
-        } else {
-          indent(out) << "if (__isset." << normalize_name((*f_iter)->get_name()) << ") {" << endl;
+          indent(out) << "if (" << prop_name((*f_iter)) << " == null)" << endl;
           indent_up();
+          out << indent()
+              << "throw new TProtocolException(TProtocolException.INVALID_DATA, "
+              << "\"required field " << prop_name((*f_iter)) << " not set\");"
+              << endl;
+          indent_down();
+        }
+      }
+      else
+      {
+        if (nullable_ && !has_default) {
+            indent(out) << "if (" << prop_name((*f_iter)) << " != null) {" << endl;
         }
+        else if (null_allowed) {
+          out << indent()
+              << "if (" << prop_name((*f_iter)) << " != null && __isset."
+              << normalize_name((*f_iter)->get_name()) << ") {"
+              << endl;
+        }
+        else {
+           indent(out) << "if (__isset." << normalize_name((*f_iter)->get_name()) << ") {" << endl;
+        }
+        indent_up();
       }
       indent(out) << "field.Name = \"" << (*f_iter)->get_name() << "\";" << endl;
       indent(out) << "field.Type = " << type_to_enum((*f_iter)->get_type()) << ";" << endl;