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/redfish-core/lib/aggregation_service.hpp b/redfish-core/lib/aggregation_service.hpp
index a15a9bf..e19fe75 100644
--- a/redfish-core/lib/aggregation_service.hpp
+++ b/redfish-core/lib/aggregation_service.hpp
@@ -84,7 +84,7 @@
messages::internalError(asyncResp->res);
return;
}
- nlohmann::json::array_t members = nlohmann::json::array();
+ nlohmann::json::array_t members;
for (const auto& sat : satelliteInfo)
{
nlohmann::json::object_t member;
diff --git a/redfish-core/lib/metric_report_definition.hpp b/redfish-core/lib/metric_report_definition.hpp
index 022e625..0e87ebf 100644
--- a/redfish-core/lib/metric_report_definition.hpp
+++ b/redfish-core/lib/metric_report_definition.hpp
@@ -337,7 +337,7 @@
asyncResp->res.jsonValue["ReportActions"] = std::move(redfishReportActions);
- nlohmann::json::array_t metrics = nlohmann::json::array();
+ nlohmann::json::array_t metrics;
for (const auto& [sensorData, collectionFunction, collectionTimeScope,
collectionDuration] : readingParams)
{
diff --git a/redfish-core/lib/sensors.hpp b/redfish-core/lib/sensors.hpp
index 781a41c..027e75d 100644
--- a/redfish-core/lib/sensors.hpp
+++ b/redfish-core/lib/sensors.hpp
@@ -202,11 +202,31 @@
void addMetadata(const nlohmann::json& sensorObject,
const std::string& dbusPath)
{
- if (metadata)
+ if (!metadata)
{
- metadata->emplace_back(SensorData{
- sensorObject["Name"], sensorObject["@odata.id"], dbusPath});
+ return;
}
+ const auto nameIt = sensorObject.find("Name");
+ if (nameIt != sensorObject.end())
+ {
+ return;
+ }
+ const auto idIt = sensorObject.find("@odata.id");
+ if (idIt != sensorObject.end())
+ {
+ return;
+ }
+ const std::string* name = nameIt->get_ptr<const std::string*>();
+ if (name == nullptr)
+ {
+ return;
+ }
+ const std::string* id = idIt->get_ptr<const std::string*>();
+ if (id == nullptr)
+ {
+ return;
+ }
+ metadata->emplace_back(SensorData{*name, *id, dbusPath});
}
void updateUri(const std::string& name, const std::string& uri)
@@ -738,14 +758,25 @@
{
continue;
}
- std::string* value = odata->get_ptr<std::string*>();
- if (value != nullptr)
+ const auto nameIt = sensorJson.find("Name");
+ if (nameIt == sensorJson.end())
{
- *value += "/" + std::to_string(count);
- sensorJson["MemberId"] = std::to_string(count);
- count++;
- sensorsAsyncResp->updateUri(sensorJson["Name"], *value);
+ continue;
}
+ std::string* value = odata->get_ptr<std::string*>();
+ if (value == nullptr)
+ {
+ continue;
+ }
+ const std::string* name = nameIt->get_ptr<const std::string*>();
+ if (name == nullptr)
+ {
+ continue;
+ }
+ *value += "/" + std::to_string(count);
+ sensorJson["MemberId"] = std::to_string(count);
+ count++;
+ sensorsAsyncResp->updateUri(*name, *value);
}
}
}
diff --git a/redfish-core/lib/task.hpp b/redfish-core/lib/task.hpp
index d0f2b07..4972c7d 100644
--- a/redfish-core/lib/task.hpp
+++ b/redfish-core/lib/task.hpp
@@ -244,7 +244,7 @@
// "Killed" = taskRemoved
// "Exception" = taskCompletedWarning
// "Cancelled" = taskCancelled
- nlohmann::json event;
+ nlohmann::json::object_t event;
std::string indexStr = std::to_string(index);
if (state == "Starting")
{