From c64830bbca7ce327ad38e51ea4ddf96328804604 Mon Sep 17 00:00:00 2001 From: "@rubynerd" Date: Mon, 19 Dec 2016 23:48:48 +0000 Subject: unwrap descriptor class before comparison of RepeatedField types self->field_type_class returns the correct Ruby class, get_def_obj returns the Descriptor object used to generate the Ruby class via msgclass, so to compare the two types we get the msgclass from the descriptor. --- ruby/ext/google/protobuf_c/storage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'ruby') diff --git a/ruby/ext/google/protobuf_c/storage.c b/ruby/ext/google/protobuf_c/storage.c index 3ff2bda6..8e474244 100644 --- a/ruby/ext/google/protobuf_c/storage.c +++ b/ruby/ext/google/protobuf_c/storage.c @@ -598,7 +598,7 @@ static void check_repeated_field_type(VALUE val, const upb_fielddef* field) { if (self->field_type == UPB_TYPE_MESSAGE || self->field_type == UPB_TYPE_ENUM) { if (self->field_type_class != - get_def_obj(upb_fielddef_subdef(field))) { + Descriptor_msgclass(get_def_obj(upb_fielddef_subdef(field)))) { rb_raise(rb_eTypeError, "Repeated field array has wrong message/enum class"); } -- cgit v1.2.3 From f3e86fd26a685dd235e9a4849f62115e1348e91b Mon Sep 17 00:00:00 2001 From: "@rubynerd" Date: Fri, 30 Dec 2016 02:26:25 +0000 Subject: handle sanity check for repeating enums correctly --- ruby/ext/google/protobuf_c/storage.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'ruby') diff --git a/ruby/ext/google/protobuf_c/storage.c b/ruby/ext/google/protobuf_c/storage.c index 8e474244..fa2e0257 100644 --- a/ruby/ext/google/protobuf_c/storage.c +++ b/ruby/ext/google/protobuf_c/storage.c @@ -595,12 +595,20 @@ static void check_repeated_field_type(VALUE val, const upb_fielddef* field) { rb_raise(rb_eTypeError, "Repeated field array has wrong element type"); } - if (self->field_type == UPB_TYPE_MESSAGE || - self->field_type == UPB_TYPE_ENUM) { + if (self->field_type == UPB_TYPE_MESSAGE) { if (self->field_type_class != Descriptor_msgclass(get_def_obj(upb_fielddef_subdef(field)))) { rb_raise(rb_eTypeError, - "Repeated field array has wrong message/enum class"); + "Repeated field array has wrong message class"); + } + } + + + if (self->field_type == UPB_TYPE_ENUM) { + if (self->field_type_class != + EnumDescriptor_enummodule(get_def_obj(upb_fielddef_subdef(field)))) { + rb_raise(rb_eTypeError, + "Repeated field array has wrong enum class"); } } } -- cgit v1.2.3 From 1e58006b3cca28d11bbc92074c10b3295330b2b9 Mon Sep 17 00:00:00 2001 From: "@rubynerd" Date: Thu, 12 Oct 2017 18:23:01 +0100 Subject: test for field reassignment --- ruby/tests/repeated_field_test.rb | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'ruby') diff --git a/ruby/tests/repeated_field_test.rb b/ruby/tests/repeated_field_test.rb index 25727b7b..b64c3991 100644 --- a/ruby/tests/repeated_field_test.rb +++ b/ruby/tests/repeated_field_test.rb @@ -126,6 +126,12 @@ class RepeatedFieldTest < Test::Unit::TestCase assert_equal false, m.repeated_string.empty? end + def test_reassign + m = TestMessage.new + m.repeated_msg = Google::Protobuf::RepeatedField.new(:message, TestMessage2, [TestMessage2.new(:foo => 1)]) + assert_equal m.repeated_msg.first, TestMessage2.new(:foo => 1) + end + def test_array_accessor m = TestMessage.new reference_arr = %w(foo bar baz) -- cgit v1.2.3