Omit using subscripting operator in json utils
Using operator[] as a result of expression is a common mistake in c++.
First it creates a key in container with empty value than execute an
expression and assign result from it to the key in container. The result
of it is that object is created two times.
Fixes https://github.com/openbmc/bmcweb/issues/139
Tested:
Used:
curl -vvv -X POST -d "{
\"EventType\": \"Alert\",
\"EventId\": \"TestEventId\",
\"EventTimestamp\": \"2017-08-08T08:24:00Z\",
\"Severity\": \"TestSeverity\",
\"Message\": \"TestMessage\",
\"MessageId\": \"TestMessageId\",
\"MessageArgs\": [ \"TestMessageArg\" ],
\"OriginOfCondition\": \"/redfish/v1/\"
}" --insecure --user root:0penBmc https://192.168.7.2/redfish/v1/EventService/Actions/EventService.SubmitTestEven
To send test event. Call returned 204 as expected.
Signed-off-by: Ed Tanous <ed@tanous.net>
Change-Id: Idf44829bfb25daf216003f591d354df65ccecb18
diff --git a/redfish-core/include/utils/json_utils.hpp b/redfish-core/include/utils/json_utils.hpp
index c355000..b794af7 100644
--- a/redfish-core/include/utils/json_utils.hpp
+++ b/redfish-core/include/utils/json_utils.hpp
@@ -415,15 +415,14 @@
bool getValueFromJsonObject(nlohmann::json& jsonData, const std::string& key,
Type& value)
{
- nlohmann::json jsonValue = jsonData[key];
- if (jsonValue.is_null())
+ nlohmann::json::iterator it = jsonData.find(key);
+ if (it == jsonData.end())
{
BMCWEB_LOG_DEBUG << "Key " << key << " not exist";
return false;
}
- return details::unpackValue(jsonValue, key, value);
+ return details::unpackValue(*it, key, value);
}
-
} // namespace json_util
} // namespace redfish