Remove implicit conversions
Since 2020, nlohmann has recognized that implicit conversions to and
from json are an issue. Many bugs have been caused at both development
time and runtime due to unexpected implicit conversions from json to
std::string/int/bool. This commit disables implicit conversions using
JSON_USE_IMPLICIT_CONVERSIONS [1]. This option will become the default
in the future. That comment was written 3 years ago at this point, so
we should prepare.
Tested:
Redfish service validator passes.
[1] https://json.nlohmann.me/api/macros/json_use_implicit_conversions/
Change-Id: Id6cc47b9bbf8889e4777fd6d77ec992f3139962c
Signed-off-by: Ed Tanous <etanous@nvidia.com>
diff --git a/test/include/sessions_test.cpp b/test/include/sessions_test.cpp
index 2840ee9..0f33712 100644
--- a/test/include/sessions_test.cpp
+++ b/test/include/sessions_test.cpp
@@ -9,9 +9,15 @@
TEST(AuthConfigMethods, FromJsonHappyPath)
{
persistent_data::AuthConfigMethods methods;
+ nlohmann::json::object_t jsonValue;
+ jsonValue["BasicAuth"] = true;
+ jsonValue["CookieAuth"] = true;
+ jsonValue["MTLSCommonNameParseMode"] = 2;
+ jsonValue["SessionToken"] = true;
+ jsonValue["TLS"] = true;
+ jsonValue["TLSStrict"] = false;
+ jsonValue["XToken"] = true;
- nlohmann::json jsonValue = nlohmann::json::parse(
- R"({"BasicAuth":true,"Cookie":true,"SessionToken":true,"TLS":true,"MTLSCommonNameParseMode":2,"TLSStrict":false,"XToken":true})");
methods.fromJson(jsonValue);
EXPECT_EQ(methods.basic, true);
@@ -29,9 +35,15 @@
persistent_data::AuthConfigMethods methods;
persistent_data::MTLSCommonNameParseMode prevValue =
methods.mTLSCommonNameParsingMode;
+ nlohmann::json::object_t jsonValue;
+ jsonValue["BasicAuth"] = true;
+ jsonValue["CookieAuth"] = true;
+ jsonValue["MTLSCommonNameParseMode"] = 4;
+ jsonValue["SessionToken"] = true;
+ jsonValue["TLS"] = true;
+ jsonValue["TLSStrict"] = false;
+ jsonValue["XToken"] = true;
- nlohmann::json jsonValue = nlohmann::json::parse(
- R"({"BasicAuth":true,"Cookie":true,"SessionToken":true,"TLS":true,"MTLSCommonNameParseMode":4,"TLSStrict":false,"XToken":true})");
methods.fromJson(jsonValue);
EXPECT_EQ(methods.basic, true);
diff --git a/test/redfish-core/include/utils/dbus_utils.cpp b/test/redfish-core/include/utils/dbus_utils.cpp
index 8aae864..269731e 100644
--- a/test/redfish-core/include/utils/dbus_utils.cpp
+++ b/test/redfish-core/include/utils/dbus_utils.cpp
@@ -41,8 +41,8 @@
boost::system::error_code ec;
sdbusplus::message_t msg;
- afterSetPropertyAction(asyncResp, "MyRedfishProperty",
- nlohmann::json("MyRedfishValue"), ec, msg);
+ afterSetPropertyAction(asyncResp, "MyRedfishProperty", "MyRedfishValue", ec,
+ msg);
EXPECT_EQ(asyncResp->res.result(), boost::beast::http::status::ok);
EXPECT_EQ(asyncResp->res.jsonValue,
diff --git a/test/redfish-core/include/utils/json_utils_test.cpp b/test/redfish-core/include/utils/json_utils_test.cpp
index c7f85f1..58e6313 100644
--- a/test/redfish-core/include/utils/json_utils_test.cpp
+++ b/test/redfish-core/include/utils/json_utils_test.cpp
@@ -470,8 +470,11 @@
TEST(SortJsonArrayByOData, ElementMissingKeyReturnsFalseArrayIsPartlySorted)
{
- nlohmann::json::array_t array =
- R"([{"@odata.id" : "/redfish/v1/100"}, {"@odata.id": "/redfish/v1/1"}, {"@odata.id" : "/redfish/v1/20"}])"_json;
+ nlohmann::json::array_t array;
+ array.push_back(R"({"@odata.id" : "/redfish/v1/100"})"_json);
+ array.push_back(R"({"@odata.id" : "/redfish/v1/1"})"_json);
+ array.push_back(R"({"@odata.id" : "/redfish/v1/20"})"_json);
+
sortJsonArrayByOData(array);
// Objects with other keys are always larger than those with the specified
// key.
@@ -483,8 +486,11 @@
TEST(SortJsonArrayByOData, SortedByStringValueOnSuccessArrayIsSorted)
{
- nlohmann::json::array_t array =
- R"([{"@odata.id": "/redfish/v1/20"}, {"@odata.id" : "/redfish/v1"}, {"@odata.id" : "/redfish/v1/100"}])"_json;
+ nlohmann::json::array_t array;
+ array.push_back(R"({"@odata.id" : "/redfish/v1/20"})"_json);
+ array.push_back(R"({"@odata.id" : "/redfish/v1"})"_json);
+ array.push_back(R"({"@odata.id" : "/redfish/v1/100"})"_json);
+
sortJsonArrayByOData(array);
EXPECT_THAT(array,
ElementsAre(R"({"@odata.id": "/redfish/v1"})"_json,
@@ -543,8 +549,11 @@
TEST(SortJsonArrayByKey, ElementMissingKeyReturnsFalseArrayIsPartlySorted)
{
- nlohmann::json::array_t array =
- R"([{"@odata.id" : "/redfish/v1/100"}, {"Name" : "/redfish/v1/5"}, {"@odata.id": "/redfish/v1/1"}, {"@odata.id" : "/redfish/v1/20"}])"_json;
+ nlohmann::json::array_t array;
+ array.push_back(R"({"@odata.id" : "/redfish/v1/100"})"_json);
+ array.push_back(R"({"Name": "/redfish/v1/5"})"_json);
+ array.push_back(R"({"@odata.id" : "/redfish/v1/1"})"_json);
+ array.push_back(R"({"@odata.id" : "/redfish/v1/20"})"_json);
sortJsonArrayByKey(array, "@odata.id");
// Objects with other keys are always smaller than those with the specified
// key.
@@ -557,8 +566,10 @@
TEST(SortJsonArrayByKey, SortedByStringValueOnSuccessArrayIsSorted)
{
- nlohmann::json::array_t array =
- R"([{"@odata.id": "/redfish/v1/20", "Name": "a"}, {"@odata.id" : "/redfish/v1", "Name": "c"}, {"@odata.id" : "/redfish/v1/100", "Name": "b"}])"_json;
+ nlohmann::json::array_t array;
+ array.push_back(R"({"@odata.id" : "/redfish/v1/20", "Name": "a"})"_json);
+ array.push_back(R"({"@odata.id" : "/redfish/v1", "Name": "c"})"_json);
+ array.push_back(R"({"@odata.id" : "/redfish/v1/100", "Name": "b"})"_json);
sortJsonArrayByKey(array, "@odata.id");
EXPECT_THAT(