Skip to content

Commit ca8e434

Browse files
committed
fail at field type mismatch in json2pb
1 parent e45e957 commit ca8e434

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/json2pb/json_to_pb.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -334,11 +334,15 @@ static bool JsonValueToProtoField(const BUTIL_RAPIDJSON_NAMESPACE::Value& value,
334334
const BUTIL_RAPIDJSON_NAMESPACE::Value & item = value[index]; \
335335
if (TYPE_MATCH == J2PCHECKTYPE(item, cpptype, jsontype)) { \
336336
reflection->Add##method(message, field, item.Get##jsontype()); \
337+
} else { \
338+
return false; \
337339
} \
338340
} \
339341
} else if (TYPE_MATCH == J2PCHECKTYPE(value, cpptype, jsontype)) { \
340342
reflection->Set##method(message, field, value.Get##jsontype()); \
341-
} \
343+
} else { \
344+
return false; \
345+
} \
342346
break; \
343347
} \
344348

@@ -430,7 +434,9 @@ static bool JsonValueToProtoField(const BUTIL_RAPIDJSON_NAMESPACE::Value& value,
430434
str = str_decoded;
431435
}
432436
reflection->AddString(message, field, str);
433-
}
437+
} else {
438+
return false;
439+
}
434440
}
435441
} else if (TYPE_MATCH == J2PCHECKTYPE(value, string, String)) {
436442
std::string str(value.GetString(), value.GetStringLength());
@@ -444,6 +450,8 @@ static bool JsonValueToProtoField(const BUTIL_RAPIDJSON_NAMESPACE::Value& value,
444450
str = str_decoded;
445451
}
446452
reflection->SetString(message, field, str);
453+
} else {
454+
return false;
447455
}
448456
break;
449457

test/brpc_protobuf_json_unittest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ TEST_F(ProtobufJsonTest, json_to_pb_expected_failed_case) {
494494
JsonContextBody data9;
495495
ret = json2pb::JsonToProtoMessage(info3, &data9, &error);
496496
ASSERT_FALSE(ret);
497-
ASSERT_STREQ("Invalid value `23' for optional field `Content.uid' which SHOULD be string, Missing required field: Ext.databyte", error.data());
497+
ASSERT_STREQ("Invalid value `23' for optional field `Content.uid' which SHOULD be string", error.data());
498498
}
499499

500500
TEST_F(ProtobufJsonTest, json_to_pb_perf_case) {

0 commit comments

Comments
 (0)