Skip to content

Commit

Permalink
THRIFT-3307 Raise an error when trying to serialize a union with an i…
Browse files Browse the repository at this point in the history
…ncorrect set_field

Client: Ruby
Patch: Joe Ennever

This closes #597
  • Loading branch information
Joe Ennever authored and nsuke committed Nov 8, 2015
1 parent d8ddb77 commit 5b15f8c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/rb/ext/struct.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ static void write_container(int ttype, VALUE field_info, VALUE value, VALUE prot

if (TYPE(value) == T_ARRAY) {
items = value;
} else {
} else {
if (rb_cSet == CLASS_OF(value)) {
items = rb_funcall(value, entries_method_id, 0);
} else {
Expand Down Expand Up @@ -670,6 +670,10 @@ static VALUE rb_thrift_union_write(VALUE self, VALUE protocol) {

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);

Expand Down
7 changes: 7 additions & 0 deletions lib/rb/spec/union_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 5b15f8c

Please sign in to comment.