diff options
author | 2017-09-20 15:35:28 -0700 | |
---|---|---|
committer | 2017-09-20 15:35:28 -0700 | |
commit | eade82c8365e1680deb22260d9739928b0492063 (patch) | |
tree | f73a51a7a24c5b6d524d6ca058847a232652f19d /ruby/src | |
parent | cf1b29dec84f9a027dacdff094bbb6b10a110ed3 (diff) | |
parent | 8eae3fe6e90ee03821d89b03cbcecd622f479028 (diff) |
Merge pull request #3639 from zanker/zanker/fix-embedded-to_h
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.java | 14 |
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"); |