Add explicit handling for sub messages. (#12540)

Fixes issue #12505.

Also add a regression test.

Closes #12540

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12540 from JasonLunn:fix_12505 24ac3ecd86
PiperOrigin-RevId: 531001738
pull/12754/head
Jason Lunn 2 years ago committed by Copybara-Service
parent d372fcded6
commit dc1387e41d
  1. 6
      ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
  2. 9
      ruby/tests/common_tests.rb

@ -901,7 +901,11 @@ public class RubyMessage extends RubyObject {
if (fdef.isRepeated()) {
copy.fields.put(fdef, this.getRepeatedField(context, fdef).deepCopy(context));
} else if (fields.containsKey(fdef)) {
copy.setFieldInternal(context, fdef, fields.get(fdef));
if (fdef.getType() == FieldDescriptor.Type.MESSAGE) {
copy.setFieldInternal(context, fdef, ((RubyMessage) fields.get(fdef)).deepCopy(context));
} else {
copy.setFieldInternal(context, fdef, fields.get(fdef));
}
} else if (builder.hasField(fdef)) {
copy.fields.put(fdef, wrapField(context, fdef, builder.getField(fdef)));
}

@ -701,6 +701,15 @@ module CommonTests
assert m.repeated_msg[0].object_id != m2.repeated_msg[0].object_id
end
def test_sub_message_deep_copy # regression test for issue 12505
m = proto_module::Foo.new(bar: proto_module::Bar.new(msg: "Hello World"))
m2 = Google::Protobuf.deep_copy(m)
assert_equal(m, m2)
assert_not_equal(m.object_id, m2.object_id)
assert_equal(m.bar, m2.bar)
assert_not_equal(m.bar.object_id, m2.bar.object_id)
end
def test_message_eq
m = proto_module::TestMessage.new(:optional_int32 => 42,
:repeated_int32 => [1, 2, 3])

Loading…
Cancel
Save