From 8099c51796bf6f94ad5fbb1f6844d700f498d3bb Mon Sep 17 00:00:00 2001 From: Ed Tanous Date: Wed, 1 Nov 2023 11:36:15 -0700 Subject: Allow parsing null or value classes In Redfish schema, just about all values can be a type (string, EDM.Numeric, etc) or null. Most APIs don't allow explicitly setting null, but there are a few cases where it is useful, namely in lists, where an an empty object {} keeps the value the same, and null deletes the value from the list. Previously we handled this by unpacking as nlohmann::json, but this allowed things like [1.0, {}] to pass the check for an array of string values. We'd ideally like to reject the 1.0 at the first stage, as well as reduce the number of tiered readJson calls that we make. This commit introducess support for unpacking std::variant types, that allows unpacking a known type, or explicitly allowing null, by unpacking std::nullptr_t. Tested: Unit tests pass. Change-Id: Ic7451877c824ac743faf1951cc2b5d9f8df8019c Signed-off-by: Ed Tanous --- .../redfish-core/include/utils/json_utils_test.cpp | 29 ++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'test') diff --git a/test/redfish-core/include/utils/json_utils_test.cpp b/test/redfish-core/include/utils/json_utils_test.cpp index 5ef80eeba6..cf5929756b 100644 --- a/test/redfish-core/include/utils/json_utils_test.cpp +++ b/test/redfish-core/include/utils/json_utils_test.cpp @@ -6,10 +6,12 @@ #include #include +#include #include #include #include #include +#include #include #include // IWYU pragma: keep @@ -70,6 +72,33 @@ TEST(ReadJson, ValidObjectElementsReturnsTrueResponseOkValuesUnpackedCorrectly) EXPECT_THAT(vec, ElementsAre(1, 2, 3)); } +TEST(ReadJson, VariantValueUnpackedNull) +{ + crow::Response res; + nlohmann::json jsonRequest = {{"nullval", nullptr}}; + + std::variant str; + + ASSERT_TRUE(readJson(jsonRequest, res, "nullval", str)); + EXPECT_EQ(res.result(), boost::beast::http::status::ok); + + EXPECT_TRUE(std::holds_alternative(str)); +} + +TEST(ReadJson, VariantValueUnpackedValue) +{ + crow::Response res; + nlohmann::json jsonRequest = {{"stringval", "mystring"}}; + + std::variant str; + + ASSERT_TRUE(readJson(jsonRequest, res, "stringval", str)); + EXPECT_EQ(res.result(), boost::beast::http::status::ok); + + ASSERT_TRUE(std::holds_alternative(str)); + EXPECT_EQ(std::get(str), "mystring"); +} + TEST(readJson, ExtraElementsReturnsFalseReponseIsBadRequest) { crow::Response res; -- cgit v1.2.3