You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@thrift.apache.org by br...@apache.org on 2011/12/27 23:27:00 UTC

svn commit: r1225035 - in /thrift/trunk: compiler/cpp/src/generate/t_java_generator.cc lib/java/test/org/apache/thrift/TestStruct.java test/ThriftTest.thrift

Author: bryanduxbury
Date: Tue Dec 27 22:26:59 2011
New Revision: 1225035

URL: http://svn.apache.org/viewvc?rev=1225035&view=rev
Log:
THRIFT-317. java: Issues with Java struct validation

Nested structs will now be validated before serialization starts.

Modified:
    thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc
    thrift/trunk/lib/java/test/org/apache/thrift/TestStruct.java
    thrift/trunk/test/ThriftTest.thrift

Modified: thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc
URL: http://svn.apache.org/viewvc/thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc?rev=1225035&r1=1225034&r2=1225035&view=diff
==============================================================================
--- thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc (original)
+++ thrift/trunk/compiler/cpp/src/generate/t_java_generator.cc Tue Dec 27 22:26:59 2011
@@ -1656,6 +1656,16 @@ void t_java_generator::generate_java_val
     }
   }
 
+  out << indent() << "// check for sub-struct validity" << endl;
+  for (f_iter = fields.begin(); f_iter != fields.end(); ++f_iter) {
+    t_type* type = (*f_iter)->get_type();
+    if (type->is_struct() && ! ((t_struct*)type)->is_union()) {
+      out << indent() << "if (" << (*f_iter)->get_name() << " != null) {" << endl;
+      out << indent() << "  " << (*f_iter)->get_name() << ".validate();" << endl;
+      out << indent() << "}" << endl;
+    }
+  }
+
   indent_down();
   indent(out) << "}" << endl << endl;
 }

Modified: thrift/trunk/lib/java/test/org/apache/thrift/TestStruct.java
URL: http://svn.apache.org/viewvc/thrift/trunk/lib/java/test/org/apache/thrift/TestStruct.java?rev=1225035&r1=1225034&r2=1225035&view=diff
==============================================================================
--- thrift/trunk/lib/java/test/org/apache/thrift/TestStruct.java (original)
+++ thrift/trunk/lib/java/test/org/apache/thrift/TestStruct.java Tue Dec 27 22:26:59 2011
@@ -22,7 +22,6 @@ import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
-
 import java.nio.ByteBuffer;
 import java.util.HashMap;
 import java.util.Map;
@@ -45,6 +44,8 @@ import thrift.test.JavaTestHelper;
 import thrift.test.Nesting;
 import thrift.test.Numberz;
 import thrift.test.OneOfEach;
+import thrift.test.StructA;
+import thrift.test.StructB;
 import thrift.test.Xtruct;
 
 public class TestStruct extends TestCase {
@@ -332,4 +333,36 @@ public class TestStruct extends TestCase
 
     assertEquals(ooe, ooe2);
   }
+
+  public void testSubStructValidation() throws Exception {
+    StructA valid = new StructA("valid");
+    StructA invalid = new StructA();
+
+    StructB b = new StructB();
+    try {
+      b.validate();
+      fail();
+    } catch (TException e) {
+      // expected
+    }
+
+    b = new StructB().setAb(valid);
+    b.validate();
+
+    b = new StructB().setAb(invalid);
+    try {
+      b.validate();
+      fail();
+    } catch (TException e) {
+      // expected
+    }
+
+    b = new StructB().setAb(valid).setAa(invalid);
+    try {
+      b.validate();
+      fail();
+    } catch (TException e) {
+      // expected
+    }
+  }
 }

Modified: thrift/trunk/test/ThriftTest.thrift
URL: http://svn.apache.org/viewvc/thrift/trunk/test/ThriftTest.thrift?rev=1225035&r1=1225034&r2=1225035&view=diff
==============================================================================
--- thrift/trunk/test/ThriftTest.thrift (original)
+++ thrift/trunk/test/ThriftTest.thrift Tue Dec 27 22:26:59 2011
@@ -233,3 +233,12 @@ struct BoolTest {
   1: optional bool b = true;
   2: optional string s = "true";
 }
+
+struct StructA {
+  1: required string s;
+}
+
+struct StructB {
+  1: optional StructA aa;
+  2: required StructA ab;
+}
\ No newline at end of file