diff --git a/trpc/config/BUILD b/trpc/config/BUILD index 447d590a..a60cae4d 100644 --- a/trpc/config/BUILD +++ b/trpc/config/BUILD @@ -132,6 +132,7 @@ cc_test( srcs = ["trpc_conf_compatible_test.cc"], deps = [ ":trpc_conf", + "@com_github_fmtlib_fmt//:fmtlib", "@com_google_googletest//:gtest", "@com_google_googletest//:gtest_main", ], diff --git a/trpc/config/trpc_conf_compatible.cc b/trpc/config/trpc_conf_compatible.cc index f4eb4f60..9031da29 100644 --- a/trpc/config/trpc_conf_compatible.cc +++ b/trpc/config/trpc_conf_compatible.cc @@ -282,7 +282,7 @@ bool TransformConfig(const Json::Value& from, std::map Json::ValueType value_type = from[*it].type(); switch (value_type) { case Json::intValue: { - config_map->insert(std::pair(key_name, std::to_string(from[*it].asInt()))); + config_map->insert(std::pair(key_name, std::to_string(from[*it].asInt64()))); break; } case Json::uintValue: { diff --git a/trpc/config/trpc_conf_compatible_test.cc b/trpc/config/trpc_conf_compatible_test.cc index 9026d47b..d7c9e81c 100644 --- a/trpc/config/trpc_conf_compatible_test.cc +++ b/trpc/config/trpc_conf_compatible_test.cc @@ -1,5 +1,7 @@ #include "trpc/config/trpc_conf_compatible.h" +#include "fmt/core.h" +#include "fmt/format.h" #include "gtest/gtest.h" #include "json/json.h" #include "yaml-cpp/yaml.h" @@ -65,6 +67,7 @@ TEST_F(TrpcConfCompatibleTest, GetInt64_Success) { ASSERT_EQ(result, -42); } + TEST_F(TrpcConfCompatibleTest, GetInt64_Fail) { Json::Value config_json; config_json["value1"] = 42; @@ -152,4 +155,39 @@ TEST_F(TrpcConfCompatibleTest, ExtractYamlKeys) { ASSERT_EQ(yaml_keys[1], "key2"); } +TEST_F(TrpcConfCompatibleTest, TransformConfig_JSON_Success_WithIntegerMaxAndMin) { + auto int_max = std::numeric_limits::max(); + auto int_min = std::numeric_limits::min(); + auto int64_max = std::numeric_limits::max(); + auto int64_min = std::numeric_limits::min(); + std::string from = fmt::format(R"({{"id": "0001", "int_max": {}, "int_min": {}, "int64_max": {}, "int64_min": {}}})", + int_max, int_min, int64_max, int64_min); + + Json::Value json_value; + bool trans_result = trpc::config::TransformConfig(from, &json_value); + ASSERT_TRUE(trans_result); + + std::map out_map; + trans_result = trpc::config::TransformConfig(json_value, &out_map, ""); + ASSERT_TRUE(trans_result); + ASSERT_TRUE(out_map.find("id") != out_map.end()); + ASSERT_TRUE(out_map.find("int_max") != out_map.end()); + ASSERT_TRUE(out_map.find("int_min") != out_map.end()); + ASSERT_TRUE(out_map.find("int64_max") != out_map.end()); + ASSERT_TRUE(out_map.find("int64_min") != out_map.end()); +} + +TEST_F(TrpcConfCompatibleTest, TransformConfig_JSON_Fail_WithIntegerOverflow) { + std::string int_overflow{"12233344449223372036854775807"}; + std::string from = fmt::format(R"({{"id": "0001", "int_overflow": {}}})", int_overflow); + + Json::Value json_value; + bool trans_result = trpc::config::TransformConfig(from, &json_value); + // Overflowed integer-64 value in cpp-json is ok. + ASSERT_TRUE(trans_result); + + int64_t v{0}; + ASSERT_FALSE(trpc::config::GetInt64(json_value, "int_overflow", &v)); +} + } // namespace trpc::testing