Use array
When passing arrays of json objects around, we should be using the
native nlohmann::json::array_t, rather than using an nlohmann::json, and
interpreting it as an array. This improves our type safety.
Tested:
EthernetInterfaces returns the same value as before. Redfish service
validator shows no new failures on those interfaces.
Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I609205832a6e99a23b4764be30045a456021fe44
diff --git a/redfish-core/lib/ethernet.hpp b/redfish-core/lib/ethernet.hpp
index 416d5dc..5c928d6 100644
--- a/redfish-core/lib/ethernet.hpp
+++ b/redfish-core/lib/ethernet.hpp
@@ -1219,15 +1219,17 @@
}
inline void
- handleIPv4StaticPatch(const std::string& ifaceId, nlohmann::json& input,
+ handleIPv4StaticPatch(const std::string& ifaceId,
+ nlohmann::json::array_t& input,
const std::vector<IPv4AddressData>& ipv4Data,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
- if ((!input.is_array()) || input.empty())
+ if (input.empty())
{
messages::propertyValueTypeError(
asyncResp->res,
- input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
+ nlohmann::json(input).dump(
+ 2, ' ', true, nlohmann::json::error_handler_t::replace),
"IPv4StaticAddresses");
return;
}
@@ -1423,15 +1425,16 @@
}
inline void handleIPv6StaticAddressesPatch(
- const std::string& ifaceId, const nlohmann::json& input,
+ const std::string& ifaceId, const nlohmann::json::array_t& input,
const std::vector<IPv6AddressData>& ipv6Data,
const std::shared_ptr<bmcweb::AsyncResp>& asyncResp)
{
- if (!input.is_array() || input.empty())
+ if (input.empty())
{
messages::propertyValueTypeError(
asyncResp->res,
- input.dump(2, ' ', true, nlohmann::json::error_handler_t::replace),
+ nlohmann::json(input).dump(
+ 2, ' ', true, nlohmann::json::error_handler_t::replace),
"IPv6StaticAddresses");
return;
}
@@ -1792,8 +1795,8 @@
std::optional<std::string> fqdn;
std::optional<std::string> macAddress;
std::optional<std::string> ipv6DefaultGateway;
- std::optional<nlohmann::json> ipv4StaticAddresses;
- std::optional<nlohmann::json> ipv6StaticAddresses;
+ std::optional<nlohmann::json::array_t> ipv4StaticAddresses;
+ std::optional<nlohmann::json::array_t> ipv6StaticAddresses;
std::optional<std::vector<std::string>> staticNameServers;
std::optional<nlohmann::json> dhcpv4;
std::optional<nlohmann::json> dhcpv6;
@@ -1893,7 +1896,7 @@
// out the intermedia nlohmann::json objects. This
// makes a copy of the structure, and operates on
// that, but could be done more efficiently
- nlohmann::json ipv4Static = *ipv4StaticAddresses;
+ nlohmann::json::array_t ipv4Static = *ipv4StaticAddresses;
handleIPv4StaticPatch(ifaceId, ipv4Static, ipv4Data, asyncResp);
}
@@ -1911,9 +1914,8 @@
if (ipv6StaticAddresses)
{
- const nlohmann::json& ipv6Static = *ipv6StaticAddresses;
- handleIPv6StaticAddressesPatch(ifaceId, ipv6Static, ipv6Data,
- asyncResp);
+ handleIPv6StaticAddressesPatch(ifaceId, *ipv6StaticAddresses,
+ ipv6Data, asyncResp);
}
if (interfaceEnabled)