aboutsummaryrefslogtreecommitdiffhomepage
path: root/ruby/src
diff options
context:
space:
mode:
authorGravatar Zachary Anker <zanker@squareup.com>2017-09-14 08:35:13 -0700
committerGravatar Zachary Anker <zanker@squareup.com>2017-09-20 11:38:05 -0700
commit83264bd160326fe808c2d5bfbebe54e77f781152 (patch)
tree68c4391f0cbf1a6fcdda11e4028a66af108f87e5 /ruby/src
parentd1bc27caef8377a710370189675cb0958443e8f1 (diff)
Fixed to_h with repeated messages to return hashes in Ruby
Diffstat (limited to 'ruby/src')
-rw-r--r--ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java14
1 files changed, 13 insertions, 1 deletions
diff --git a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
index 733ccfbc..0cdb67fe 100644
--- a/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
+++ b/ruby/src/main/java/com/google/protobuf/jruby/RubyMessage.java
@@ -367,7 +367,19 @@ public class RubyMessage extends RubyObject {
for (Descriptors.FieldDescriptor fdef : this.descriptor.getFields()) {
IRubyObject value = getField(context, fdef);
if (!value.isNil()) {
- if (value.respondsTo("to_h")) {
+ if (fdef.isRepeated() && !fdef.isMapField()) {
+ if (fdef.getType() != Descriptors.FieldDescriptor.Type.MESSAGE) {
+ value = Helpers.invoke(context, value, "to_a");
+ } else {
+ RubyArray ary = value.convertToArray();
+ for (int i = 0; i < ary.size(); i++) {
+ IRubyObject submsg = Helpers.invoke(context, ary.eltInternal(i), "to_h");
+ ary.eltInternalSet(i, submsg);
+ }
+
+ value = ary.to_ary();
+ }
+ } else if (value.respondsTo("to_h")) {
value = Helpers.invoke(context, value, "to_h");
} else if (value.respondsTo("to_a")) {
value = Helpers.invoke(context, value, "to_a");