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 18d5c80ad9SNan Zhou #include "error_messages.hpp" 19d5c80ad9SNan Zhou #include "http_request.hpp" 20d5c80ad9SNan Zhou #include "http_response.hpp" 21d5c80ad9SNan Zhou #include "logging.hpp" 22*faf100f9SEd Tanous 23*faf100f9SEd Tanous #include <nlohmann/json.hpp> 240627a2c7SEd Tanous 25d5c80ad9SNan Zhou #include <array> 26d5c80ad9SNan Zhou #include <cmath> 27d5c80ad9SNan Zhou #include <cstddef> 28d5c80ad9SNan Zhou #include <cstdint> 29d5c80ad9SNan Zhou #include <limits> 30d5c80ad9SNan Zhou #include <map> 31d5c80ad9SNan Zhou #include <optional> 32ea2e6eecSWilly Tu #include <span> 33d5c80ad9SNan Zhou #include <string> 34d5c80ad9SNan Zhou #include <string_view> 35d5c80ad9SNan Zhou #include <type_traits> 36d5c80ad9SNan Zhou #include <utility> 37d5c80ad9SNan Zhou #include <variant> 38d5c80ad9SNan Zhou #include <vector> 39d5c80ad9SNan Zhou 40d5c80ad9SNan Zhou // IWYU pragma: no_include <stdint.h> 411e75e1ddSNan Zhou // IWYU pragma: no_forward_declare crow::Request 421214b7e7SGunnar Mills 431abe55efSEd Tanous namespace redfish 441abe55efSEd Tanous { 451abe55efSEd Tanous 461abe55efSEd Tanous namespace json_util 471abe55efSEd Tanous { 4877dd8813SKowalski, Kamil 4977dd8813SKowalski, Kamil /** 5077dd8813SKowalski, Kamil * @brief Processes request to extract JSON from its body. If it fails, adds 5177dd8813SKowalski, Kamil * MalformedJSON message to response and ends it. 5277dd8813SKowalski, Kamil * 5377dd8813SKowalski, Kamil * @param[io] res Response object 5477dd8813SKowalski, Kamil * @param[in] req Request object 5577dd8813SKowalski, Kamil * @param[out] reqJson JSON object extracted from request's body 5677dd8813SKowalski, Kamil * 5777dd8813SKowalski, Kamil * @return true if JSON is valid, false when JSON is invalid and response has 5877dd8813SKowalski, Kamil * been filled with message and ended. 5977dd8813SKowalski, Kamil */ 6055c7b7a2SEd Tanous bool processJsonFromRequest(crow::Response& res, const crow::Request& req, 6177dd8813SKowalski, Kamil nlohmann::json& reqJson); 629712f8acSEd Tanous namespace details 639712f8acSEd Tanous { 64771cfa0fSJason M. Bills 651214b7e7SGunnar Mills template <typename Type> 662c70f800SEd Tanous struct IsOptional : std::false_type 671214b7e7SGunnar Mills {}; 689712f8acSEd Tanous 69771cfa0fSJason M. Bills template <typename Type> 702c70f800SEd Tanous struct IsOptional<std::optional<Type>> : std::true_type 711214b7e7SGunnar Mills {}; 729712f8acSEd Tanous 73771cfa0fSJason M. Bills template <typename Type> 742c70f800SEd Tanous struct IsVector : std::false_type 751214b7e7SGunnar Mills {}; 76b1556427SEd Tanous 771214b7e7SGunnar Mills template <typename Type> 782c70f800SEd Tanous struct IsVector<std::vector<Type>> : std::true_type 791214b7e7SGunnar Mills {}; 80b1556427SEd Tanous 811214b7e7SGunnar Mills template <typename Type> 822c70f800SEd Tanous struct IsStdArray : std::false_type 831214b7e7SGunnar Mills {}; 84318226c2SJames Feist 85318226c2SJames Feist template <typename Type, std::size_t size> 862c70f800SEd Tanous struct IsStdArray<std::array<Type, size>> : std::true_type 871214b7e7SGunnar Mills {}; 88318226c2SJames Feist 89471a5eb8SAppaRao Puli enum class UnpackErrorCode 90471a5eb8SAppaRao Puli { 91471a5eb8SAppaRao Puli success, 92471a5eb8SAppaRao Puli invalidType, 93471a5eb8SAppaRao Puli outOfRange 94471a5eb8SAppaRao Puli }; 95471a5eb8SAppaRao Puli 96a6acbb31SJames Feist template <typename ToType, typename FromType> 97ea2e6eecSWilly Tu bool checkRange(const FromType& from, std::string_view key) 98a6acbb31SJames Feist { 99ee344e0fSEd Tanous if (from > std::numeric_limits<ToType>::max()) 100a6acbb31SJames Feist { 101a6acbb31SJames Feist BMCWEB_LOG_DEBUG << "Value for key " << key 102a6acbb31SJames Feist << " was greater than max: " << __PRETTY_FUNCTION__; 103a6acbb31SJames Feist return false; 104a6acbb31SJames Feist } 105ee344e0fSEd Tanous if (from < std::numeric_limits<ToType>::lowest()) 106a6acbb31SJames Feist { 107a6acbb31SJames Feist BMCWEB_LOG_DEBUG << "Value for key " << key 108a6acbb31SJames Feist << " was less than min: " << __PRETTY_FUNCTION__; 109a6acbb31SJames Feist return false; 110a6acbb31SJames Feist } 111a6acbb31SJames Feist if constexpr (std::is_floating_point_v<ToType>) 112a6acbb31SJames Feist { 113ee344e0fSEd Tanous if (std::isnan(from)) 114a6acbb31SJames Feist { 115a6acbb31SJames Feist BMCWEB_LOG_DEBUG << "Value for key " << key << " was NAN"; 116a6acbb31SJames Feist return false; 117a6acbb31SJames Feist } 118a6acbb31SJames Feist } 119a6acbb31SJames Feist 120a6acbb31SJames Feist return true; 121a6acbb31SJames Feist } 122a6acbb31SJames Feist 123771cfa0fSJason M. Bills template <typename Type> 124471a5eb8SAppaRao Puli UnpackErrorCode unpackValueWithErrorCode(nlohmann::json& jsonValue, 125ea2e6eecSWilly Tu std::string_view key, Type& value) 126771cfa0fSJason M. Bills { 127471a5eb8SAppaRao Puli UnpackErrorCode ret = UnpackErrorCode::success; 12841352c24SSantosh Puranik 129a6acbb31SJames Feist if constexpr (std::is_floating_point_v<Type>) 130771cfa0fSJason M. Bills { 131a6acbb31SJames Feist double helper = 0; 132a6acbb31SJames Feist double* jsonPtr = jsonValue.get_ptr<double*>(); 133771cfa0fSJason M. Bills 134771cfa0fSJason M. Bills if (jsonPtr == nullptr) 135771cfa0fSJason M. Bills { 136a6acbb31SJames Feist int64_t* intPtr = jsonValue.get_ptr<int64_t*>(); 137a6acbb31SJames Feist if (intPtr != nullptr) 138771cfa0fSJason M. Bills { 139a6acbb31SJames Feist helper = static_cast<double>(*intPtr); 140a6acbb31SJames Feist jsonPtr = &helper; 141771cfa0fSJason M. Bills } 142a6acbb31SJames Feist } 1435eb2bef2SAppaRao Puli if (jsonPtr == nullptr) 1445eb2bef2SAppaRao Puli { 145471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 1465eb2bef2SAppaRao Puli } 147cb13a392SEd Tanous if (!checkRange<Type>(*jsonPtr, key)) 148771cfa0fSJason M. Bills { 149471a5eb8SAppaRao Puli return UnpackErrorCode::outOfRange; 150771cfa0fSJason M. Bills } 151771cfa0fSJason M. Bills value = static_cast<Type>(*jsonPtr); 152771cfa0fSJason M. Bills } 153a6acbb31SJames Feist 154a6acbb31SJames Feist else if constexpr (std::is_signed_v<Type>) 155a6acbb31SJames Feist { 156a6acbb31SJames Feist int64_t* jsonPtr = jsonValue.get_ptr<int64_t*>(); 157271584abSEd Tanous if (jsonPtr == nullptr) 158271584abSEd Tanous { 159471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 160271584abSEd Tanous } 161cb13a392SEd Tanous if (!checkRange<Type>(*jsonPtr, key)) 162a6acbb31SJames Feist { 163471a5eb8SAppaRao Puli return UnpackErrorCode::outOfRange; 164a6acbb31SJames Feist } 165a6acbb31SJames Feist value = static_cast<Type>(*jsonPtr); 166a6acbb31SJames Feist } 167a6acbb31SJames Feist 1688102ddbaSAppaRao Puli else if constexpr ((std::is_unsigned_v<Type>)&&( 1698102ddbaSAppaRao Puli !std::is_same_v<bool, Type>)) 170a6acbb31SJames Feist { 171a6acbb31SJames Feist uint64_t* jsonPtr = jsonValue.get_ptr<uint64_t*>(); 172271584abSEd Tanous if (jsonPtr == nullptr) 173271584abSEd Tanous { 174471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 175271584abSEd Tanous } 176cb13a392SEd Tanous if (!checkRange<Type>(*jsonPtr, key)) 177a6acbb31SJames Feist { 178471a5eb8SAppaRao Puli return UnpackErrorCode::outOfRange; 179a6acbb31SJames Feist } 180a6acbb31SJames Feist value = static_cast<Type>(*jsonPtr); 181a6acbb31SJames Feist } 182a6acbb31SJames Feist 1830627a2c7SEd Tanous else if constexpr (std::is_same_v<nlohmann::json, Type>) 1840627a2c7SEd Tanous { 1850627a2c7SEd Tanous value = std::move(jsonValue); 1860627a2c7SEd Tanous } 187471a5eb8SAppaRao Puli else 188471a5eb8SAppaRao Puli { 189471a5eb8SAppaRao Puli using JsonType = std::add_const_t<std::add_pointer_t<Type>>; 190471a5eb8SAppaRao Puli JsonType jsonPtr = jsonValue.get_ptr<JsonType>(); 191471a5eb8SAppaRao Puli if (jsonPtr == nullptr) 192471a5eb8SAppaRao Puli { 193471a5eb8SAppaRao Puli BMCWEB_LOG_DEBUG 194471a5eb8SAppaRao Puli << "Value for key " << key 195471a5eb8SAppaRao Puli << " was incorrect type: " << jsonValue.type_name(); 196471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 197471a5eb8SAppaRao Puli } 198471a5eb8SAppaRao Puli value = std::move(*jsonPtr); 199471a5eb8SAppaRao Puli } 200471a5eb8SAppaRao Puli return ret; 201471a5eb8SAppaRao Puli } 202471a5eb8SAppaRao Puli 203471a5eb8SAppaRao Puli template <typename Type> 204ea2e6eecSWilly Tu bool unpackValue(nlohmann::json& jsonValue, std::string_view key, 205471a5eb8SAppaRao Puli crow::Response& res, Type& value) 206471a5eb8SAppaRao Puli { 207471a5eb8SAppaRao Puli bool ret = true; 208471a5eb8SAppaRao Puli 2092c70f800SEd Tanous if constexpr (IsOptional<Type>::value) 210471a5eb8SAppaRao Puli { 211471a5eb8SAppaRao Puli value.emplace(); 212471a5eb8SAppaRao Puli ret = unpackValue<typename Type::value_type>(jsonValue, key, res, 213471a5eb8SAppaRao Puli *value) && 214471a5eb8SAppaRao Puli ret; 215471a5eb8SAppaRao Puli } 2162c70f800SEd Tanous else if constexpr (IsStdArray<Type>::value) 217318226c2SJames Feist { 218318226c2SJames Feist if (!jsonValue.is_array()) 219318226c2SJames Feist { 22071f52d96SEd Tanous messages::propertyValueTypeError( 22171f52d96SEd Tanous res, 22271f52d96SEd Tanous res.jsonValue.dump(2, ' ', true, 22371f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 22471f52d96SEd Tanous key); 22541352c24SSantosh Puranik return false; 226318226c2SJames Feist } 227318226c2SJames Feist if (jsonValue.size() != value.size()) 228318226c2SJames Feist { 22971f52d96SEd Tanous messages::propertyValueTypeError( 23071f52d96SEd Tanous res, 23171f52d96SEd Tanous res.jsonValue.dump(2, ' ', true, 23271f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 23371f52d96SEd Tanous key); 23441352c24SSantosh Puranik return false; 235318226c2SJames Feist } 236318226c2SJames Feist size_t index = 0; 237318226c2SJames Feist for (const auto& val : jsonValue.items()) 238318226c2SJames Feist { 23941352c24SSantosh Puranik ret = unpackValue<typename Type::value_type>(val.value(), key, res, 24041352c24SSantosh Puranik value[index++]) && 24141352c24SSantosh Puranik ret; 242318226c2SJames Feist } 243318226c2SJames Feist } 2442c70f800SEd Tanous else if constexpr (IsVector<Type>::value) 245b1556427SEd Tanous { 246b1556427SEd Tanous if (!jsonValue.is_array()) 247b1556427SEd Tanous { 24871f52d96SEd Tanous messages::propertyValueTypeError( 24971f52d96SEd Tanous res, 25071f52d96SEd Tanous res.jsonValue.dump(2, ' ', true, 25171f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 25271f52d96SEd Tanous key); 25341352c24SSantosh Puranik return false; 254b1556427SEd Tanous } 255b1556427SEd Tanous 256b1556427SEd Tanous for (const auto& val : jsonValue.items()) 257b1556427SEd Tanous { 258b1556427SEd Tanous value.emplace_back(); 25941352c24SSantosh Puranik ret = unpackValue<typename Type::value_type>(val.value(), key, res, 26041352c24SSantosh Puranik value.back()) && 26141352c24SSantosh Puranik ret; 262b1556427SEd Tanous } 263b1556427SEd Tanous } 264771cfa0fSJason M. Bills else 265771cfa0fSJason M. Bills { 266471a5eb8SAppaRao Puli UnpackErrorCode ec = unpackValueWithErrorCode(jsonValue, key, value); 267471a5eb8SAppaRao Puli if (ec != UnpackErrorCode::success) 268771cfa0fSJason M. Bills { 269471a5eb8SAppaRao Puli if (ec == UnpackErrorCode::invalidType) 270471a5eb8SAppaRao Puli { 27171f52d96SEd Tanous messages::propertyValueTypeError( 27271f52d96SEd Tanous res, 27371f52d96SEd Tanous jsonValue.dump(2, ' ', true, 27471f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 27571f52d96SEd Tanous key); 276471a5eb8SAppaRao Puli } 277471a5eb8SAppaRao Puli else if (ec == UnpackErrorCode::outOfRange) 278471a5eb8SAppaRao Puli { 27971f52d96SEd Tanous messages::propertyValueNotInList( 28071f52d96SEd Tanous res, 28171f52d96SEd Tanous jsonValue.dump(2, ' ', true, 28271f52d96SEd Tanous nlohmann::json::error_handler_t::replace), 28371f52d96SEd Tanous key); 284471a5eb8SAppaRao Puli } 28541352c24SSantosh Puranik return false; 286771cfa0fSJason M. Bills } 287771cfa0fSJason M. Bills } 288471a5eb8SAppaRao Puli 289471a5eb8SAppaRao Puli return ret; 290471a5eb8SAppaRao Puli } 291471a5eb8SAppaRao Puli 292471a5eb8SAppaRao Puli template <typename Type> 293ea2e6eecSWilly Tu bool unpackValue(nlohmann::json& jsonValue, std::string_view key, Type& value) 294471a5eb8SAppaRao Puli { 295471a5eb8SAppaRao Puli bool ret = true; 2962c70f800SEd Tanous if constexpr (IsOptional<Type>::value) 297471a5eb8SAppaRao Puli { 298471a5eb8SAppaRao Puli value.emplace(); 299471a5eb8SAppaRao Puli ret = unpackValue<typename Type::value_type>(jsonValue, key, *value) && 300471a5eb8SAppaRao Puli ret; 301471a5eb8SAppaRao Puli } 3022c70f800SEd Tanous else if constexpr (IsStdArray<Type>::value) 303471a5eb8SAppaRao Puli { 304471a5eb8SAppaRao Puli if (!jsonValue.is_array()) 305471a5eb8SAppaRao Puli { 306471a5eb8SAppaRao Puli return false; 307471a5eb8SAppaRao Puli } 308471a5eb8SAppaRao Puli if (jsonValue.size() != value.size()) 309471a5eb8SAppaRao Puli { 310471a5eb8SAppaRao Puli return false; 311471a5eb8SAppaRao Puli } 312471a5eb8SAppaRao Puli size_t index = 0; 313471a5eb8SAppaRao Puli for (const auto& val : jsonValue.items()) 314471a5eb8SAppaRao Puli { 315471a5eb8SAppaRao Puli ret = unpackValue<typename Type::value_type>(val.value(), key, 316471a5eb8SAppaRao Puli value[index++]) && 317471a5eb8SAppaRao Puli ret; 318471a5eb8SAppaRao Puli } 319471a5eb8SAppaRao Puli } 3202c70f800SEd Tanous else if constexpr (IsVector<Type>::value) 321471a5eb8SAppaRao Puli { 322471a5eb8SAppaRao Puli if (!jsonValue.is_array()) 323471a5eb8SAppaRao Puli { 324471a5eb8SAppaRao Puli return false; 325471a5eb8SAppaRao Puli } 326471a5eb8SAppaRao Puli 327471a5eb8SAppaRao Puli for (const auto& val : jsonValue.items()) 328471a5eb8SAppaRao Puli { 329471a5eb8SAppaRao Puli value.emplace_back(); 330471a5eb8SAppaRao Puli ret = unpackValue<typename Type::value_type>(val.value(), key, 331471a5eb8SAppaRao Puli value.back()) && 332471a5eb8SAppaRao Puli ret; 333471a5eb8SAppaRao Puli } 334471a5eb8SAppaRao Puli } 335471a5eb8SAppaRao Puli else 336471a5eb8SAppaRao Puli { 337471a5eb8SAppaRao Puli UnpackErrorCode ec = unpackValueWithErrorCode(jsonValue, key, value); 338471a5eb8SAppaRao Puli if (ec != UnpackErrorCode::success) 339471a5eb8SAppaRao Puli { 340471a5eb8SAppaRao Puli return false; 341471a5eb8SAppaRao Puli } 342471a5eb8SAppaRao Puli } 343471a5eb8SAppaRao Puli 34441352c24SSantosh Puranik return ret; 345771cfa0fSJason M. Bills } 3469712f8acSEd Tanous } // namespace details 3479712f8acSEd Tanous 348ea2e6eecSWilly Tu // clang-format off 349ea2e6eecSWilly Tu using UnpackVariant = std::variant< 350ea2e6eecSWilly Tu uint8_t*, 351ea2e6eecSWilly Tu uint16_t*, 352ea2e6eecSWilly Tu int16_t*, 353ea2e6eecSWilly Tu uint32_t*, 354ea2e6eecSWilly Tu int32_t*, 355ea2e6eecSWilly Tu uint64_t*, 356ea2e6eecSWilly Tu int64_t*, 357ea2e6eecSWilly Tu bool*, 358ea2e6eecSWilly Tu double*, 359ea2e6eecSWilly Tu std::string*, 360ea2e6eecSWilly Tu nlohmann::json*, 361ea2e6eecSWilly Tu std::vector<uint8_t>*, 362ea2e6eecSWilly Tu std::vector<uint16_t>*, 363ea2e6eecSWilly Tu std::vector<int16_t>*, 364ea2e6eecSWilly Tu std::vector<uint32_t>*, 365ea2e6eecSWilly Tu std::vector<int32_t>*, 366ea2e6eecSWilly Tu std::vector<uint64_t>*, 367ea2e6eecSWilly Tu std::vector<int64_t>*, 368ea2e6eecSWilly Tu //std::vector<bool>*, 369ea2e6eecSWilly Tu std::vector<double>*, 370ea2e6eecSWilly Tu std::vector<std::string>*, 371ea2e6eecSWilly Tu std::vector<nlohmann::json>*, 372ea2e6eecSWilly Tu std::optional<uint8_t>*, 373ea2e6eecSWilly Tu std::optional<uint16_t>*, 374ea2e6eecSWilly Tu std::optional<int16_t>*, 375ea2e6eecSWilly Tu std::optional<uint32_t>*, 376ea2e6eecSWilly Tu std::optional<int32_t>*, 377ea2e6eecSWilly Tu std::optional<uint64_t>*, 378ea2e6eecSWilly Tu std::optional<int64_t>*, 379ea2e6eecSWilly Tu std::optional<bool>*, 380ea2e6eecSWilly Tu std::optional<double>*, 381ea2e6eecSWilly Tu std::optional<std::string>*, 382ea2e6eecSWilly Tu std::optional<nlohmann::json>*, 383ea2e6eecSWilly Tu std::optional<std::vector<uint8_t>>*, 384ea2e6eecSWilly Tu std::optional<std::vector<uint16_t>>*, 385ea2e6eecSWilly Tu std::optional<std::vector<int16_t>>*, 386ea2e6eecSWilly Tu std::optional<std::vector<uint32_t>>*, 387ea2e6eecSWilly Tu std::optional<std::vector<int32_t>>*, 388ea2e6eecSWilly Tu std::optional<std::vector<uint64_t>>*, 389ea2e6eecSWilly Tu std::optional<std::vector<int64_t>>*, 390ea2e6eecSWilly Tu //std::optional<std::vector<bool>>*, 391ea2e6eecSWilly Tu std::optional<std::vector<double>>*, 392ea2e6eecSWilly Tu std::optional<std::vector<std::string>>*, 393ea2e6eecSWilly Tu std::optional<std::vector<nlohmann::json>>* 394ea2e6eecSWilly Tu >; 395ea2e6eecSWilly Tu // clang-format on 396ea2e6eecSWilly Tu 397ea2e6eecSWilly Tu struct PerUnpack 398ea2e6eecSWilly Tu { 399ea2e6eecSWilly Tu std::string_view key; 400ea2e6eecSWilly Tu UnpackVariant value; 401ea2e6eecSWilly Tu bool complete = false; 402ea2e6eecSWilly Tu }; 403ea2e6eecSWilly Tu 404ea2e6eecSWilly Tu inline bool readJsonHelper(nlohmann::json& jsonRequest, crow::Response& res, 405ea2e6eecSWilly Tu std::span<PerUnpack> toUnpack) 4069712f8acSEd Tanous { 40741352c24SSantosh Puranik bool result = true; 408d91415c4SEd Tanous nlohmann::json::object_t* obj = 409d91415c4SEd Tanous jsonRequest.get_ptr<nlohmann::json::object_t*>(); 410d91415c4SEd Tanous if (obj == nullptr) 4119712f8acSEd Tanous { 4129712f8acSEd Tanous BMCWEB_LOG_DEBUG << "Json value is not an object"; 413f12894f8SJason M. Bills messages::unrecognizedRequestBody(res); 4149712f8acSEd Tanous return false; 4159712f8acSEd Tanous } 416d91415c4SEd Tanous for (auto& item : *obj) 4179712f8acSEd Tanous { 418ea2e6eecSWilly Tu size_t unpackIndex = 0; 419ea2e6eecSWilly Tu for (; unpackIndex < toUnpack.size(); unpackIndex++) 420ea2e6eecSWilly Tu { 421ea2e6eecSWilly Tu PerUnpack& unpackSpec = toUnpack[unpackIndex]; 422ea2e6eecSWilly Tu std::string_view key = unpackSpec.key; 423ea2e6eecSWilly Tu size_t keysplitIndex = key.find('/'); 424ea2e6eecSWilly Tu std::string_view leftover; 425ea2e6eecSWilly Tu if (keysplitIndex != std::string_view::npos) 426ea2e6eecSWilly Tu { 427ea2e6eecSWilly Tu leftover = key.substr(keysplitIndex + 1); 428ea2e6eecSWilly Tu key = key.substr(0, keysplitIndex); 429ea2e6eecSWilly Tu } 430ea2e6eecSWilly Tu 431d91415c4SEd Tanous if (key != item.first || unpackSpec.complete) 432ea2e6eecSWilly Tu { 433ea2e6eecSWilly Tu continue; 434ea2e6eecSWilly Tu } 435ea2e6eecSWilly Tu 436ea2e6eecSWilly Tu // Sublevel key 437ea2e6eecSWilly Tu if (!leftover.empty()) 438ea2e6eecSWilly Tu { 439ea2e6eecSWilly Tu // Include the slash in the key so we can compare later 440ea2e6eecSWilly Tu key = unpackSpec.key.substr(0, keysplitIndex + 1); 441ea2e6eecSWilly Tu nlohmann::json j; 442d91415c4SEd Tanous result = details::unpackValue<nlohmann::json>(item.second, key, 443ea2e6eecSWilly Tu res, j) && 44441352c24SSantosh Puranik result; 44555f79e6fSEd Tanous if (!result) 446ea2e6eecSWilly Tu { 447ea2e6eecSWilly Tu return result; 4489712f8acSEd Tanous } 4499712f8acSEd Tanous 450ea2e6eecSWilly Tu std::vector<PerUnpack> nextLevel; 451ea2e6eecSWilly Tu for (PerUnpack& p : toUnpack) 452ea2e6eecSWilly Tu { 453ea2e6eecSWilly Tu if (!p.key.starts_with(key)) 454ea2e6eecSWilly Tu { 455ea2e6eecSWilly Tu continue; 456ea2e6eecSWilly Tu } 457ea2e6eecSWilly Tu std::string_view thisLeftover = p.key.substr(key.size()); 458ea2e6eecSWilly Tu nextLevel.push_back({thisLeftover, p.value, false}); 459ea2e6eecSWilly Tu p.complete = true; 4609712f8acSEd Tanous } 46177dd8813SKowalski, Kamil 462ea2e6eecSWilly Tu result = readJsonHelper(j, res, nextLevel) && result; 463ea2e6eecSWilly Tu break; 464ea2e6eecSWilly Tu } 465ea2e6eecSWilly Tu 466002d39b4SEd Tanous result = std::visit( 467ea2e6eecSWilly Tu [&item, &unpackSpec, &res](auto&& val) { 468ea2e6eecSWilly Tu using ContainedT = 469ea2e6eecSWilly Tu std::remove_pointer_t<std::decay_t<decltype(val)>>; 470ea2e6eecSWilly Tu return details::unpackValue<ContainedT>( 471d91415c4SEd Tanous item.second, unpackSpec.key, res, *val); 472ea2e6eecSWilly Tu }, 473ea2e6eecSWilly Tu unpackSpec.value) && 474ea2e6eecSWilly Tu result; 475ea2e6eecSWilly Tu 476ea2e6eecSWilly Tu unpackSpec.complete = true; 477ea2e6eecSWilly Tu break; 478ea2e6eecSWilly Tu } 479ea2e6eecSWilly Tu 480ea2e6eecSWilly Tu if (unpackIndex == toUnpack.size()) 481ea2e6eecSWilly Tu { 482d91415c4SEd Tanous messages::propertyUnknown(res, item.first); 483ea2e6eecSWilly Tu result = false; 484ea2e6eecSWilly Tu } 485ea2e6eecSWilly Tu } 486ea2e6eecSWilly Tu 487ea2e6eecSWilly Tu for (PerUnpack& perUnpack : toUnpack) 488ea2e6eecSWilly Tu { 48955f79e6fSEd Tanous if (!perUnpack.complete) 490ea2e6eecSWilly Tu { 491ea2e6eecSWilly Tu bool isOptional = std::visit( 492ea2e6eecSWilly Tu [](auto&& val) { 493ea2e6eecSWilly Tu using ContainedType = 494ea2e6eecSWilly Tu std::remove_pointer_t<std::decay_t<decltype(val)>>; 495ea2e6eecSWilly Tu return details::IsOptional<ContainedType>::value; 496ea2e6eecSWilly Tu }, 497ea2e6eecSWilly Tu perUnpack.value); 498ea2e6eecSWilly Tu if (isOptional) 499ea2e6eecSWilly Tu { 500ea2e6eecSWilly Tu continue; 501ea2e6eecSWilly Tu } 502ea2e6eecSWilly Tu messages::propertyMissing(res, perUnpack.key); 503ea2e6eecSWilly Tu result = false; 504ea2e6eecSWilly Tu } 505ea2e6eecSWilly Tu } 506ea2e6eecSWilly Tu return result; 507ea2e6eecSWilly Tu } 508ea2e6eecSWilly Tu 50989492a15SPatrick Williams inline void packVariant(std::span<PerUnpack> /*toPack*/) {} 510ea2e6eecSWilly Tu 511ea2e6eecSWilly Tu template <typename FirstType, typename... UnpackTypes> 512ea2e6eecSWilly Tu void packVariant(std::span<PerUnpack> toPack, std::string_view key, 513ea2e6eecSWilly Tu FirstType& first, UnpackTypes&&... in) 514ea2e6eecSWilly Tu { 515ea2e6eecSWilly Tu if (toPack.empty()) 516ea2e6eecSWilly Tu { 517ea2e6eecSWilly Tu return; 518ea2e6eecSWilly Tu } 519ea2e6eecSWilly Tu toPack[0].key = key; 520ea2e6eecSWilly Tu toPack[0].value = &first; 521ea2e6eecSWilly Tu // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay) 522ea2e6eecSWilly Tu packVariant(toPack.subspan(1), std::forward<UnpackTypes&&>(in)...); 523ea2e6eecSWilly Tu } 524ea2e6eecSWilly Tu 525ea2e6eecSWilly Tu template <typename FirstType, typename... UnpackTypes> 526ea2e6eecSWilly Tu bool readJson(nlohmann::json& jsonRequest, crow::Response& res, 527ea2e6eecSWilly Tu std::string_view key, FirstType&& first, UnpackTypes&&... in) 528ea2e6eecSWilly Tu { 529ea2e6eecSWilly Tu const std::size_t n = sizeof...(UnpackTypes) + 2; 530ea2e6eecSWilly Tu std::array<PerUnpack, n / 2> toUnpack2; 531ea2e6eecSWilly Tu packVariant(toUnpack2, key, first, std::forward<UnpackTypes&&>(in)...); 532ea2e6eecSWilly Tu return readJsonHelper(jsonRequest, res, toUnpack2); 533ea2e6eecSWilly Tu } 534ea2e6eecSWilly Tu 535ea2e6eecSWilly Tu inline std::optional<nlohmann::json> 536ea2e6eecSWilly Tu readJsonPatchHelper(const crow::Request& req, crow::Response& res) 5370627a2c7SEd Tanous { 5380627a2c7SEd Tanous nlohmann::json jsonRequest; 5390627a2c7SEd Tanous if (!json_util::processJsonFromRequest(res, req, jsonRequest)) 5400627a2c7SEd Tanous { 5410627a2c7SEd Tanous BMCWEB_LOG_DEBUG << "Json value not readable"; 542ea2e6eecSWilly Tu return std::nullopt; 5430627a2c7SEd Tanous } 544357bb8f8SEd Tanous nlohmann::json::object_t* object = 545357bb8f8SEd Tanous jsonRequest.get_ptr<nlohmann::json::object_t*>(); 546357bb8f8SEd Tanous if (object == nullptr || object->empty()) 54715ed6780SWilly Tu { 54815ed6780SWilly Tu BMCWEB_LOG_DEBUG << "Json value is empty"; 54915ed6780SWilly Tu messages::emptyJSON(res); 550ea2e6eecSWilly Tu return std::nullopt; 551ea2e6eecSWilly Tu } 552357bb8f8SEd Tanous std::erase_if(*object, 553357bb8f8SEd Tanous [](const std::pair<std::string, nlohmann::json>& item) { 554357bb8f8SEd Tanous return item.first.starts_with("@odata."); 555357bb8f8SEd Tanous }); 556357bb8f8SEd Tanous if (object->empty()) 557357bb8f8SEd Tanous { 558357bb8f8SEd Tanous // If the update request only contains OData annotations, the service 559357bb8f8SEd Tanous // should return the HTTP 400 Bad Request status code with the 560357bb8f8SEd Tanous // NoOperation message from the Base Message Registry, ... 561357bb8f8SEd Tanous messages::noOperation(res); 562357bb8f8SEd Tanous return std::nullopt; 563357bb8f8SEd Tanous } 564357bb8f8SEd Tanous 565ea2e6eecSWilly Tu return {std::move(jsonRequest)}; 566ea2e6eecSWilly Tu } 567ea2e6eecSWilly Tu 568ea2e6eecSWilly Tu template <typename... UnpackTypes> 569ea2e6eecSWilly Tu bool readJsonPatch(const crow::Request& req, crow::Response& res, 570ea2e6eecSWilly Tu std::string_view key, UnpackTypes&&... in) 571ea2e6eecSWilly Tu { 572ea2e6eecSWilly Tu std::optional<nlohmann::json> jsonRequest = readJsonPatchHelper(req, res); 573ea2e6eecSWilly Tu if (jsonRequest == std::nullopt) 574ea2e6eecSWilly Tu { 57515ed6780SWilly Tu return false; 57615ed6780SWilly Tu } 57715ed6780SWilly Tu 578ea2e6eecSWilly Tu return readJson(*jsonRequest, res, key, std::forward<UnpackTypes&&>(in)...); 57915ed6780SWilly Tu } 58015ed6780SWilly Tu 58115ed6780SWilly Tu template <typename... UnpackTypes> 58215ed6780SWilly Tu bool readJsonAction(const crow::Request& req, crow::Response& res, 583ea2e6eecSWilly Tu const char* key, UnpackTypes&&... in) 58415ed6780SWilly Tu { 58515ed6780SWilly Tu nlohmann::json jsonRequest; 58615ed6780SWilly Tu if (!json_util::processJsonFromRequest(res, req, jsonRequest)) 58715ed6780SWilly Tu { 58815ed6780SWilly Tu BMCWEB_LOG_DEBUG << "Json value not readable"; 58915ed6780SWilly Tu return false; 59015ed6780SWilly Tu } 591ea2e6eecSWilly Tu return readJson(jsonRequest, res, key, std::forward<UnpackTypes&&>(in)...); 5920627a2c7SEd Tanous } 59377dd8813SKowalski, Kamil } // namespace json_util 59477dd8813SKowalski, Kamil } // namespace redfish 595