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

fail at field type mismatch in json2pb #2608

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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
Loading