diff options
author | Ed Tanous <edtanous@google.com> | 2023-11-01 21:36:15 +0300 |
---|---|---|
committer | Ed Tanous <ed@tanous.net> | 2024-04-04 01:51:36 +0300 |
commit | 8099c51796bf6f94ad5fbb1f6844d700f498d3bb (patch) | |
tree | 5d99081be81b028b1756622015332de872ef661f /test | |
parent | 8e5cc7bdbf8727e9527d23973f81702799e80bd8 (diff) | |
download | bmcweb-8099c51796bf6f94ad5fbb1f6844d700f498d3bb.tar.xz |
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 <edtanous@google.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/redfish-core/include/utils/json_utils_test.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
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 <boost/beast/http/status.hpp> #include <nlohmann/json.hpp> +#include <cstddef> #include <cstdint> #include <optional> #include <string> #include <system_error> +#include <variant> #include <vector> #include <gmock/gmock.h> // 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<std::string, std::nullptr_t> str; + + ASSERT_TRUE(readJson(jsonRequest, res, "nullval", str)); + EXPECT_EQ(res.result(), boost::beast::http::status::ok); + + EXPECT_TRUE(std::holds_alternative<std::nullptr_t>(str)); +} + +TEST(ReadJson, VariantValueUnpackedValue) +{ + crow::Response res; + nlohmann::json jsonRequest = {{"stringval", "mystring"}}; + + std::variant<std::string, std::nullptr_t> str; + + ASSERT_TRUE(readJson(jsonRequest, res, "stringval", str)); + EXPECT_EQ(res.result(), boost::beast::http::status::ok); + + ASSERT_TRUE(std::holds_alternative<std::string>(str)); + EXPECT_EQ(std::get<std::string>(str), "mystring"); +} + TEST(readJson, ExtraElementsReturnsFalseReponseIsBadRequest) { crow::Response res; |