THRIFT-1331. rb: Ruby library deserializes an empty map to nil
authorBryan Duxbury <bryanduxbury@apache.org>
Tue, 20 Sep 2011 18:45:56 +0000 (18:45 +0000)
committerBryan Duxbury <bryanduxbury@apache.org>
Tue, 20 Sep 2011 18:45:56 +0000 (18:45 +0000)
This patch properly detects when the metadata is omitted in Compact Protocol messages.

Patch: Armaan Sarkar

git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1173300 13f79535-47bb-0310-9956-ffa450edef68

lib/rb/ext/struct.c
lib/rb/lib/thrift/struct_union.rb
lib/rb/spec/compact_protocol_spec.rb
test/DebugProtoTest.thrift

index dae338e..590983f 100644 (file)
@@ -484,7 +484,7 @@ static VALUE read_anything(VALUE protocol, int ttype, VALUE field_info) {
     if (!NIL_P(key_info) && !NIL_P(value_info)) {
       int specified_key_type = FIX2INT(rb_hash_aref(key_info, type_sym));
       int specified_value_type = FIX2INT(rb_hash_aref(value_info, type_sym));
-      if (specified_key_type == key_ttype && specified_value_type == value_ttype) {
+      if (num_entries == 0 || (specified_key_type == key_ttype && specified_value_type == value_ttype)) {
         result = rb_hash_new();
 
         for (i = 0; i < num_entries; ++i) {
index 0ff175f..4e0afcf 100644 (file)
@@ -56,7 +56,7 @@ module Thrift
       when Types::MAP
         key_type, val_type, size = iprot.read_map_begin
         # Skip the map contents if the declared key or value types don't match the expected ones.
-        if (key_type != field[:key][:type] || val_type != field[:value][:type])
+        if (size != 0 && (key_type != field[:key][:type] || val_type != field[:value][:type]))
           size.times do
             iprot.skip(key_type)
             iprot.skip(val_type)
index 1fa2182..a88a2ae 100644 (file)
@@ -115,6 +115,17 @@ describe Thrift::CompactProtocol do
     brcp2.should == brcp
   end
   
+  it "should deserialize an empty map to an empty hash" do
+    struct = SingleMapTestStruct.new(:i32_map => {})
+    ser = Thrift::Serializer.new(Thrift::CompactProtocolFactory.new)
+    bytes = ser.serialize(struct)
+
+    deser = Thrift::Deserializer.new(Thrift::CompactProtocolFactory.new)
+    struct2 = SingleMapTestStruct.new
+    deser.deserialize(struct2, bytes)
+    struct.should == struct2
+  end
+  
   class JankyHandler
     def Janky(i32arg)
       i32arg * 2
index 23d5d97..b8b89b7 100644 (file)
@@ -161,6 +161,10 @@ struct CompactProtoTestStruct {
   49: map<byte, list<byte>>       byte_list_map;
 }
 
+// To be used to test the serialization of an empty map
+struct SingleMapTestStruct {
+  1: required map<i32, i32>       i32_map;
+}
 
 const CompactProtoTestStruct COMPACT_TEST = {
   'a_byte'             : 127,