xref: /openbmc/bmcweb/features/redfish/include/utils/json_utils.hpp (revision ee344e0fa76b2b24866d25b305e12ae82824f01b)
177dd8813SKowalski, Kamil /*
277dd8813SKowalski, Kamil // Copyright (c) 2018 Intel Corporation
377dd8813SKowalski, Kamil //
477dd8813SKowalski, Kamil // Licensed under the Apache License, Version 2.0 (the "License");
577dd8813SKowalski, Kamil // you may not use this file except in compliance with the License.
677dd8813SKowalski, Kamil // You may obtain a copy of the License at
777dd8813SKowalski, Kamil //
877dd8813SKowalski, Kamil //      http://www.apache.org/licenses/LICENSE-2.0
977dd8813SKowalski, Kamil //
1077dd8813SKowalski, Kamil // Unless required by applicable law or agreed to in writing, software
1177dd8813SKowalski, Kamil // distributed under the License is distributed on an "AS IS" BASIS,
1277dd8813SKowalski, Kamil // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1377dd8813SKowalski, Kamil // See the License for the specific language governing permissions and
1477dd8813SKowalski, Kamil // limitations under the License.
1577dd8813SKowalski, Kamil */
1677dd8813SKowalski, Kamil #pragma once
179712f8acSEd Tanous 
1877dd8813SKowalski, Kamil #include <crow/http_request.h>
1977dd8813SKowalski, Kamil #include <crow/http_response.h>
2077dd8813SKowalski, Kamil 
219712f8acSEd Tanous #include <bitset>
229712f8acSEd Tanous #include <error_messages.hpp>
231abe55efSEd Tanous #include <nlohmann/json.hpp>
240627a2c7SEd Tanous 
251abe55efSEd Tanous namespace redfish
261abe55efSEd Tanous {
271abe55efSEd Tanous 
281abe55efSEd Tanous namespace json_util
291abe55efSEd Tanous {
3077dd8813SKowalski, Kamil 
3177dd8813SKowalski, Kamil /**
3277dd8813SKowalski, Kamil  * @brief Processes request to extract JSON from its body. If it fails, adds
3377dd8813SKowalski, Kamil  *       MalformedJSON message to response and ends it.
3477dd8813SKowalski, Kamil  *
3577dd8813SKowalski, Kamil  * @param[io]  res       Response object
3677dd8813SKowalski, Kamil  * @param[in]  req       Request object
3777dd8813SKowalski, Kamil  * @param[out] reqJson   JSON object extracted from request's body
3877dd8813SKowalski, Kamil  *
3977dd8813SKowalski, Kamil  * @return true if JSON is valid, false when JSON is invalid and response has
4077dd8813SKowalski, Kamil  *         been filled with message and ended.
4177dd8813SKowalski, Kamil  */
4255c7b7a2SEd Tanous bool processJsonFromRequest(crow::Response& res, const crow::Request& req,
4377dd8813SKowalski, Kamil                             nlohmann::json& reqJson);
449712f8acSEd Tanous namespace details
459712f8acSEd Tanous {
46771cfa0fSJason M. Bills 
47771cfa0fSJason M. Bills template <typename Type> struct is_optional : std::false_type
489712f8acSEd Tanous {
499712f8acSEd Tanous };
509712f8acSEd Tanous 
51771cfa0fSJason M. Bills template <typename Type>
52a24526dcSEd Tanous struct is_optional<std::optional<Type>> : std::true_type
539712f8acSEd Tanous {
549712f8acSEd Tanous };
559712f8acSEd Tanous 
56771cfa0fSJason M. Bills template <typename Type>
57771cfa0fSJason M. Bills constexpr bool is_optional_v = is_optional<Type>::value;
58771cfa0fSJason M. Bills 
59b1556427SEd Tanous template <typename Type> struct is_vector : std::false_type
60b1556427SEd Tanous {
61b1556427SEd Tanous };
62b1556427SEd Tanous 
63b1556427SEd Tanous template <typename Type> struct is_vector<std::vector<Type>> : std::true_type
64b1556427SEd Tanous {
65b1556427SEd Tanous };
66b1556427SEd Tanous 
67b1556427SEd Tanous template <typename Type> constexpr bool is_vector_v = is_vector<Type>::value;
68b1556427SEd Tanous 
69318226c2SJames Feist template <typename Type> struct is_std_array : std::false_type
70318226c2SJames Feist {
71318226c2SJames Feist };
72318226c2SJames Feist 
73318226c2SJames Feist template <typename Type, std::size_t size>
74318226c2SJames Feist struct is_std_array<std::array<Type, size>> : std::true_type
75318226c2SJames Feist {
76318226c2SJames Feist };
77318226c2SJames Feist 
78318226c2SJames Feist template <typename Type>
79318226c2SJames Feist constexpr bool is_std_array_v = is_std_array<Type>::value;
80318226c2SJames Feist 
81a6acbb31SJames Feist template <typename ToType, typename FromType>
82*ee344e0fSEd Tanous bool checkRange(const FromType& from, const std::string& key,
83a6acbb31SJames Feist                 nlohmann::json& jsonValue, crow::Response& res)
84a6acbb31SJames Feist {
85*ee344e0fSEd Tanous     if (from > std::numeric_limits<ToType>::max())
86a6acbb31SJames Feist     {
87a6acbb31SJames Feist         BMCWEB_LOG_DEBUG << "Value for key " << key
88a6acbb31SJames Feist                          << " was greater than max: " << __PRETTY_FUNCTION__;
89a6acbb31SJames Feist         messages::propertyValueNotInList(res, jsonValue.dump(), key);
90a6acbb31SJames Feist         return false;
91a6acbb31SJames Feist     }
92*ee344e0fSEd Tanous     if (from < std::numeric_limits<ToType>::lowest())
93a6acbb31SJames Feist     {
94a6acbb31SJames Feist         BMCWEB_LOG_DEBUG << "Value for key " << key
95a6acbb31SJames Feist                          << " was less than min: " << __PRETTY_FUNCTION__;
96a6acbb31SJames Feist         messages::propertyValueNotInList(res, jsonValue.dump(), key);
97a6acbb31SJames Feist         return false;
98a6acbb31SJames Feist     }
99a6acbb31SJames Feist     if constexpr (std::is_floating_point_v<ToType>)
100a6acbb31SJames Feist     {
101*ee344e0fSEd Tanous         if (std::isnan(from))
102a6acbb31SJames Feist         {
103a6acbb31SJames Feist             BMCWEB_LOG_DEBUG << "Value for key " << key << " was NAN";
104a6acbb31SJames Feist             messages::propertyValueNotInList(res, jsonValue.dump(), key);
105a6acbb31SJames Feist             return false;
106a6acbb31SJames Feist         }
107a6acbb31SJames Feist     }
108a6acbb31SJames Feist 
109a6acbb31SJames Feist     return true;
110a6acbb31SJames Feist }
111a6acbb31SJames Feist 
112771cfa0fSJason M. Bills template <typename Type>
11341352c24SSantosh Puranik bool unpackValue(nlohmann::json& jsonValue, const std::string& key,
114771cfa0fSJason M. Bills                  crow::Response& res, Type& value)
115771cfa0fSJason M. Bills {
11641352c24SSantosh Puranik     bool ret = true;
11741352c24SSantosh Puranik 
118a6acbb31SJames Feist     if constexpr (std::is_floating_point_v<Type>)
119771cfa0fSJason M. Bills     {
120a6acbb31SJames Feist         double helper = 0;
121a6acbb31SJames Feist         double* jsonPtr = jsonValue.get_ptr<double*>();
122771cfa0fSJason M. Bills 
123771cfa0fSJason M. Bills         if (jsonPtr == nullptr)
124771cfa0fSJason M. Bills         {
125a6acbb31SJames Feist             int64_t* intPtr = jsonValue.get_ptr<int64_t*>();
126a6acbb31SJames Feist             if (intPtr != nullptr)
127771cfa0fSJason M. Bills             {
128a6acbb31SJames Feist                 helper = static_cast<double>(*intPtr);
129a6acbb31SJames Feist                 jsonPtr = &helper;
130771cfa0fSJason M. Bills             }
131a6acbb31SJames Feist         }
132*ee344e0fSEd Tanous         if (!checkRange<Type>(*jsonPtr, key, jsonValue, res))
133771cfa0fSJason M. Bills         {
13441352c24SSantosh Puranik             return false;
135771cfa0fSJason M. Bills         }
136771cfa0fSJason M. Bills         value = static_cast<Type>(*jsonPtr);
137771cfa0fSJason M. Bills     }
138a6acbb31SJames Feist 
139a6acbb31SJames Feist     else if constexpr (std::is_signed_v<Type>)
140a6acbb31SJames Feist     {
141a6acbb31SJames Feist         int64_t* jsonPtr = jsonValue.get_ptr<int64_t*>();
142*ee344e0fSEd Tanous         if (!checkRange<Type>(*jsonPtr, key, jsonValue, res))
143a6acbb31SJames Feist         {
14441352c24SSantosh Puranik             return false;
145a6acbb31SJames Feist         }
146a6acbb31SJames Feist         value = static_cast<Type>(*jsonPtr);
147a6acbb31SJames Feist     }
148a6acbb31SJames Feist 
1498102ddbaSAppaRao Puli     else if constexpr ((std::is_unsigned_v<Type>)&&(
1508102ddbaSAppaRao Puli                            !std::is_same_v<bool, Type>))
151a6acbb31SJames Feist     {
152a6acbb31SJames Feist         uint64_t* jsonPtr = jsonValue.get_ptr<uint64_t*>();
153*ee344e0fSEd Tanous         if (!checkRange<Type>(*jsonPtr, key, jsonValue, res))
154a6acbb31SJames Feist         {
15541352c24SSantosh Puranik             return false;
156a6acbb31SJames Feist         }
157a6acbb31SJames Feist         value = static_cast<Type>(*jsonPtr);
158a6acbb31SJames Feist     }
159a6acbb31SJames Feist 
160771cfa0fSJason M. Bills     else if constexpr (is_optional_v<Type>)
161771cfa0fSJason M. Bills     {
162771cfa0fSJason M. Bills         value.emplace();
16341352c24SSantosh Puranik         ret = unpackValue<typename Type::value_type>(jsonValue, key, res,
16441352c24SSantosh Puranik                                                      *value) &&
16541352c24SSantosh Puranik               ret;
166771cfa0fSJason M. Bills     }
1670627a2c7SEd Tanous     else if constexpr (std::is_same_v<nlohmann::json, Type>)
1680627a2c7SEd Tanous     {
1690627a2c7SEd Tanous         // Must be a complex type.  Simple types (int string etc) should be
1700627a2c7SEd Tanous         // unpacked directly
1710627a2c7SEd Tanous         if (!jsonValue.is_object() && !jsonValue.is_array())
1720627a2c7SEd Tanous         {
1730627a2c7SEd Tanous             messages::propertyValueTypeError(res, jsonValue.dump(), key);
17441352c24SSantosh Puranik             return false;
1750627a2c7SEd Tanous         }
1760627a2c7SEd Tanous 
1770627a2c7SEd Tanous         value = std::move(jsonValue);
1780627a2c7SEd Tanous     }
179318226c2SJames Feist     else if constexpr (is_std_array_v<Type>)
180318226c2SJames Feist     {
181318226c2SJames Feist         if (!jsonValue.is_array())
182318226c2SJames Feist         {
183318226c2SJames Feist             messages::propertyValueTypeError(res, res.jsonValue.dump(), key);
18441352c24SSantosh Puranik             return false;
185318226c2SJames Feist         }
186318226c2SJames Feist         if (jsonValue.size() != value.size())
187318226c2SJames Feist         {
188318226c2SJames Feist             messages::propertyValueTypeError(res, res.jsonValue.dump(), key);
18941352c24SSantosh Puranik             return false;
190318226c2SJames Feist         }
191318226c2SJames Feist         size_t index = 0;
192318226c2SJames Feist         for (const auto& val : jsonValue.items())
193318226c2SJames Feist         {
19441352c24SSantosh Puranik             ret = unpackValue<typename Type::value_type>(val.value(), key, res,
19541352c24SSantosh Puranik                                                          value[index++]) &&
19641352c24SSantosh Puranik                   ret;
197318226c2SJames Feist         }
198318226c2SJames Feist     }
199b1556427SEd Tanous     else if constexpr (is_vector_v<Type>)
200b1556427SEd Tanous     {
201b1556427SEd Tanous         if (!jsonValue.is_array())
202b1556427SEd Tanous         {
203b1556427SEd Tanous             messages::propertyValueTypeError(res, res.jsonValue.dump(), key);
20441352c24SSantosh Puranik             return false;
205b1556427SEd Tanous         }
206b1556427SEd Tanous 
207b1556427SEd Tanous         for (const auto& val : jsonValue.items())
208b1556427SEd Tanous         {
209b1556427SEd Tanous             value.emplace_back();
21041352c24SSantosh Puranik             ret = unpackValue<typename Type::value_type>(val.value(), key, res,
21141352c24SSantosh Puranik                                                          value.back()) &&
21241352c24SSantosh Puranik                   ret;
213b1556427SEd Tanous         }
214b1556427SEd Tanous     }
215771cfa0fSJason M. Bills     else
216771cfa0fSJason M. Bills     {
217771cfa0fSJason M. Bills         using JsonType = std::add_const_t<std::add_pointer_t<Type>>;
218771cfa0fSJason M. Bills         JsonType jsonPtr = jsonValue.get_ptr<JsonType>();
219771cfa0fSJason M. Bills         if (jsonPtr == nullptr)
220771cfa0fSJason M. Bills         {
221771cfa0fSJason M. Bills             BMCWEB_LOG_DEBUG
222771cfa0fSJason M. Bills                 << "Value for key " << key
223771cfa0fSJason M. Bills                 << " was incorrect type: " << jsonValue.type_name();
224771cfa0fSJason M. Bills             messages::propertyValueTypeError(res, jsonValue.dump(), key);
22541352c24SSantosh Puranik             return false;
226771cfa0fSJason M. Bills         }
227771cfa0fSJason M. Bills         value = std::move(*jsonPtr);
228771cfa0fSJason M. Bills     }
22941352c24SSantosh Puranik     return ret;
230771cfa0fSJason M. Bills }
231771cfa0fSJason M. Bills 
2329712f8acSEd Tanous template <size_t Count, size_t Index>
23341352c24SSantosh Puranik bool readJsonValues(const std::string& key, nlohmann::json& jsonValue,
2349712f8acSEd Tanous                     crow::Response& res, std::bitset<Count>& handled)
2359712f8acSEd Tanous {
2369712f8acSEd Tanous     BMCWEB_LOG_DEBUG << "Unable to find variable for key" << key;
237a08b46ccSJason M. Bills     messages::propertyUnknown(res, key);
23841352c24SSantosh Puranik     return false;
2399712f8acSEd Tanous }
2409712f8acSEd Tanous 
2419712f8acSEd Tanous template <size_t Count, size_t Index, typename ValueType,
2429712f8acSEd Tanous           typename... UnpackTypes>
24341352c24SSantosh Puranik bool readJsonValues(const std::string& key, nlohmann::json& jsonValue,
2449712f8acSEd Tanous                     crow::Response& res, std::bitset<Count>& handled,
2459712f8acSEd Tanous                     const char* keyToCheck, ValueType& valueToFill,
2469712f8acSEd Tanous                     UnpackTypes&... in)
2479712f8acSEd Tanous {
24841352c24SSantosh Puranik     bool ret = true;
2499712f8acSEd Tanous     if (key != keyToCheck)
2509712f8acSEd Tanous     {
25141352c24SSantosh Puranik         ret = readJsonValues<Count, Index + 1>(key, jsonValue, res, handled,
25241352c24SSantosh Puranik                                                in...) &&
25341352c24SSantosh Puranik               ret;
25441352c24SSantosh Puranik         return ret;
2559712f8acSEd Tanous     }
2569712f8acSEd Tanous 
2579712f8acSEd Tanous     handled.set(Index);
2589712f8acSEd Tanous 
25941352c24SSantosh Puranik     return unpackValue<ValueType>(jsonValue, key, res, valueToFill) && ret;
2609712f8acSEd Tanous }
2619712f8acSEd Tanous 
2629712f8acSEd Tanous template <size_t Index = 0, size_t Count>
26341352c24SSantosh Puranik bool handleMissing(std::bitset<Count>& handled, crow::Response& res)
2649712f8acSEd Tanous {
26541352c24SSantosh Puranik     return true;
2669712f8acSEd Tanous }
2679712f8acSEd Tanous 
2689712f8acSEd Tanous template <size_t Index = 0, size_t Count, typename ValueType,
2699712f8acSEd Tanous           typename... UnpackTypes>
27041352c24SSantosh Puranik bool handleMissing(std::bitset<Count>& handled, crow::Response& res,
2719712f8acSEd Tanous                    const char* key, ValueType& unused, UnpackTypes&... in)
2729712f8acSEd Tanous {
27341352c24SSantosh Puranik     bool ret = true;
274771cfa0fSJason M. Bills     if (!handled.test(Index) && !is_optional_v<ValueType>)
2759712f8acSEd Tanous     {
27641352c24SSantosh Puranik         ret = false;
277a08b46ccSJason M. Bills         messages::propertyMissing(res, key);
2789712f8acSEd Tanous     }
27941352c24SSantosh Puranik     return details::handleMissing<Index + 1, Count>(handled, res, in...) && ret;
2809712f8acSEd Tanous }
2819712f8acSEd Tanous } // namespace details
2829712f8acSEd Tanous 
2839712f8acSEd Tanous template <typename... UnpackTypes>
2840627a2c7SEd Tanous bool readJson(nlohmann::json& jsonRequest, crow::Response& res, const char* key,
2859712f8acSEd Tanous               UnpackTypes&... in)
2869712f8acSEd Tanous {
28741352c24SSantosh Puranik     bool result = true;
2889712f8acSEd Tanous     if (!jsonRequest.is_object())
2899712f8acSEd Tanous     {
2909712f8acSEd Tanous         BMCWEB_LOG_DEBUG << "Json value is not an object";
291f12894f8SJason M. Bills         messages::unrecognizedRequestBody(res);
2929712f8acSEd Tanous         return false;
2939712f8acSEd Tanous     }
2949712f8acSEd Tanous 
2959712f8acSEd Tanous     if (jsonRequest.empty())
2969712f8acSEd Tanous     {
2979712f8acSEd Tanous         BMCWEB_LOG_DEBUG << "Json value is empty";
298f12894f8SJason M. Bills         messages::emptyJSON(res);
2999712f8acSEd Tanous         return false;
3009712f8acSEd Tanous     }
3019712f8acSEd Tanous 
3029712f8acSEd Tanous     std::bitset<(sizeof...(in) + 1) / 2> handled(0);
3039712f8acSEd Tanous     for (const auto& item : jsonRequest.items())
3049712f8acSEd Tanous     {
30541352c24SSantosh Puranik         result =
3069712f8acSEd Tanous             details::readJsonValues<(sizeof...(in) + 1) / 2, 0, UnpackTypes...>(
30741352c24SSantosh Puranik                 item.key(), item.value(), res, handled, key, in...) &&
30841352c24SSantosh Puranik             result;
3099712f8acSEd Tanous     }
3109712f8acSEd Tanous 
31141352c24SSantosh Puranik     BMCWEB_LOG_DEBUG << "JSON result is: " << result;
3129712f8acSEd Tanous 
31341352c24SSantosh Puranik     return details::handleMissing(handled, res, key, in...) && result;
3149712f8acSEd Tanous }
31577dd8813SKowalski, Kamil 
3160627a2c7SEd Tanous template <typename... UnpackTypes>
3170627a2c7SEd Tanous bool readJson(const crow::Request& req, crow::Response& res, const char* key,
3180627a2c7SEd Tanous               UnpackTypes&... in)
3190627a2c7SEd Tanous {
3200627a2c7SEd Tanous     nlohmann::json jsonRequest;
3210627a2c7SEd Tanous     if (!json_util::processJsonFromRequest(res, req, jsonRequest))
3220627a2c7SEd Tanous     {
3230627a2c7SEd Tanous         BMCWEB_LOG_DEBUG << "Json value not readable";
3240627a2c7SEd Tanous         return false;
3250627a2c7SEd Tanous     }
3260627a2c7SEd Tanous     return readJson(jsonRequest, res, key, in...);
3270627a2c7SEd Tanous }
3280627a2c7SEd Tanous 
32977dd8813SKowalski, Kamil } // namespace json_util
33077dd8813SKowalski, Kamil } // namespace redfish
331