Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ruby has_...? method #19731

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion ruby/ext/google/protobuf_c/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,8 @@ static VALUE Message_field_accessor(VALUE _self, const upb_FieldDef* f,
if (!upb_FieldDef_HasPresence(f)) {
rb_raise(rb_eRuntimeError, "Field does not have presence.");
}
return upb_Message_HasFieldByDef(Message_Get(_self, NULL), f);
return upb_Message_HasFieldByDef(Message_Get(_self, NULL), f) ? Qtrue
: Qfalse;
case METHOD_WRAPPER_GETTER: {
Message* self = ruby_to_Message(_self);
if (upb_Message_HasFieldByDef(self->msg, f)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,9 @@ public IRubyObject methodMissing(ThreadContext context, IRubyObject[] args) {
fieldDescriptor = descriptor.findFieldByName(methodName);

if (fieldDescriptor != null && fieldDescriptor.hasPresence()) {
return fields.containsKey(fieldDescriptor) ? runtime.getTrue() : runtime.getFalse();
return (fields.containsKey(fieldDescriptor) || builder.hasField(fieldDescriptor))
? runtime.getTrue()
: runtime.getFalse();
}

} else if (methodName.endsWith(AS_VALUE_SUFFIX)) {
Expand Down Expand Up @@ -952,7 +954,7 @@ protected IRubyObject hasField(ThreadContext context, FieldDescriptor fieldDescr
if (!fieldDescriptor.hasPresence()) {
throw context.runtime.newArgumentError("does not track presence");
}
return fields.containsKey(fieldDescriptor)
return (fields.containsKey(fieldDescriptor) || builder.hasField(fieldDescriptor))
? context.runtime.getTrue()
: context.runtime.getFalse();
}
Expand Down
9 changes: 9 additions & 0 deletions ruby/tests/basic_proto2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,15 @@ def test_has_field

m = TestMessage.new(:optional_int32 => nil)
refute m.has_optional_int32?
refute TestMessage.descriptor.lookup('optional_int32').has?(m)

m = TestMessage.new(:optional_int32 => 0)
assert m.has_optional_int32?
assert TestMessage.descriptor.lookup('optional_int32').has?(m)

m = TestMessage.decode(TestMessage.encode(m))
assert m.has_optional_int32?
assert TestMessage.descriptor.lookup('optional_int32').has?(m)

assert_raises NoMethodError do
m.has_repeated_msg?
Expand Down
Loading