diff --git a/src/json2pb/json_to_pb.cpp b/src/json2pb/json_to_pb.cpp index 60ba4fdfc9..4d4fbfa3c5 100644 --- a/src/json2pb/json_to_pb.cpp +++ b/src/json2pb/json_to_pb.cpp @@ -334,11 +334,15 @@ static bool JsonValueToProtoField(const BUTIL_RAPIDJSON_NAMESPACE::Value& value, const BUTIL_RAPIDJSON_NAMESPACE::Value & item = value[index]; \ if (TYPE_MATCH == J2PCHECKTYPE(item, cpptype, jsontype)) { \ reflection->Add##method(message, field, item.Get##jsontype()); \ + } else { \ + return false; \ } \ } \ } else if (TYPE_MATCH == J2PCHECKTYPE(value, cpptype, jsontype)) { \ reflection->Set##method(message, field, value.Get##jsontype()); \ - } \ + } else { \ + return false; \ + } \ break; \ } \ @@ -430,7 +434,9 @@ static bool JsonValueToProtoField(const BUTIL_RAPIDJSON_NAMESPACE::Value& value, str = str_decoded; } reflection->AddString(message, field, str); - } + } else { + return false; + } } } else if (TYPE_MATCH == J2PCHECKTYPE(value, string, String)) { std::string str(value.GetString(), value.GetStringLength()); @@ -444,6 +450,8 @@ static bool JsonValueToProtoField(const BUTIL_RAPIDJSON_NAMESPACE::Value& value, str = str_decoded; } reflection->SetString(message, field, str); + } else { + return false; } break; diff --git a/test/brpc_protobuf_json_unittest.cpp b/test/brpc_protobuf_json_unittest.cpp index 3565772e1d..917f2e0117 100644 --- a/test/brpc_protobuf_json_unittest.cpp +++ b/test/brpc_protobuf_json_unittest.cpp @@ -494,7 +494,7 @@ TEST_F(ProtobufJsonTest, json_to_pb_expected_failed_case) { JsonContextBody data9; ret = json2pb::JsonToProtoMessage(info3, &data9, &error); ASSERT_FALSE(ret); - ASSERT_STREQ("Invalid value `23' for optional field `Content.uid' which SHOULD be string, Missing required field: Ext.databyte", error.data()); + ASSERT_STREQ("Invalid value `23' for optional field `Content.uid' which SHOULD be string", error.data()); } TEST_F(ProtobufJsonTest, json_to_pb_perf_case) {