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 
18*d5c80ad9SNan Zhou #include "error_messages.hpp"
19*d5c80ad9SNan Zhou #include "http_request.hpp"
20*d5c80ad9SNan Zhou #include "http_response.hpp"
21*d5c80ad9SNan Zhou #include "logging.hpp"
22*d5c80ad9SNan Zhou #include "nlohmann/json.hpp"
230627a2c7SEd Tanous 
24*d5c80ad9SNan Zhou #include <array>
25*d5c80ad9SNan Zhou #include <cmath>
26*d5c80ad9SNan Zhou #include <cstddef>
27*d5c80ad9SNan Zhou #include <cstdint>
28*d5c80ad9SNan Zhou #include <limits>
29*d5c80ad9SNan Zhou #include <map>
30*d5c80ad9SNan Zhou #include <optional>
31ea2e6eecSWilly Tu #include <span>
32*d5c80ad9SNan Zhou #include <string>
33*d5c80ad9SNan Zhou #include <string_view>
34*d5c80ad9SNan Zhou #include <type_traits>
35*d5c80ad9SNan Zhou #include <utility>
36*d5c80ad9SNan Zhou #include <variant>
37*d5c80ad9SNan Zhou #include <vector>
38*d5c80ad9SNan Zhou 
39*d5c80ad9SNan Zhou // IWYU pragma: no_include <stdint.h>
40*d5c80ad9SNan Zhou 
41*d5c80ad9SNan Zhou namespace crow
42*d5c80ad9SNan Zhou {
43*d5c80ad9SNan Zhou struct Request;
44*d5c80ad9SNan Zhou } // namespace crow
451214b7e7SGunnar Mills 
461abe55efSEd Tanous namespace redfish
471abe55efSEd Tanous {
481abe55efSEd Tanous 
491abe55efSEd Tanous namespace json_util
501abe55efSEd Tanous {
5177dd8813SKowalski, Kamil 
5277dd8813SKowalski, Kamil /**
5377dd8813SKowalski, Kamil  * @brief Processes request to extract JSON from its body. If it fails, adds
5477dd8813SKowalski, Kamil  *       MalformedJSON message to response and ends it.
5577dd8813SKowalski, Kamil  *
5677dd8813SKowalski, Kamil  * @param[io]  res       Response object
5777dd8813SKowalski, Kamil  * @param[in]  req       Request object
5877dd8813SKowalski, Kamil  * @param[out] reqJson   JSON object extracted from request's body
5977dd8813SKowalski, Kamil  *
6077dd8813SKowalski, Kamil  * @return true if JSON is valid, false when JSON is invalid and response has
6177dd8813SKowalski, Kamil  *         been filled with message and ended.
6277dd8813SKowalski, Kamil  */
6355c7b7a2SEd Tanous bool processJsonFromRequest(crow::Response& res, const crow::Request& req,
6477dd8813SKowalski, Kamil                             nlohmann::json& reqJson);
659712f8acSEd Tanous namespace details
669712f8acSEd Tanous {
67771cfa0fSJason M. Bills 
681214b7e7SGunnar Mills template <typename Type>
692c70f800SEd Tanous struct IsOptional : std::false_type
701214b7e7SGunnar Mills {};
719712f8acSEd Tanous 
72771cfa0fSJason M. Bills template <typename Type>
732c70f800SEd Tanous struct IsOptional<std::optional<Type>> : std::true_type
741214b7e7SGunnar Mills {};
759712f8acSEd Tanous 
76771cfa0fSJason M. Bills template <typename Type>
772c70f800SEd Tanous struct IsVector : std::false_type
781214b7e7SGunnar Mills {};
79b1556427SEd Tanous 
801214b7e7SGunnar Mills template <typename Type>
812c70f800SEd Tanous struct IsVector<std::vector<Type>> : std::true_type
821214b7e7SGunnar Mills {};
83b1556427SEd Tanous 
841214b7e7SGunnar Mills template <typename Type>
852c70f800SEd Tanous struct IsStdArray : std::false_type
861214b7e7SGunnar Mills {};
87318226c2SJames Feist 
88318226c2SJames Feist template <typename Type, std::size_t size>
892c70f800SEd Tanous struct IsStdArray<std::array<Type, size>> : std::true_type
901214b7e7SGunnar Mills {};
91318226c2SJames Feist 
92471a5eb8SAppaRao Puli enum class UnpackErrorCode
93471a5eb8SAppaRao Puli {
94471a5eb8SAppaRao Puli     success,
95471a5eb8SAppaRao Puli     invalidType,
96471a5eb8SAppaRao Puli     outOfRange
97471a5eb8SAppaRao Puli };
98471a5eb8SAppaRao Puli 
99a6acbb31SJames Feist template <typename ToType, typename FromType>
100ea2e6eecSWilly Tu bool checkRange(const FromType& from, std::string_view key)
101a6acbb31SJames Feist {
102ee344e0fSEd Tanous     if (from > std::numeric_limits<ToType>::max())
103a6acbb31SJames Feist     {
104a6acbb31SJames Feist         BMCWEB_LOG_DEBUG << "Value for key " << key
105a6acbb31SJames Feist                          << " was greater than max: " << __PRETTY_FUNCTION__;
106a6acbb31SJames Feist         return false;
107a6acbb31SJames Feist     }
108ee344e0fSEd Tanous     if (from < std::numeric_limits<ToType>::lowest())
109a6acbb31SJames Feist     {
110a6acbb31SJames Feist         BMCWEB_LOG_DEBUG << "Value for key " << key
111a6acbb31SJames Feist                          << " was less than min: " << __PRETTY_FUNCTION__;
112a6acbb31SJames Feist         return false;
113a6acbb31SJames Feist     }
114a6acbb31SJames Feist     if constexpr (std::is_floating_point_v<ToType>)
115a6acbb31SJames Feist     {
116ee344e0fSEd Tanous         if (std::isnan(from))
117a6acbb31SJames Feist         {
118a6acbb31SJames Feist             BMCWEB_LOG_DEBUG << "Value for key " << key << " was NAN";
119a6acbb31SJames Feist             return false;
120a6acbb31SJames Feist         }
121a6acbb31SJames Feist     }
122a6acbb31SJames Feist 
123a6acbb31SJames Feist     return true;
124a6acbb31SJames Feist }
125a6acbb31SJames Feist 
126771cfa0fSJason M. Bills template <typename Type>
127471a5eb8SAppaRao Puli UnpackErrorCode unpackValueWithErrorCode(nlohmann::json& jsonValue,
128ea2e6eecSWilly Tu                                          std::string_view key, Type& value)
129771cfa0fSJason M. Bills {
130471a5eb8SAppaRao Puli     UnpackErrorCode ret = UnpackErrorCode::success;
13141352c24SSantosh Puranik 
132a6acbb31SJames Feist     if constexpr (std::is_floating_point_v<Type>)
133771cfa0fSJason M. Bills     {
134a6acbb31SJames Feist         double helper = 0;
135a6acbb31SJames Feist         double* jsonPtr = jsonValue.get_ptr<double*>();
136771cfa0fSJason M. Bills 
137771cfa0fSJason M. Bills         if (jsonPtr == nullptr)
138771cfa0fSJason M. Bills         {
139a6acbb31SJames Feist             int64_t* intPtr = jsonValue.get_ptr<int64_t*>();
140a6acbb31SJames Feist             if (intPtr != nullptr)
141771cfa0fSJason M. Bills             {
142a6acbb31SJames Feist                 helper = static_cast<double>(*intPtr);
143a6acbb31SJames Feist                 jsonPtr = &helper;
144771cfa0fSJason M. Bills             }
145a6acbb31SJames Feist         }
1465eb2bef2SAppaRao Puli         if (jsonPtr == nullptr)
1475eb2bef2SAppaRao Puli         {
148471a5eb8SAppaRao Puli             return UnpackErrorCode::invalidType;
1495eb2bef2SAppaRao Puli         }
150cb13a392SEd Tanous         if (!checkRange<Type>(*jsonPtr, key))
151771cfa0fSJason M. Bills         {
152471a5eb8SAppaRao Puli             return UnpackErrorCode::outOfRange;
153771cfa0fSJason M. Bills         }
154771cfa0fSJason M. Bills         value = static_cast<Type>(*jsonPtr);
155771cfa0fSJason M. Bills     }
156a6acbb31SJames Feist 
157a6acbb31SJames Feist     else if constexpr (std::is_signed_v<Type>)
158a6acbb31SJames Feist     {
159a6acbb31SJames Feist         int64_t* jsonPtr = jsonValue.get_ptr<int64_t*>();
160271584abSEd Tanous         if (jsonPtr == nullptr)
161271584abSEd Tanous         {
162471a5eb8SAppaRao Puli             return UnpackErrorCode::invalidType;
163271584abSEd Tanous         }
164cb13a392SEd Tanous         if (!checkRange<Type>(*jsonPtr, key))
165a6acbb31SJames Feist         {
166471a5eb8SAppaRao Puli             return UnpackErrorCode::outOfRange;
167a6acbb31SJames Feist         }
168a6acbb31SJames Feist         value = static_cast<Type>(*jsonPtr);
169a6acbb31SJames Feist     }
170a6acbb31SJames Feist 
1718102ddbaSAppaRao Puli     else if constexpr ((std::is_unsigned_v<Type>)&&(
1728102ddbaSAppaRao Puli                            !std::is_same_v<bool, Type>))
173a6acbb31SJames Feist     {
174a6acbb31SJames Feist         uint64_t* jsonPtr = jsonValue.get_ptr<uint64_t*>();
175271584abSEd Tanous         if (jsonPtr == nullptr)
176271584abSEd Tanous         {
177471a5eb8SAppaRao Puli             return UnpackErrorCode::invalidType;
178271584abSEd Tanous         }
179cb13a392SEd Tanous         if (!checkRange<Type>(*jsonPtr, key))
180a6acbb31SJames Feist         {
181471a5eb8SAppaRao Puli             return UnpackErrorCode::outOfRange;
182a6acbb31SJames Feist         }
183a6acbb31SJames Feist         value = static_cast<Type>(*jsonPtr);
184a6acbb31SJames Feist     }
185a6acbb31SJames Feist 
1860627a2c7SEd Tanous     else if constexpr (std::is_same_v<nlohmann::json, Type>)
1870627a2c7SEd Tanous     {
1880627a2c7SEd Tanous         value = std::move(jsonValue);
1890627a2c7SEd Tanous     }
190471a5eb8SAppaRao Puli     else
191471a5eb8SAppaRao Puli     {
192471a5eb8SAppaRao Puli         using JsonType = std::add_const_t<std::add_pointer_t<Type>>;
193471a5eb8SAppaRao Puli         JsonType jsonPtr = jsonValue.get_ptr<JsonType>();
194471a5eb8SAppaRao Puli         if (jsonPtr == nullptr)
195471a5eb8SAppaRao Puli         {
196471a5eb8SAppaRao Puli             BMCWEB_LOG_DEBUG
197471a5eb8SAppaRao Puli                 << "Value for key " << key
198471a5eb8SAppaRao Puli                 << " was incorrect type: " << jsonValue.type_name();
199471a5eb8SAppaRao Puli             return UnpackErrorCode::invalidType;
200471a5eb8SAppaRao Puli         }
201471a5eb8SAppaRao Puli         value = std::move(*jsonPtr);
202471a5eb8SAppaRao Puli     }
203471a5eb8SAppaRao Puli     return ret;
204471a5eb8SAppaRao Puli }
205471a5eb8SAppaRao Puli 
206471a5eb8SAppaRao Puli template <typename Type>
207ea2e6eecSWilly Tu bool unpackValue(nlohmann::json& jsonValue, std::string_view key,
208471a5eb8SAppaRao Puli                  crow::Response& res, Type& value)
209471a5eb8SAppaRao Puli {
210471a5eb8SAppaRao Puli     bool ret = true;
211471a5eb8SAppaRao Puli 
2122c70f800SEd Tanous     if constexpr (IsOptional<Type>::value)
213471a5eb8SAppaRao Puli     {
214471a5eb8SAppaRao Puli         value.emplace();
215471a5eb8SAppaRao Puli         ret = unpackValue<typename Type::value_type>(jsonValue, key, res,
216471a5eb8SAppaRao Puli                                                      *value) &&
217471a5eb8SAppaRao Puli               ret;
218471a5eb8SAppaRao Puli     }
2192c70f800SEd Tanous     else if constexpr (IsStdArray<Type>::value)
220318226c2SJames Feist     {
221318226c2SJames Feist         if (!jsonValue.is_array())
222318226c2SJames Feist         {
22371f52d96SEd Tanous             messages::propertyValueTypeError(
22471f52d96SEd Tanous                 res,
22571f52d96SEd Tanous                 res.jsonValue.dump(2, ' ', true,
22671f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace),
22771f52d96SEd Tanous                 key);
22841352c24SSantosh Puranik             return false;
229318226c2SJames Feist         }
230318226c2SJames Feist         if (jsonValue.size() != value.size())
231318226c2SJames Feist         {
23271f52d96SEd Tanous             messages::propertyValueTypeError(
23371f52d96SEd Tanous                 res,
23471f52d96SEd Tanous                 res.jsonValue.dump(2, ' ', true,
23571f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace),
23671f52d96SEd Tanous                 key);
23741352c24SSantosh Puranik             return false;
238318226c2SJames Feist         }
239318226c2SJames Feist         size_t index = 0;
240318226c2SJames Feist         for (const auto& val : jsonValue.items())
241318226c2SJames Feist         {
24241352c24SSantosh Puranik             ret = unpackValue<typename Type::value_type>(val.value(), key, res,
24341352c24SSantosh Puranik                                                          value[index++]) &&
24441352c24SSantosh Puranik                   ret;
245318226c2SJames Feist         }
246318226c2SJames Feist     }
2472c70f800SEd Tanous     else if constexpr (IsVector<Type>::value)
248b1556427SEd Tanous     {
249b1556427SEd Tanous         if (!jsonValue.is_array())
250b1556427SEd Tanous         {
25171f52d96SEd Tanous             messages::propertyValueTypeError(
25271f52d96SEd Tanous                 res,
25371f52d96SEd Tanous                 res.jsonValue.dump(2, ' ', true,
25471f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace),
25571f52d96SEd Tanous                 key);
25641352c24SSantosh Puranik             return false;
257b1556427SEd Tanous         }
258b1556427SEd Tanous 
259b1556427SEd Tanous         for (const auto& val : jsonValue.items())
260b1556427SEd Tanous         {
261b1556427SEd Tanous             value.emplace_back();
26241352c24SSantosh Puranik             ret = unpackValue<typename Type::value_type>(val.value(), key, res,
26341352c24SSantosh Puranik                                                          value.back()) &&
26441352c24SSantosh Puranik                   ret;
265b1556427SEd Tanous         }
266b1556427SEd Tanous     }
267771cfa0fSJason M. Bills     else
268771cfa0fSJason M. Bills     {
269471a5eb8SAppaRao Puli         UnpackErrorCode ec = unpackValueWithErrorCode(jsonValue, key, value);
270471a5eb8SAppaRao Puli         if (ec != UnpackErrorCode::success)
271771cfa0fSJason M. Bills         {
272471a5eb8SAppaRao Puli             if (ec == UnpackErrorCode::invalidType)
273471a5eb8SAppaRao Puli             {
27471f52d96SEd Tanous                 messages::propertyValueTypeError(
27571f52d96SEd Tanous                     res,
27671f52d96SEd Tanous                     jsonValue.dump(2, ' ', true,
27771f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace),
27871f52d96SEd Tanous                     key);
279471a5eb8SAppaRao Puli             }
280471a5eb8SAppaRao Puli             else if (ec == UnpackErrorCode::outOfRange)
281471a5eb8SAppaRao Puli             {
28271f52d96SEd Tanous                 messages::propertyValueNotInList(
28371f52d96SEd Tanous                     res,
28471f52d96SEd Tanous                     jsonValue.dump(2, ' ', true,
28571f52d96SEd Tanous                                    nlohmann::json::error_handler_t::replace),
28671f52d96SEd Tanous                     key);
287471a5eb8SAppaRao Puli             }
28841352c24SSantosh Puranik             return false;
289771cfa0fSJason M. Bills         }
290771cfa0fSJason M. Bills     }
291471a5eb8SAppaRao Puli 
292471a5eb8SAppaRao Puli     return ret;
293471a5eb8SAppaRao Puli }
294471a5eb8SAppaRao Puli 
295471a5eb8SAppaRao Puli template <typename Type>
296ea2e6eecSWilly Tu bool unpackValue(nlohmann::json& jsonValue, std::string_view key, Type& value)
297471a5eb8SAppaRao Puli {
298471a5eb8SAppaRao Puli     bool ret = true;
2992c70f800SEd Tanous     if constexpr (IsOptional<Type>::value)
300471a5eb8SAppaRao Puli     {
301471a5eb8SAppaRao Puli         value.emplace();
302471a5eb8SAppaRao Puli         ret = unpackValue<typename Type::value_type>(jsonValue, key, *value) &&
303471a5eb8SAppaRao Puli               ret;
304471a5eb8SAppaRao Puli     }
3052c70f800SEd Tanous     else if constexpr (IsStdArray<Type>::value)
306471a5eb8SAppaRao Puli     {
307471a5eb8SAppaRao Puli         if (!jsonValue.is_array())
308471a5eb8SAppaRao Puli         {
309471a5eb8SAppaRao Puli             return false;
310471a5eb8SAppaRao Puli         }
311471a5eb8SAppaRao Puli         if (jsonValue.size() != value.size())
312471a5eb8SAppaRao Puli         {
313471a5eb8SAppaRao Puli             return false;
314471a5eb8SAppaRao Puli         }
315471a5eb8SAppaRao Puli         size_t index = 0;
316471a5eb8SAppaRao Puli         for (const auto& val : jsonValue.items())
317471a5eb8SAppaRao Puli         {
318471a5eb8SAppaRao Puli             ret = unpackValue<typename Type::value_type>(val.value(), key,
319471a5eb8SAppaRao Puli                                                          value[index++]) &&
320471a5eb8SAppaRao Puli                   ret;
321471a5eb8SAppaRao Puli         }
322471a5eb8SAppaRao Puli     }
3232c70f800SEd Tanous     else if constexpr (IsVector<Type>::value)
324471a5eb8SAppaRao Puli     {
325471a5eb8SAppaRao Puli         if (!jsonValue.is_array())
326471a5eb8SAppaRao Puli         {
327471a5eb8SAppaRao Puli             return false;
328471a5eb8SAppaRao Puli         }
329471a5eb8SAppaRao Puli 
330471a5eb8SAppaRao Puli         for (const auto& val : jsonValue.items())
331471a5eb8SAppaRao Puli         {
332471a5eb8SAppaRao Puli             value.emplace_back();
333471a5eb8SAppaRao Puli             ret = unpackValue<typename Type::value_type>(val.value(), key,
334471a5eb8SAppaRao Puli                                                          value.back()) &&
335471a5eb8SAppaRao Puli                   ret;
336471a5eb8SAppaRao Puli         }
337471a5eb8SAppaRao Puli     }
338471a5eb8SAppaRao Puli     else
339471a5eb8SAppaRao Puli     {
340471a5eb8SAppaRao Puli         UnpackErrorCode ec = unpackValueWithErrorCode(jsonValue, key, value);
341471a5eb8SAppaRao Puli         if (ec != UnpackErrorCode::success)
342471a5eb8SAppaRao Puli         {
343471a5eb8SAppaRao Puli             return false;
344471a5eb8SAppaRao Puli         }
345471a5eb8SAppaRao Puli     }
346471a5eb8SAppaRao Puli 
34741352c24SSantosh Puranik     return ret;
348771cfa0fSJason M. Bills }
3499712f8acSEd Tanous } // namespace details
3509712f8acSEd Tanous 
351ea2e6eecSWilly Tu // clang-format off
352ea2e6eecSWilly Tu using UnpackVariant = std::variant<
353ea2e6eecSWilly Tu     uint8_t*,
354ea2e6eecSWilly Tu     uint16_t*,
355ea2e6eecSWilly Tu     int16_t*,
356ea2e6eecSWilly Tu     uint32_t*,
357ea2e6eecSWilly Tu     int32_t*,
358ea2e6eecSWilly Tu     uint64_t*,
359ea2e6eecSWilly Tu     int64_t*,
360ea2e6eecSWilly Tu     bool*,
361ea2e6eecSWilly Tu     double*,
362ea2e6eecSWilly Tu     std::string*,
363ea2e6eecSWilly Tu     nlohmann::json*,
364ea2e6eecSWilly Tu     std::vector<uint8_t>*,
365ea2e6eecSWilly Tu     std::vector<uint16_t>*,
366ea2e6eecSWilly Tu     std::vector<int16_t>*,
367ea2e6eecSWilly Tu     std::vector<uint32_t>*,
368ea2e6eecSWilly Tu     std::vector<int32_t>*,
369ea2e6eecSWilly Tu     std::vector<uint64_t>*,
370ea2e6eecSWilly Tu     std::vector<int64_t>*,
371ea2e6eecSWilly Tu     //std::vector<bool>*,
372ea2e6eecSWilly Tu     std::vector<double>*,
373ea2e6eecSWilly Tu     std::vector<std::string>*,
374ea2e6eecSWilly Tu     std::vector<nlohmann::json>*,
375ea2e6eecSWilly Tu     std::optional<uint8_t>*,
376ea2e6eecSWilly Tu     std::optional<uint16_t>*,
377ea2e6eecSWilly Tu     std::optional<int16_t>*,
378ea2e6eecSWilly Tu     std::optional<uint32_t>*,
379ea2e6eecSWilly Tu     std::optional<int32_t>*,
380ea2e6eecSWilly Tu     std::optional<uint64_t>*,
381ea2e6eecSWilly Tu     std::optional<int64_t>*,
382ea2e6eecSWilly Tu     std::optional<bool>*,
383ea2e6eecSWilly Tu     std::optional<double>*,
384ea2e6eecSWilly Tu     std::optional<std::string>*,
385ea2e6eecSWilly Tu     std::optional<nlohmann::json>*,
386ea2e6eecSWilly Tu     std::optional<std::vector<uint8_t>>*,
387ea2e6eecSWilly Tu     std::optional<std::vector<uint16_t>>*,
388ea2e6eecSWilly Tu     std::optional<std::vector<int16_t>>*,
389ea2e6eecSWilly Tu     std::optional<std::vector<uint32_t>>*,
390ea2e6eecSWilly Tu     std::optional<std::vector<int32_t>>*,
391ea2e6eecSWilly Tu     std::optional<std::vector<uint64_t>>*,
392ea2e6eecSWilly Tu     std::optional<std::vector<int64_t>>*,
393ea2e6eecSWilly Tu     //std::optional<std::vector<bool>>*,
394ea2e6eecSWilly Tu     std::optional<std::vector<double>>*,
395ea2e6eecSWilly Tu     std::optional<std::vector<std::string>>*,
396ea2e6eecSWilly Tu     std::optional<std::vector<nlohmann::json>>*
397ea2e6eecSWilly Tu >;
398ea2e6eecSWilly Tu // clang-format on
399ea2e6eecSWilly Tu 
400ea2e6eecSWilly Tu struct PerUnpack
401ea2e6eecSWilly Tu {
402ea2e6eecSWilly Tu     std::string_view key;
403ea2e6eecSWilly Tu     UnpackVariant value;
404ea2e6eecSWilly Tu     bool complete = false;
405ea2e6eecSWilly Tu };
406ea2e6eecSWilly Tu 
407ea2e6eecSWilly Tu inline bool readJsonHelper(nlohmann::json& jsonRequest, crow::Response& res,
408ea2e6eecSWilly Tu                            std::span<PerUnpack> toUnpack)
4099712f8acSEd Tanous {
41041352c24SSantosh Puranik     bool result = true;
4119712f8acSEd Tanous     if (!jsonRequest.is_object())
4129712f8acSEd Tanous     {
4139712f8acSEd Tanous         BMCWEB_LOG_DEBUG << "Json value is not an object";
414f12894f8SJason M. Bills         messages::unrecognizedRequestBody(res);
4159712f8acSEd Tanous         return false;
4169712f8acSEd Tanous     }
417ea2e6eecSWilly Tu     for (auto& item : jsonRequest.items())
4189712f8acSEd Tanous     {
419ea2e6eecSWilly Tu         size_t unpackIndex = 0;
420ea2e6eecSWilly Tu         for (; unpackIndex < toUnpack.size(); unpackIndex++)
421ea2e6eecSWilly Tu         {
422ea2e6eecSWilly Tu             PerUnpack& unpackSpec = toUnpack[unpackIndex];
423ea2e6eecSWilly Tu             std::string_view key = unpackSpec.key;
424ea2e6eecSWilly Tu             size_t keysplitIndex = key.find('/');
425ea2e6eecSWilly Tu             std::string_view leftover;
426ea2e6eecSWilly Tu             if (keysplitIndex != std::string_view::npos)
427ea2e6eecSWilly Tu             {
428ea2e6eecSWilly Tu                 leftover = key.substr(keysplitIndex + 1);
429ea2e6eecSWilly Tu                 key = key.substr(0, keysplitIndex);
430ea2e6eecSWilly Tu             }
431ea2e6eecSWilly Tu 
432ea2e6eecSWilly Tu             if (key != item.key() || unpackSpec.complete)
433ea2e6eecSWilly Tu             {
434ea2e6eecSWilly Tu                 continue;
435ea2e6eecSWilly Tu             }
436ea2e6eecSWilly Tu 
437ea2e6eecSWilly Tu             // Sublevel key
438ea2e6eecSWilly Tu             if (!leftover.empty())
439ea2e6eecSWilly Tu             {
440ea2e6eecSWilly Tu                 // Include the slash in the key so we can compare later
441ea2e6eecSWilly Tu                 key = unpackSpec.key.substr(0, keysplitIndex + 1);
442ea2e6eecSWilly Tu                 nlohmann::json j;
443ea2e6eecSWilly Tu                 result = details::unpackValue<nlohmann::json>(item.value(), key,
444ea2e6eecSWilly Tu                                                               res, j) &&
44541352c24SSantosh Puranik                          result;
44655f79e6fSEd Tanous                 if (!result)
447ea2e6eecSWilly Tu                 {
448ea2e6eecSWilly Tu                     return result;
4499712f8acSEd Tanous                 }
4509712f8acSEd Tanous 
451ea2e6eecSWilly Tu                 std::vector<PerUnpack> nextLevel;
452ea2e6eecSWilly Tu                 for (PerUnpack& p : toUnpack)
453ea2e6eecSWilly Tu                 {
454ea2e6eecSWilly Tu                     if (!p.key.starts_with(key))
455ea2e6eecSWilly Tu                     {
456ea2e6eecSWilly Tu                         continue;
457ea2e6eecSWilly Tu                     }
458ea2e6eecSWilly Tu                     std::string_view thisLeftover = p.key.substr(key.size());
459ea2e6eecSWilly Tu                     nextLevel.push_back({thisLeftover, p.value, false});
460ea2e6eecSWilly Tu                     p.complete = true;
4619712f8acSEd Tanous                 }
46277dd8813SKowalski, Kamil 
463ea2e6eecSWilly Tu                 result = readJsonHelper(j, res, nextLevel) && result;
464ea2e6eecSWilly Tu                 break;
465ea2e6eecSWilly Tu             }
466ea2e6eecSWilly Tu 
467002d39b4SEd Tanous             result = std::visit(
468ea2e6eecSWilly Tu                          [&item, &unpackSpec, &res](auto&& val) {
469ea2e6eecSWilly Tu                 using ContainedT =
470ea2e6eecSWilly Tu                     std::remove_pointer_t<std::decay_t<decltype(val)>>;
471ea2e6eecSWilly Tu                 return details::unpackValue<ContainedT>(
472ea2e6eecSWilly Tu                     item.value(), unpackSpec.key, res, *val);
473ea2e6eecSWilly Tu                          },
474ea2e6eecSWilly Tu                          unpackSpec.value) &&
475ea2e6eecSWilly Tu                 result;
476ea2e6eecSWilly Tu 
477ea2e6eecSWilly Tu             unpackSpec.complete = true;
478ea2e6eecSWilly Tu             break;
479ea2e6eecSWilly Tu         }
480ea2e6eecSWilly Tu 
481ea2e6eecSWilly Tu         if (unpackIndex == toUnpack.size())
482ea2e6eecSWilly Tu         {
483ea2e6eecSWilly Tu             messages::propertyUnknown(res, item.key());
484ea2e6eecSWilly Tu             result = false;
485ea2e6eecSWilly Tu         }
486ea2e6eecSWilly Tu     }
487ea2e6eecSWilly Tu 
488ea2e6eecSWilly Tu     for (PerUnpack& perUnpack : toUnpack)
489ea2e6eecSWilly Tu     {
49055f79e6fSEd Tanous         if (!perUnpack.complete)
491ea2e6eecSWilly Tu         {
492ea2e6eecSWilly Tu             bool isOptional = std::visit(
493ea2e6eecSWilly Tu                 [](auto&& val) {
494ea2e6eecSWilly Tu                 using ContainedType =
495ea2e6eecSWilly Tu                     std::remove_pointer_t<std::decay_t<decltype(val)>>;
496ea2e6eecSWilly Tu                 return details::IsOptional<ContainedType>::value;
497ea2e6eecSWilly Tu                 },
498ea2e6eecSWilly Tu                 perUnpack.value);
499ea2e6eecSWilly Tu             if (isOptional)
500ea2e6eecSWilly Tu             {
501ea2e6eecSWilly Tu                 continue;
502ea2e6eecSWilly Tu             }
503ea2e6eecSWilly Tu             messages::propertyMissing(res, perUnpack.key);
504ea2e6eecSWilly Tu             result = false;
505ea2e6eecSWilly Tu         }
506ea2e6eecSWilly Tu     }
507ea2e6eecSWilly Tu     return result;
508ea2e6eecSWilly Tu }
509ea2e6eecSWilly Tu 
510ea2e6eecSWilly Tu inline void packVariant(std::span<PerUnpack> /*toPack*/)
511ea2e6eecSWilly Tu {}
512ea2e6eecSWilly Tu 
513ea2e6eecSWilly Tu template <typename FirstType, typename... UnpackTypes>
514ea2e6eecSWilly Tu void packVariant(std::span<PerUnpack> toPack, std::string_view key,
515ea2e6eecSWilly Tu                  FirstType& first, UnpackTypes&&... in)
516ea2e6eecSWilly Tu {
517ea2e6eecSWilly Tu     if (toPack.empty())
518ea2e6eecSWilly Tu     {
519ea2e6eecSWilly Tu         return;
520ea2e6eecSWilly Tu     }
521ea2e6eecSWilly Tu     toPack[0].key = key;
522ea2e6eecSWilly Tu     toPack[0].value = &first;
523ea2e6eecSWilly Tu     // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
524ea2e6eecSWilly Tu     packVariant(toPack.subspan(1), std::forward<UnpackTypes&&>(in)...);
525ea2e6eecSWilly Tu }
526ea2e6eecSWilly Tu 
527ea2e6eecSWilly Tu template <typename FirstType, typename... UnpackTypes>
528ea2e6eecSWilly Tu bool readJson(nlohmann::json& jsonRequest, crow::Response& res,
529ea2e6eecSWilly Tu               std::string_view key, FirstType&& first, UnpackTypes&&... in)
530ea2e6eecSWilly Tu {
531ea2e6eecSWilly Tu     const std::size_t n = sizeof...(UnpackTypes) + 2;
532ea2e6eecSWilly Tu     std::array<PerUnpack, n / 2> toUnpack2;
533ea2e6eecSWilly Tu     packVariant(toUnpack2, key, first, std::forward<UnpackTypes&&>(in)...);
534ea2e6eecSWilly Tu     return readJsonHelper(jsonRequest, res, toUnpack2);
535ea2e6eecSWilly Tu }
536ea2e6eecSWilly Tu 
537ea2e6eecSWilly Tu inline std::optional<nlohmann::json>
538ea2e6eecSWilly Tu     readJsonPatchHelper(const crow::Request& req, crow::Response& res)
5390627a2c7SEd Tanous {
5400627a2c7SEd Tanous     nlohmann::json jsonRequest;
5410627a2c7SEd Tanous     if (!json_util::processJsonFromRequest(res, req, jsonRequest))
5420627a2c7SEd Tanous     {
5430627a2c7SEd Tanous         BMCWEB_LOG_DEBUG << "Json value not readable";
544ea2e6eecSWilly Tu         return std::nullopt;
5450627a2c7SEd Tanous     }
546357bb8f8SEd Tanous     nlohmann::json::object_t* object =
547357bb8f8SEd Tanous         jsonRequest.get_ptr<nlohmann::json::object_t*>();
548357bb8f8SEd Tanous     if (object == nullptr || object->empty())
54915ed6780SWilly Tu     {
55015ed6780SWilly Tu         BMCWEB_LOG_DEBUG << "Json value is empty";
55115ed6780SWilly Tu         messages::emptyJSON(res);
552ea2e6eecSWilly Tu         return std::nullopt;
553ea2e6eecSWilly Tu     }
554357bb8f8SEd Tanous     std::erase_if(*object,
555357bb8f8SEd Tanous                   [](const std::pair<std::string, nlohmann::json>& item) {
556357bb8f8SEd Tanous         return item.first.starts_with("@odata.");
557357bb8f8SEd Tanous     });
558357bb8f8SEd Tanous     if (object->empty())
559357bb8f8SEd Tanous     {
560357bb8f8SEd Tanous         //  If the update request only contains OData annotations, the service
561357bb8f8SEd Tanous         //  should return the HTTP 400 Bad Request status code with the
562357bb8f8SEd Tanous         //  NoOperation message from the Base Message Registry, ...
563357bb8f8SEd Tanous         messages::noOperation(res);
564357bb8f8SEd Tanous         return std::nullopt;
565357bb8f8SEd Tanous     }
566357bb8f8SEd Tanous 
567ea2e6eecSWilly Tu     return {std::move(jsonRequest)};
568ea2e6eecSWilly Tu }
569ea2e6eecSWilly Tu 
570ea2e6eecSWilly Tu template <typename... UnpackTypes>
571ea2e6eecSWilly Tu bool readJsonPatch(const crow::Request& req, crow::Response& res,
572ea2e6eecSWilly Tu                    std::string_view key, UnpackTypes&&... in)
573ea2e6eecSWilly Tu {
574ea2e6eecSWilly Tu     std::optional<nlohmann::json> jsonRequest = readJsonPatchHelper(req, res);
575ea2e6eecSWilly Tu     if (jsonRequest == std::nullopt)
576ea2e6eecSWilly Tu     {
57715ed6780SWilly Tu         return false;
57815ed6780SWilly Tu     }
57915ed6780SWilly Tu 
580ea2e6eecSWilly Tu     return readJson(*jsonRequest, res, key, std::forward<UnpackTypes&&>(in)...);
58115ed6780SWilly Tu }
58215ed6780SWilly Tu 
58315ed6780SWilly Tu template <typename... UnpackTypes>
58415ed6780SWilly Tu bool readJsonAction(const crow::Request& req, crow::Response& res,
585ea2e6eecSWilly Tu                     const char* key, UnpackTypes&&... in)
58615ed6780SWilly Tu {
58715ed6780SWilly Tu     nlohmann::json jsonRequest;
58815ed6780SWilly Tu     if (!json_util::processJsonFromRequest(res, req, jsonRequest))
58915ed6780SWilly Tu     {
59015ed6780SWilly Tu         BMCWEB_LOG_DEBUG << "Json value not readable";
59115ed6780SWilly Tu         return false;
59215ed6780SWilly Tu     }
593ea2e6eecSWilly Tu     return readJson(jsonRequest, res, key, std::forward<UnpackTypes&&>(in)...);
5940627a2c7SEd Tanous }
5950627a2c7SEd Tanous 
59677dd8813SKowalski, Kamil } // namespace json_util
59777dd8813SKowalski, Kamil } // namespace redfish
598