Skip to content

Commit

Permalink
fail at field type mismatch in json2pb
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterRK committed Jan 17, 2025
1 parent e45e957 commit ca8e434
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/json2pb/json_to_pb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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; \
} \

Expand Down Expand Up @@ -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());
Expand All @@ -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;

Expand Down
2 changes: 1 addition & 1 deletion test/brpc_protobuf_json_unittest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit ca8e434

Please sign in to comment.