THRIFT-3307 Raise an error when trying to serialize a union with an incorrect set_field
Client: Ruby
Patch: Joe Ennever

This closes #597
diff --git a/lib/rb/ext/struct.c b/lib/rb/ext/struct.c
index f500d03..e3aa855 100644
--- a/lib/rb/ext/struct.c
+++ b/lib/rb/ext/struct.c
@@ -290,7 +290,7 @@
 
     if (TYPE(value) == T_ARRAY) {
       items = value;
-    } else {        
+    } else {
       if (rb_cSet == CLASS_OF(value)) {
         items = rb_funcall(value, entries_method_id, 0);
       } else {
@@ -670,6 +670,10 @@
 
   VALUE field_info = rb_hash_aref(struct_fields, field_id);
 
+  if(NIL_P(field_info)) {
+    rb_raise(rb_eRuntimeError, "set_field is not valid for this union!");
+  }
+
   VALUE ttype_value = rb_hash_aref(field_info, type_sym);
   int ttype = FIX2INT(ttype_value);
 
diff --git a/lib/rb/spec/union_spec.rb b/lib/rb/spec/union_spec.rb
index a427090..6ad3194 100644
--- a/lib/rb/spec/union_spec.rb
+++ b/lib/rb/spec/union_spec.rb
@@ -48,6 +48,13 @@
       lambda { union.some_characters }.should raise_error(RuntimeError, "some_characters is not union's set field.")
     end
 
+    it "should raise for wrong set field when hash initialized and type checking is off" do
+      Thrift.type_checking = false
+      union = SpecNamespace::My_union.new({incorrect_field: :incorrect})
+      example = lambda { Thrift::Serializer.new.serialize(union) }
+      example.should raise_error(RuntimeError, "set_field is not valid for this union!")
+    end
+
     it "should not be equal to nil" do
       union = SpecNamespace::My_union.new
       union.should_not == nil