diff options
author | Ed Tanous <ed@tanous.net> | 2024-03-27 03:30:42 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2024-04-16 20:46:51 +0300 |
commit | ed4de7a8a5601b58b2e29824b10bf7fcc699a1b8 (patch) | |
tree | 95cca38853cc99a33ce145e3f0fa4f534f6677b1 /redfish-core/include | |
parent | 5910d945ab14872a5f22a0222d146783c9e4c4c5 (diff) | |
download | bmcweb-ed4de7a8a5601b58b2e29824b10bf7fcc699a1b8.tar.xz |
Add type safety for NTP server objects
NTPServers is our last usage of nlohmann::json in a readJson unpack.
The capability and unit tests are left in place for that type in case we
need them in the future, but for now, document them as deprecated.
Tested: Redfish service validator passes. Redfish protocol validator
passes most tests (1 known failure in SSE is unrelated to this change).
Change-Id: If4b2ea061a941cc23d47189af7ff453094dc7dca
Signed-off-by: Ed Tanous <ed@tanous.net>
Diffstat (limited to 'redfish-core/include')
-rw-r--r-- | redfish-core/include/utils/json_utils.hpp | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/redfish-core/include/utils/json_utils.hpp b/redfish-core/include/utils/json_utils.hpp index d7d87a60cf..a013847872 100644 --- a/redfish-core/include/utils/json_utils.hpp +++ b/redfish-core/include/utils/json_utils.hpp @@ -151,7 +151,7 @@ UnpackErrorCode unpackValueVariant(nlohmann::json& j, std::string_view key, { if constexpr (Index < std::variant_size_v<std::variant<Args...>>) { - std::variant_alternative_t<Index, std::variant<Args...>> type; + std::variant_alternative_t<Index, std::variant<Args...>> type{}; UnpackErrorCode unpack = unpackValueWithErrorCode(j, key, type); if (unpack == UnpackErrorCode::success) { @@ -235,6 +235,23 @@ UnpackErrorCode unpackValueWithErrorCode(nlohmann::json& jsonValue, return UnpackErrorCode::invalidType; } } + else if constexpr (IsVector<Type>::value) + { + nlohmann::json::object_t* obj = + jsonValue.get_ptr<nlohmann::json::object_t*>(); + if (obj == nullptr) + { + return UnpackErrorCode::invalidType; + } + + for (const auto& val : *obj) + { + value.emplace_back(); + ret = unpackValueWithErrorCode<typename Type::value_type>( + val, key, value.back()) && + ret; + } + } else { using JsonType = std::add_const_t<std::add_pointer_t<Type>>; @@ -403,7 +420,6 @@ using UnpackVariant = std::variant< bool*, double*, std::string*, - nlohmann::json*, nlohmann::json::object_t*, std::variant<std::string, std::nullptr_t>*, std::variant<uint8_t, std::nullptr_t>*, @@ -425,7 +441,6 @@ using UnpackVariant = std::variant< //std::vector<bool>*, std::vector<double>*, std::vector<std::string>*, - std::vector<nlohmann::json>*, std::vector<nlohmann::json::object_t>*, std::optional<uint8_t>*, std::optional<uint16_t>*, @@ -437,7 +452,6 @@ using UnpackVariant = std::variant< std::optional<bool>*, std::optional<double>*, std::optional<std::string>*, - std::optional<nlohmann::json>*, std::optional<nlohmann::json::object_t>*, std::optional<std::vector<uint8_t>>*, std::optional<std::vector<uint16_t>>*, @@ -449,7 +463,6 @@ using UnpackVariant = std::variant< //std::optional<std::vector<bool>>*, std::optional<std::vector<double>>*, std::optional<std::vector<std::string>>*, - std::optional<std::vector<nlohmann::json>>*, std::optional<std::vector<nlohmann::json::object_t>>*, std::optional<std::variant<std::string, std::nullptr_t>>*, std::optional<std::variant<uint8_t, std::nullptr_t>>*, @@ -462,7 +475,15 @@ using UnpackVariant = std::variant< std::optional<std::variant<double, std::nullptr_t>>*, std::optional<std::variant<bool, std::nullptr_t>>*, std::optional<std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>*, - std::optional<std::variant<nlohmann::json::object_t, std::nullptr_t>>* + std::optional<std::vector<std::variant<std::string, nlohmann::json::object_t, std::nullptr_t>>>*, + + // Note, these types are kept for historical completeness, but should not be used, + // As they do not provide object type safety. Instead, rely on nlohmann::json::object_t + // Will be removed Q2 2025 + nlohmann::json*, + std::optional<std::vector<nlohmann::json>>*, + std::vector<nlohmann::json>*, + std::optional<nlohmann::json>* >; // clang-format on |