Unverified Commit e508fc0c authored by Paul Yang's avatar Paul Yang Committed by GitHub

Check the message to be encoded is the wrong type. (#4885) (#4949)

* Check the message to be encoded is the wrong type. (#4885)

* Change TypeError to ArgumentError
parent fc90fd6d
......@@ -1106,6 +1106,13 @@ static void putmsg(VALUE msg_rb, const Descriptor* desc,
TypedData_Get_Struct(msg_rb, MessageHeader, &Message_type, msg);
if (desc != msg->descriptor) {
rb_raise(rb_eArgError,
"The type of given msg is '%s', expect '%s'.",
upb_msgdef_fullname(msg->descriptor->msgdef),
upb_msgdef_fullname(desc->msgdef));
}
for (upb_msg_field_begin(&i, desc->msgdef);
!upb_msg_field_done(&i);
upb_msg_field_next(&i)) {
......
......@@ -84,4 +84,14 @@ class EncodeDecodeTest < Test::Unit::TestCase
assert_match 'optional_int32', json
end
def test_encode_wrong_msg
e = assert_raise ::ArgumentError do
m = A::B::C::TestMessage.new(
:optional_int32 => 1,
)
Google::Protobuf::Any.encode(m)
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment