From b6164cbec4dd7f5c4e6e7667b203874e11cd8b3c Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Wed, 6 Mar 2024 12:04:01 -0800 Subject: Make readJson accept object_t Redfish supports several type systems for json. This makes parsing into proper types a challenge. Nlohmann supports 3 core data types, nlohmann::json, which supports all json types (float, int, array, object). Nlohmann::json::object_t, which is a specific typedef of std::map, and nlohmann::json::array_t, which is a specific typedef of std::map. Redfish allows reading our arrays of complex objects, similar to NtpServers: [null, {}, "string"] Which makes it a challenge to support. This commit allows parsing out objects as a nlohmann::object_t, which gives the ability to later use it in a type safe manner, without having to call get_ptr. Tested: Unit tests pass. Change-Id: I4134338951ce27c2f56841a45b56bc64ad1753db Signed-off-by: Ed Tanous --- redfish-core/include/utils/json_utils.hpp | 84 +++++++++++++++++----- .../redfish-core/include/utils/json_utils_test.cpp | 21 ++++++ 2 files changed, 87 insertions(+), 18 deletions(-) diff --git a/redfish-core/include/utils/json_utils.hpp b/redfish-core/include/utils/json_utils.hpp index 82f1fe2b64..c8af2d15db 100644 --- a/redfish-core/include/utils/json_utils.hpp +++ b/redfish-core/include/utils/json_utils.hpp @@ -341,6 +341,7 @@ using UnpackVariant = std::variant< double*, std::string*, nlohmann::json*, + nlohmann::json::object_t*, std::vector*, std::vector*, std::vector*, @@ -352,6 +353,7 @@ using UnpackVariant = std::variant< std::vector*, std::vector*, std::vector*, + std::vector*, std::optional*, std::optional*, std::optional*, @@ -363,6 +365,7 @@ using UnpackVariant = std::variant< std::optional*, std::optional*, std::optional*, + std::optional*, std::optional>*, std::optional>*, std::optional>*, @@ -373,7 +376,8 @@ using UnpackVariant = std::variant< //std::optional>*, std::optional>*, std::optional>*, - std::optional>* + std::optional>*, + std::optional>* >; // clang-format on @@ -385,18 +389,14 @@ struct PerUnpack }; inline bool readJsonHelper(nlohmann::json& jsonRequest, crow::Response& res, - std::span toUnpack) + std::span toUnpack); + +inline bool readJsonHelperObject(nlohmann::json::object_t& obj, + crow::Response& res, + std::span toUnpack) { bool result = true; - nlohmann::json::object_t* obj = - jsonRequest.get_ptr(); - if (obj == nullptr) - { - BMCWEB_LOG_DEBUG("Json value is not an object"); - messages::unrecognizedRequestBody(res); - return false; - } - for (auto& item : *obj) + for (auto& item : obj) { size_t unpackIndex = 0; for (; unpackIndex < toUnpack.size(); unpackIndex++) @@ -489,6 +489,20 @@ inline bool readJsonHelper(nlohmann::json& jsonRequest, crow::Response& res, return result; } +inline bool readJsonHelper(nlohmann::json& jsonRequest, crow::Response& res, + std::span toUnpack) +{ + nlohmann::json::object_t* obj = + jsonRequest.get_ptr(); + if (obj == nullptr) + { + BMCWEB_LOG_DEBUG("Json value is not an object"); + messages::unrecognizedRequestBody(res); + return false; + } + return readJsonHelperObject(*obj, res, toUnpack); +} + inline void packVariant(std::span /*toPack*/) {} template @@ -506,16 +520,32 @@ void packVariant(std::span toPack, std::string_view key, } template -bool readJson(nlohmann::json& jsonRequest, crow::Response& res, - std::string_view key, FirstType&& first, UnpackTypes&&... in) +bool readJsonObject(nlohmann::json::object_t& jsonRequest, crow::Response& res, + std::string_view key, FirstType&& first, + UnpackTypes&&... in) { const std::size_t n = sizeof...(UnpackTypes) + 2; std::array toUnpack2; packVariant(toUnpack2, key, first, std::forward(in)...); - return readJsonHelper(jsonRequest, res, toUnpack2); + return readJsonHelperObject(jsonRequest, res, toUnpack2); +} + +template +bool readJson(nlohmann::json& jsonRequest, crow::Response& res, + std::string_view key, FirstType&& first, UnpackTypes&&... in) +{ + nlohmann::json::object_t* obj = + jsonRequest.get_ptr(); + if (obj == nullptr) + { + BMCWEB_LOG_DEBUG("Json value is not an object"); + messages::unrecognizedRequestBody(res); + return false; + } + return readJsonObject(*obj, res, key, first, in...); } -inline std::optional +inline std::optional readJsonPatchHelper(const crow::Request& req, crow::Response& res) { nlohmann::json jsonRequest; @@ -545,7 +575,7 @@ inline std::optional return std::nullopt; } - return {std::move(jsonRequest)}; + return {std::move(*object)}; } template @@ -557,8 +587,17 @@ bool readJsonPatch(const crow::Request& req, crow::Response& res, { return false; } + nlohmann::json::object_t* object = + jsonRequest->get_ptr(); + if (object == nullptr) + { + BMCWEB_LOG_DEBUG("Json value is empty"); + messages::emptyJSON(res); + return false; + } - return readJson(*jsonRequest, res, key, std::forward(in)...); + return readJsonObject(*object, res, key, + std::forward(in)...); } template @@ -571,7 +610,16 @@ bool readJsonAction(const crow::Request& req, crow::Response& res, BMCWEB_LOG_DEBUG("Json value not readable"); return false; } - return readJson(jsonRequest, res, key, std::forward(in)...); + nlohmann::json::object_t* object = + jsonRequest.get_ptr(); + if (object == nullptr) + { + BMCWEB_LOG_DEBUG("Json value is empty"); + messages::emptyJSON(res); + return false; + } + return readJsonObject(*object, res, key, + std::forward(in)...); } // Determines if two json objects are less, based on the presence of the diff --git a/test/redfish-core/include/utils/json_utils_test.cpp b/test/redfish-core/include/utils/json_utils_test.cpp index 5485bba91e..ad4d80538d 100644 --- a/test/redfish-core/include/utils/json_utils_test.cpp +++ b/test/redfish-core/include/utils/json_utils_test.cpp @@ -48,6 +48,27 @@ TEST(ReadJson, ValidElementsReturnsTrueResponseOkValuesUnpackedCorrectly) EXPECT_THAT(vec, ElementsAre(1, 2, 3)); } +TEST(ReadJson, ValidObjectElementsReturnsTrueResponseOkValuesUnpackedCorrectly) +{ + crow::Response res; + nlohmann::json::object_t jsonRequest; + jsonRequest["integer"] = 1; + jsonRequest["string"] = "hello"; + jsonRequest["vector"] = std::vector{1, 2, 3}; + + int64_t integer = 0; + std::string str; + std::vector vec; + ASSERT_TRUE(readJsonObject(jsonRequest, res, "integer", integer, "string", + str, "vector", vec)); + EXPECT_EQ(res.result(), boost::beast::http::status::ok); + EXPECT_THAT(res.jsonValue, IsEmpty()); + + EXPECT_EQ(integer, 1); + EXPECT_EQ(str, "hello"); + EXPECT_THAT(vec, ElementsAre(1, 2, 3)); +} + TEST(readJson, ExtraElementsReturnsFalseReponseIsBadRequest) { crow::Response res; -- cgit v1.2.3