1*40e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 2*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 3*40e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation 477dd8813SKowalski, Kamil #pragma once 59712f8acSEd Tanous 6d5c80ad9SNan Zhou #include "error_messages.hpp" 78a7c4b47SNan Zhou #include "http_connection.hpp" 8d5c80ad9SNan Zhou #include "http_request.hpp" 9d5c80ad9SNan Zhou #include "http_response.hpp" 10185444b1SNan Zhou #include "human_sort.hpp" 11d5c80ad9SNan Zhou #include "logging.hpp" 12faf100f9SEd Tanous 13e7bcf475SJayanth Othayoth #include <boost/system/result.hpp> 14e7bcf475SJayanth Othayoth #include <boost/url/parse.hpp> 15e7bcf475SJayanth Othayoth #include <boost/url/url_view.hpp> 16faf100f9SEd Tanous #include <nlohmann/json.hpp> 170627a2c7SEd Tanous 18185444b1SNan Zhou #include <algorithm> 19d5c80ad9SNan Zhou #include <array> 20d5c80ad9SNan Zhou #include <cmath> 21d5c80ad9SNan Zhou #include <cstddef> 22d5c80ad9SNan Zhou #include <cstdint> 23d5c80ad9SNan Zhou #include <limits> 24d5c80ad9SNan Zhou #include <map> 25d5c80ad9SNan Zhou #include <optional> 263544d2a7SEd Tanous #include <ranges> 27ea2e6eecSWilly Tu #include <span> 28d5c80ad9SNan Zhou #include <string> 29d5c80ad9SNan Zhou #include <string_view> 30d5c80ad9SNan Zhou #include <type_traits> 31d5c80ad9SNan Zhou #include <utility> 32d5c80ad9SNan Zhou #include <variant> 33d5c80ad9SNan Zhou #include <vector> 34d5c80ad9SNan Zhou 351e75e1ddSNan Zhou // IWYU pragma: no_forward_declare crow::Request 361214b7e7SGunnar Mills 371abe55efSEd Tanous namespace redfish 381abe55efSEd Tanous { 391abe55efSEd Tanous 401abe55efSEd Tanous namespace json_util 411abe55efSEd Tanous { 4277dd8813SKowalski, Kamil 4377dd8813SKowalski, Kamil /** 4477dd8813SKowalski, Kamil * @brief Processes request to extract JSON from its body. If it fails, adds 4577dd8813SKowalski, Kamil * MalformedJSON message to response and ends it. 4677dd8813SKowalski, Kamil * 4777dd8813SKowalski, Kamil * @param[io] res Response object 4877dd8813SKowalski, Kamil * @param[in] req Request object 4977dd8813SKowalski, Kamil * @param[out] reqJson JSON object extracted from request's body 5077dd8813SKowalski, Kamil * 5177dd8813SKowalski, Kamil * @return true if JSON is valid, false when JSON is invalid and response has 5277dd8813SKowalski, Kamil * been filled with message and ended. 5377dd8813SKowalski, Kamil */ 5455c7b7a2SEd Tanous bool processJsonFromRequest(crow::Response& res, const crow::Request& req, 5577dd8813SKowalski, Kamil nlohmann::json& reqJson); 569712f8acSEd Tanous namespace details 579712f8acSEd Tanous { 58771cfa0fSJason M. Bills 591214b7e7SGunnar Mills template <typename Type> 602c70f800SEd Tanous struct IsOptional : std::false_type 611214b7e7SGunnar Mills {}; 629712f8acSEd Tanous 63771cfa0fSJason M. Bills template <typename Type> 642c70f800SEd Tanous struct IsOptional<std::optional<Type>> : std::true_type 651214b7e7SGunnar Mills {}; 669712f8acSEd Tanous 67771cfa0fSJason M. Bills template <typename Type> 682c70f800SEd Tanous struct IsVector : std::false_type 691214b7e7SGunnar Mills {}; 70b1556427SEd Tanous 711214b7e7SGunnar Mills template <typename Type> 722c70f800SEd Tanous struct IsVector<std::vector<Type>> : std::true_type 731214b7e7SGunnar Mills {}; 74b1556427SEd Tanous 751214b7e7SGunnar Mills template <typename Type> 762c70f800SEd Tanous struct IsStdArray : std::false_type 771214b7e7SGunnar Mills {}; 78318226c2SJames Feist 79318226c2SJames Feist template <typename Type, std::size_t size> 802c70f800SEd Tanous struct IsStdArray<std::array<Type, size>> : std::true_type 811214b7e7SGunnar Mills {}; 82318226c2SJames Feist 838099c517SEd Tanous template <typename Type> 848099c517SEd Tanous struct IsVariant : std::false_type 858099c517SEd Tanous {}; 868099c517SEd Tanous 878099c517SEd Tanous template <typename... Types> 888099c517SEd Tanous struct IsVariant<std::variant<Types...>> : std::true_type 898099c517SEd Tanous {}; 908099c517SEd Tanous 91471a5eb8SAppaRao Puli enum class UnpackErrorCode 92471a5eb8SAppaRao Puli { 93471a5eb8SAppaRao Puli success, 94471a5eb8SAppaRao Puli invalidType, 95471a5eb8SAppaRao Puli outOfRange 96471a5eb8SAppaRao Puli }; 97471a5eb8SAppaRao Puli 98a6acbb31SJames Feist template <typename ToType, typename FromType> 99c09966bdSEd Tanous bool checkRange(const FromType& from [[maybe_unused]], 100c09966bdSEd Tanous std::string_view key [[maybe_unused]]) 101a6acbb31SJames Feist { 102a6acbb31SJames Feist if constexpr (std::is_floating_point_v<ToType>) 103a6acbb31SJames Feist { 104ee344e0fSEd Tanous if (std::isnan(from)) 105a6acbb31SJames Feist { 10662598e31SEd Tanous BMCWEB_LOG_DEBUG("Value for key {} was NAN", key); 107a6acbb31SJames Feist return false; 108a6acbb31SJames Feist } 109a6acbb31SJames Feist } 110c09966bdSEd Tanous if constexpr (std::numeric_limits<ToType>::max() < 111c09966bdSEd Tanous std::numeric_limits<FromType>::max()) 112c09966bdSEd Tanous { 113c09966bdSEd Tanous if (from > std::numeric_limits<ToType>::max()) 114c09966bdSEd Tanous { 115c09966bdSEd Tanous BMCWEB_LOG_DEBUG("Value for key {} was greater than max {}", key, 116c09966bdSEd Tanous std::numeric_limits<FromType>::max()); 117c09966bdSEd Tanous return false; 118c09966bdSEd Tanous } 119c09966bdSEd Tanous } 120c09966bdSEd Tanous if constexpr (std::numeric_limits<ToType>::lowest() > 121c09966bdSEd Tanous std::numeric_limits<FromType>::lowest()) 122c09966bdSEd Tanous { 123c09966bdSEd Tanous if (from < std::numeric_limits<ToType>::lowest()) 124c09966bdSEd Tanous { 125c09966bdSEd Tanous BMCWEB_LOG_DEBUG("Value for key {} was less than min {}", key, 126c09966bdSEd Tanous std::numeric_limits<FromType>::lowest()); 127c09966bdSEd Tanous return false; 128c09966bdSEd Tanous } 129c09966bdSEd Tanous } 130a6acbb31SJames Feist 131a6acbb31SJames Feist return true; 132a6acbb31SJames Feist } 133a6acbb31SJames Feist 134771cfa0fSJason M. Bills template <typename Type> 135471a5eb8SAppaRao Puli UnpackErrorCode unpackValueWithErrorCode(nlohmann::json& jsonValue, 1368099c517SEd Tanous std::string_view key, Type& value); 1378099c517SEd Tanous 1388099c517SEd Tanous template <std::size_t Index = 0, typename... Args> 1398099c517SEd Tanous UnpackErrorCode unpackValueVariant(nlohmann::json& j, std::string_view key, 1408099c517SEd Tanous std::variant<Args...>& v) 1418099c517SEd Tanous { 1428099c517SEd Tanous if constexpr (Index < std::variant_size_v<std::variant<Args...>>) 1438099c517SEd Tanous { 144ed4de7a8SEd Tanous std::variant_alternative_t<Index, std::variant<Args...>> type{}; 1458099c517SEd Tanous UnpackErrorCode unpack = unpackValueWithErrorCode(j, key, type); 1468099c517SEd Tanous if (unpack == UnpackErrorCode::success) 1478099c517SEd Tanous { 1488099c517SEd Tanous v = std::move(type); 1498099c517SEd Tanous return unpack; 1508099c517SEd Tanous } 1518099c517SEd Tanous 1528099c517SEd Tanous return unpackValueVariant<Index + 1, Args...>(j, key, v); 1538099c517SEd Tanous } 1548099c517SEd Tanous return UnpackErrorCode::invalidType; 1558099c517SEd Tanous } 1568099c517SEd Tanous 1578099c517SEd Tanous template <typename Type> 1588099c517SEd Tanous UnpackErrorCode unpackValueWithErrorCode(nlohmann::json& jsonValue, 159ea2e6eecSWilly Tu std::string_view key, Type& value) 160771cfa0fSJason M. Bills { 161471a5eb8SAppaRao Puli UnpackErrorCode ret = UnpackErrorCode::success; 16241352c24SSantosh Puranik 163a6acbb31SJames Feist if constexpr (std::is_floating_point_v<Type>) 164771cfa0fSJason M. Bills { 165a6acbb31SJames Feist double helper = 0; 166a6acbb31SJames Feist double* jsonPtr = jsonValue.get_ptr<double*>(); 167771cfa0fSJason M. Bills 168771cfa0fSJason M. Bills if (jsonPtr == nullptr) 169771cfa0fSJason M. Bills { 170a6acbb31SJames Feist int64_t* intPtr = jsonValue.get_ptr<int64_t*>(); 171a6acbb31SJames Feist if (intPtr != nullptr) 172771cfa0fSJason M. Bills { 173a6acbb31SJames Feist helper = static_cast<double>(*intPtr); 174a6acbb31SJames Feist jsonPtr = &helper; 175771cfa0fSJason M. Bills } 176a6acbb31SJames Feist } 1775eb2bef2SAppaRao Puli if (jsonPtr == nullptr) 1785eb2bef2SAppaRao Puli { 179471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 1805eb2bef2SAppaRao Puli } 181cb13a392SEd Tanous if (!checkRange<Type>(*jsonPtr, key)) 182771cfa0fSJason M. Bills { 183471a5eb8SAppaRao Puli return UnpackErrorCode::outOfRange; 184771cfa0fSJason M. Bills } 185771cfa0fSJason M. Bills value = static_cast<Type>(*jsonPtr); 186771cfa0fSJason M. Bills } 187a6acbb31SJames Feist 188a6acbb31SJames Feist else if constexpr (std::is_signed_v<Type>) 189a6acbb31SJames Feist { 190a6acbb31SJames Feist int64_t* jsonPtr = jsonValue.get_ptr<int64_t*>(); 191271584abSEd Tanous if (jsonPtr == nullptr) 192271584abSEd Tanous { 193471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 194271584abSEd Tanous } 195cb13a392SEd Tanous if (!checkRange<Type>(*jsonPtr, key)) 196a6acbb31SJames Feist { 197471a5eb8SAppaRao Puli return UnpackErrorCode::outOfRange; 198a6acbb31SJames Feist } 199a6acbb31SJames Feist value = static_cast<Type>(*jsonPtr); 200a6acbb31SJames Feist } 201a6acbb31SJames Feist 202bd79bce8SPatrick Williams else if constexpr ((std::is_unsigned_v<Type>) && 203bd79bce8SPatrick Williams (!std::is_same_v<bool, Type>)) 204a6acbb31SJames Feist { 205a6acbb31SJames Feist uint64_t* jsonPtr = jsonValue.get_ptr<uint64_t*>(); 206271584abSEd Tanous if (jsonPtr == nullptr) 207271584abSEd Tanous { 208471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 209271584abSEd Tanous } 210cb13a392SEd Tanous if (!checkRange<Type>(*jsonPtr, key)) 211a6acbb31SJames Feist { 212471a5eb8SAppaRao Puli return UnpackErrorCode::outOfRange; 213a6acbb31SJames Feist } 214a6acbb31SJames Feist value = static_cast<Type>(*jsonPtr); 215a6acbb31SJames Feist } 216a6acbb31SJames Feist 2170627a2c7SEd Tanous else if constexpr (std::is_same_v<nlohmann::json, Type>) 2180627a2c7SEd Tanous { 2190627a2c7SEd Tanous value = std::move(jsonValue); 2200627a2c7SEd Tanous } 2218099c517SEd Tanous else if constexpr (std::is_same_v<std::nullptr_t, Type>) 2228099c517SEd Tanous { 2238099c517SEd Tanous if (!jsonValue.is_null()) 2248099c517SEd Tanous { 2258099c517SEd Tanous return UnpackErrorCode::invalidType; 2268099c517SEd Tanous } 2278099c517SEd Tanous } 228ed4de7a8SEd Tanous else if constexpr (IsVector<Type>::value) 229ed4de7a8SEd Tanous { 230ed4de7a8SEd Tanous nlohmann::json::object_t* obj = 231ed4de7a8SEd Tanous jsonValue.get_ptr<nlohmann::json::object_t*>(); 232ed4de7a8SEd Tanous if (obj == nullptr) 233ed4de7a8SEd Tanous { 234ed4de7a8SEd Tanous return UnpackErrorCode::invalidType; 235ed4de7a8SEd Tanous } 236ed4de7a8SEd Tanous 237ed4de7a8SEd Tanous for (const auto& val : *obj) 238ed4de7a8SEd Tanous { 239ed4de7a8SEd Tanous value.emplace_back(); 240ed4de7a8SEd Tanous ret = unpackValueWithErrorCode<typename Type::value_type>( 241ed4de7a8SEd Tanous val, key, value.back()) && 242ed4de7a8SEd Tanous ret; 243ed4de7a8SEd Tanous } 244ed4de7a8SEd Tanous } 245471a5eb8SAppaRao Puli else 246471a5eb8SAppaRao Puli { 247471a5eb8SAppaRao Puli using JsonType = std::add_const_t<std::add_pointer_t<Type>>; 248471a5eb8SAppaRao Puli JsonType jsonPtr = jsonValue.get_ptr<JsonType>(); 249471a5eb8SAppaRao Puli if (jsonPtr == nullptr) 250471a5eb8SAppaRao Puli { 25162598e31SEd Tanous BMCWEB_LOG_DEBUG("Value for key {} was incorrect type: {}", key, 25262598e31SEd Tanous jsonValue.type_name()); 253471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 254471a5eb8SAppaRao Puli } 255471a5eb8SAppaRao Puli value = std::move(*jsonPtr); 256471a5eb8SAppaRao Puli } 257471a5eb8SAppaRao Puli return ret; 258471a5eb8SAppaRao Puli } 259471a5eb8SAppaRao Puli 260471a5eb8SAppaRao Puli template <typename Type> 261ea2e6eecSWilly Tu bool unpackValue(nlohmann::json& jsonValue, std::string_view key, 262471a5eb8SAppaRao Puli crow::Response& res, Type& value) 263471a5eb8SAppaRao Puli { 264471a5eb8SAppaRao Puli bool ret = true; 265471a5eb8SAppaRao Puli 2662c70f800SEd Tanous if constexpr (IsOptional<Type>::value) 267471a5eb8SAppaRao Puli { 268471a5eb8SAppaRao Puli value.emplace(); 269471a5eb8SAppaRao Puli ret = unpackValue<typename Type::value_type>(jsonValue, key, res, 270471a5eb8SAppaRao Puli *value) && 271471a5eb8SAppaRao Puli ret; 272471a5eb8SAppaRao Puli } 2732c70f800SEd Tanous else if constexpr (IsStdArray<Type>::value) 274318226c2SJames Feist { 2750bdda665SEd Tanous nlohmann::json::array_t* arr = 2760bdda665SEd Tanous jsonValue.get_ptr<nlohmann::json::array_t*>(); 2770bdda665SEd Tanous if (arr == nullptr) 278318226c2SJames Feist { 2792e8c4bdaSEd Tanous messages::propertyValueTypeError(res, res.jsonValue, key); 28041352c24SSantosh Puranik return false; 281318226c2SJames Feist } 282318226c2SJames Feist if (jsonValue.size() != value.size()) 283318226c2SJames Feist { 2842e8c4bdaSEd Tanous messages::propertyValueTypeError(res, res.jsonValue, key); 28541352c24SSantosh Puranik return false; 286318226c2SJames Feist } 287318226c2SJames Feist size_t index = 0; 2880bdda665SEd Tanous for (auto& val : *arr) 289318226c2SJames Feist { 2900bdda665SEd Tanous ret = unpackValue<typename Type::value_type>(val, key, res, 29141352c24SSantosh Puranik value[index++]) && 29241352c24SSantosh Puranik ret; 293318226c2SJames Feist } 294318226c2SJames Feist } 2952c70f800SEd Tanous else if constexpr (IsVector<Type>::value) 296b1556427SEd Tanous { 2970bdda665SEd Tanous nlohmann::json::array_t* arr = 2980bdda665SEd Tanous jsonValue.get_ptr<nlohmann::json::array_t*>(); 2990bdda665SEd Tanous if (arr == nullptr) 300b1556427SEd Tanous { 3012e8c4bdaSEd Tanous messages::propertyValueTypeError(res, res.jsonValue, key); 30241352c24SSantosh Puranik return false; 303b1556427SEd Tanous } 304b1556427SEd Tanous 3050bdda665SEd Tanous for (auto& val : *arr) 306b1556427SEd Tanous { 307b1556427SEd Tanous value.emplace_back(); 3080bdda665SEd Tanous ret = unpackValue<typename Type::value_type>(val, key, res, 30941352c24SSantosh Puranik value.back()) && 31041352c24SSantosh Puranik ret; 311b1556427SEd Tanous } 312b1556427SEd Tanous } 3138099c517SEd Tanous else if constexpr (IsVariant<Type>::value) 3148099c517SEd Tanous { 3158099c517SEd Tanous UnpackErrorCode ec = unpackValueVariant(jsonValue, key, value); 3168099c517SEd Tanous if (ec != UnpackErrorCode::success) 3178099c517SEd Tanous { 3188099c517SEd Tanous if (ec == UnpackErrorCode::invalidType) 3198099c517SEd Tanous { 3208099c517SEd Tanous messages::propertyValueTypeError(res, jsonValue, key); 3218099c517SEd Tanous } 3228099c517SEd Tanous else if (ec == UnpackErrorCode::outOfRange) 3238099c517SEd Tanous { 324340d74c8SMyung Bae messages::propertyValueOutOfRange(res, jsonValue, key); 3258099c517SEd Tanous } 3268099c517SEd Tanous return false; 3278099c517SEd Tanous } 3288099c517SEd Tanous } 329771cfa0fSJason M. Bills else 330771cfa0fSJason M. Bills { 331471a5eb8SAppaRao Puli UnpackErrorCode ec = unpackValueWithErrorCode(jsonValue, key, value); 332471a5eb8SAppaRao Puli if (ec != UnpackErrorCode::success) 333771cfa0fSJason M. Bills { 334471a5eb8SAppaRao Puli if (ec == UnpackErrorCode::invalidType) 335471a5eb8SAppaRao Puli { 3362e8c4bdaSEd Tanous messages::propertyValueTypeError(res, jsonValue, key); 337471a5eb8SAppaRao Puli } 338471a5eb8SAppaRao Puli else if (ec == UnpackErrorCode::outOfRange) 339471a5eb8SAppaRao Puli { 340340d74c8SMyung Bae messages::propertyValueOutOfRange(res, jsonValue, key); 341471a5eb8SAppaRao Puli } 34241352c24SSantosh Puranik return false; 343771cfa0fSJason M. Bills } 344771cfa0fSJason M. Bills } 345471a5eb8SAppaRao Puli 346471a5eb8SAppaRao Puli return ret; 347471a5eb8SAppaRao Puli } 348471a5eb8SAppaRao Puli 349471a5eb8SAppaRao Puli template <typename Type> 350ea2e6eecSWilly Tu bool unpackValue(nlohmann::json& jsonValue, std::string_view key, Type& value) 351471a5eb8SAppaRao Puli { 352471a5eb8SAppaRao Puli bool ret = true; 3532c70f800SEd Tanous if constexpr (IsOptional<Type>::value) 354471a5eb8SAppaRao Puli { 355471a5eb8SAppaRao Puli value.emplace(); 356471a5eb8SAppaRao Puli ret = unpackValue<typename Type::value_type>(jsonValue, key, *value) && 357471a5eb8SAppaRao Puli ret; 358471a5eb8SAppaRao Puli } 3592c70f800SEd Tanous else if constexpr (IsStdArray<Type>::value) 360471a5eb8SAppaRao Puli { 3610bdda665SEd Tanous nlohmann::json::array_t* arr = 3620bdda665SEd Tanous jsonValue.get_ptr<nlohmann::json::array_t*>(); 3630bdda665SEd Tanous if (arr == nullptr) 364471a5eb8SAppaRao Puli { 365471a5eb8SAppaRao Puli return false; 366471a5eb8SAppaRao Puli } 367471a5eb8SAppaRao Puli if (jsonValue.size() != value.size()) 368471a5eb8SAppaRao Puli { 369471a5eb8SAppaRao Puli return false; 370471a5eb8SAppaRao Puli } 371471a5eb8SAppaRao Puli size_t index = 0; 3720bdda665SEd Tanous for (const auto& val : *arr) 373471a5eb8SAppaRao Puli { 3740bdda665SEd Tanous ret = unpackValue<typename Type::value_type>(val, key, 375471a5eb8SAppaRao Puli value[index++]) && 376471a5eb8SAppaRao Puli ret; 377471a5eb8SAppaRao Puli } 378471a5eb8SAppaRao Puli } 3792c70f800SEd Tanous else if constexpr (IsVector<Type>::value) 380471a5eb8SAppaRao Puli { 3810bdda665SEd Tanous nlohmann::json::array_t* arr = 3820bdda665SEd Tanous jsonValue.get_ptr<nlohmann::json::array_t*>(); 3830bdda665SEd Tanous if (arr == nullptr) 384471a5eb8SAppaRao Puli { 385471a5eb8SAppaRao Puli return false; 386471a5eb8SAppaRao Puli } 387471a5eb8SAppaRao Puli 3880bdda665SEd Tanous for (const auto& val : *arr) 389471a5eb8SAppaRao Puli { 390471a5eb8SAppaRao Puli value.emplace_back(); 3910bdda665SEd Tanous ret = unpackValue<typename Type::value_type>(val, key, 392471a5eb8SAppaRao Puli value.back()) && 393471a5eb8SAppaRao Puli ret; 394471a5eb8SAppaRao Puli } 395471a5eb8SAppaRao Puli } 396471a5eb8SAppaRao Puli else 397471a5eb8SAppaRao Puli { 398471a5eb8SAppaRao Puli UnpackErrorCode ec = unpackValueWithErrorCode(jsonValue, key, value); 399471a5eb8SAppaRao Puli if (ec != UnpackErrorCode::success) 400471a5eb8SAppaRao Puli { 401471a5eb8SAppaRao Puli return false; 402471a5eb8SAppaRao Puli } 403471a5eb8SAppaRao Puli } 404471a5eb8SAppaRao Puli 40541352c24SSantosh Puranik return ret; 406771cfa0fSJason M. Bills } 4079712f8acSEd Tanous } // namespace details 4089712f8acSEd Tanous 409ea2e6eecSWilly Tu // clang-format off 410ea2e6eecSWilly Tu using UnpackVariant = std::variant< 411ea2e6eecSWilly Tu uint8_t*, 412ea2e6eecSWilly Tu uint16_t*, 413ea2e6eecSWilly Tu int16_t*, 414ea2e6eecSWilly Tu uint32_t*, 415ea2e6eecSWilly Tu int32_t*, 416ea2e6eecSWilly Tu uint64_t*, 417ea2e6eecSWilly Tu int64_t*, 418ea2e6eecSWilly Tu bool*, 419ea2e6eecSWilly Tu double*, 420ea2e6eecSWilly Tu std::string*, 421b6164cbeSEd Tanous nlohmann::json::object_t*, 4228099c517SEd Tanous std::variant<std::string, std::nullptr_t>*, 4238099c517SEd Tanous std::variant<uint8_t, std::nullptr_t>*, 4248099c517SEd Tanous std::variant<int16_t, std::nullptr_t>*, 4258099c517SEd Tanous std::variant<uint16_t, std::nullptr_t>*, 4268099c517SEd Tanous std::variant<int32_t, std::nullptr_t>*, 4278099c517SEd Tanous std::variant<uint32_t, std::nullptr_t>*, 4288099c517SEd Tanous std::variant<int64_t, std::nullptr_t>*, 4298099c517SEd Tanous std::variant<uint64_t, std::nullptr_t>*, 4308099c517SEd Tanous std::variant<double, std::nullptr_t>*, 4318099c517SEd Tanous std::variant<bool, std::nullptr_t>*, 432ea2e6eecSWilly Tu std::vector<uint8_t>*, 433ea2e6eecSWilly Tu std::vector<uint16_t>*, 434ea2e6eecSWilly Tu std::vector<int16_t>*, 435ea2e6eecSWilly Tu std::vector<uint32_t>*, 436ea2e6eecSWilly Tu std::vector<int32_t>*, 437ea2e6eecSWilly Tu std::vector<uint64_t>*, 438ea2e6eecSWilly Tu std::vector<int64_t>*, 439ea2e6eecSWilly Tu //std::vector<bool>*, 440ea2e6eecSWilly Tu std::vector<double>*, 441ea2e6eecSWilly Tu std::vector<std::string>*, 442b6164cbeSEd Tanous std::vector<nlohmann::json::object_t>*, 443ea2e6eecSWilly Tu std::optional<uint8_t>*, 444ea2e6eecSWilly Tu std::optional<uint16_t>*, 445ea2e6eecSWilly Tu std::optional<int16_t>*, 446ea2e6eecSWilly Tu std::optional<uint32_t>*, 447ea2e6eecSWilly Tu std::optional<int32_t>*, 448ea2e6eecSWilly Tu std::optional<uint64_t>*, 449ea2e6eecSWilly Tu std::optional<int64_t>*, 450ea2e6eecSWilly Tu std::optional<bool>*, 451ea2e6eecSWilly Tu std::optional<double>*, 452ea2e6eecSWilly Tu std::optional<std::string>*, 453b6164cbeSEd Tanous std::optional<nlohmann::json::object_t>*, 454ea2e6eecSWilly Tu std::optional<std::vector<uint8_t>>*, 455ea2e6eecSWilly Tu std::optional<std::vector<uint16_t>>*, 456ea2e6eecSWilly Tu std::optional<std::vector<int16_t>>*, 457ea2e6eecSWilly Tu std::optional<std::vector<uint32_t>>*, 458ea2e6eecSWilly Tu std::optional<std::vector<int32_t>>*, 459ea2e6eecSWilly Tu std::optional<std::vector<uint64_t>>*, 460ea2e6eecSWilly Tu std::optional<std::vector<int64_t>>*, 461ea2e6eecSWilly Tu //std::optional<std::vector<bool>>*, 462ea2e6eecSWilly Tu std::optional<std::vector<double>>*, 463ea2e6eecSWilly Tu std::optional<std::vector<std::string>>*, 4648099c517SEd Tanous std::optional<std::vector<nlohmann::json::object_t>>*, 4658099c517SEd Tanous std::optional<std::variant<std::string, std::nullptr_t>>*, 4668099c517SEd Tanous std::optional<std::variant<uint8_t, std::nullptr_t>>*, 4678099c517SEd Tanous std::optional<std::variant<int16_t, std::nullptr_t>>*, 4688099c517SEd Tanous std::optional<std::variant<uint16_t, std::nullptr_t>>*, 4698099c517SEd Tanous std::optional<std::variant<int32_t, std::nullptr_t>>*, 4708099c517SEd Tanous std::optional<std::variant<uint32_t, std::nullptr_t>>*, 4718099c517SEd Tanous std::optional<std::variant<int64_t, std::nullptr_t>>*, 4728099c517SEd Tanous std::optional<std::variant<uint64_t, std::nullptr_t>>*, 4738099c517SEd Tanous std::optional<std::variant<double, std::nullptr_t>>*, 4748099c517SEd Tanous std::optional<std::variant<bool, std::nullptr_t>>*, 4758099c517SEd Tanous std::optional<std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>*, 476ed4de7a8SEd Tanous std::optional<std::vector<std::variant<std::string, nlohmann::json::object_t, std::nullptr_t>>>*, 477ed4de7a8SEd Tanous 478ed4de7a8SEd Tanous // Note, these types are kept for historical completeness, but should not be used, 479ed4de7a8SEd Tanous // As they do not provide object type safety. Instead, rely on nlohmann::json::object_t 480ed4de7a8SEd Tanous // Will be removed Q2 2025 481ed4de7a8SEd Tanous nlohmann::json*, 482ed4de7a8SEd Tanous std::optional<std::vector<nlohmann::json>>*, 483ed4de7a8SEd Tanous std::vector<nlohmann::json>*, 484ed4de7a8SEd Tanous std::optional<nlohmann::json>* 485ea2e6eecSWilly Tu >; 486ea2e6eecSWilly Tu // clang-format on 487ea2e6eecSWilly Tu 488ea2e6eecSWilly Tu struct PerUnpack 489ea2e6eecSWilly Tu { 490ea2e6eecSWilly Tu std::string_view key; 491ea2e6eecSWilly Tu UnpackVariant value; 492ea2e6eecSWilly Tu bool complete = false; 493ea2e6eecSWilly Tu }; 494ea2e6eecSWilly Tu 495ea2e6eecSWilly Tu inline bool readJsonHelper(nlohmann::json& jsonRequest, crow::Response& res, 496b6164cbeSEd Tanous std::span<PerUnpack> toUnpack); 497b6164cbeSEd Tanous 498b6164cbeSEd Tanous inline bool readJsonHelperObject(nlohmann::json::object_t& obj, 499b6164cbeSEd Tanous crow::Response& res, 500ea2e6eecSWilly Tu std::span<PerUnpack> toUnpack) 5019712f8acSEd Tanous { 50241352c24SSantosh Puranik bool result = true; 503b6164cbeSEd Tanous for (auto& item : obj) 5049712f8acSEd Tanous { 505ea2e6eecSWilly Tu size_t unpackIndex = 0; 506ea2e6eecSWilly Tu for (; unpackIndex < toUnpack.size(); unpackIndex++) 507ea2e6eecSWilly Tu { 508ea2e6eecSWilly Tu PerUnpack& unpackSpec = toUnpack[unpackIndex]; 509ea2e6eecSWilly Tu std::string_view key = unpackSpec.key; 510ea2e6eecSWilly Tu size_t keysplitIndex = key.find('/'); 511ea2e6eecSWilly Tu std::string_view leftover; 512ea2e6eecSWilly Tu if (keysplitIndex != std::string_view::npos) 513ea2e6eecSWilly Tu { 514ea2e6eecSWilly Tu leftover = key.substr(keysplitIndex + 1); 515ea2e6eecSWilly Tu key = key.substr(0, keysplitIndex); 516ea2e6eecSWilly Tu } 517ea2e6eecSWilly Tu 518d91415c4SEd Tanous if (key != item.first || unpackSpec.complete) 519ea2e6eecSWilly Tu { 520ea2e6eecSWilly Tu continue; 521ea2e6eecSWilly Tu } 522ea2e6eecSWilly Tu 523ea2e6eecSWilly Tu // Sublevel key 524ea2e6eecSWilly Tu if (!leftover.empty()) 525ea2e6eecSWilly Tu { 526ea2e6eecSWilly Tu // Include the slash in the key so we can compare later 527ea2e6eecSWilly Tu key = unpackSpec.key.substr(0, keysplitIndex + 1); 528ea2e6eecSWilly Tu nlohmann::json j; 529d91415c4SEd Tanous result = details::unpackValue<nlohmann::json>(item.second, key, 530ea2e6eecSWilly Tu res, j) && 53141352c24SSantosh Puranik result; 53255f79e6fSEd Tanous if (!result) 533ea2e6eecSWilly Tu { 534ea2e6eecSWilly Tu return result; 5359712f8acSEd Tanous } 5369712f8acSEd Tanous 537ea2e6eecSWilly Tu std::vector<PerUnpack> nextLevel; 538ea2e6eecSWilly Tu for (PerUnpack& p : toUnpack) 539ea2e6eecSWilly Tu { 540ea2e6eecSWilly Tu if (!p.key.starts_with(key)) 541ea2e6eecSWilly Tu { 542ea2e6eecSWilly Tu continue; 543ea2e6eecSWilly Tu } 544ea2e6eecSWilly Tu std::string_view thisLeftover = p.key.substr(key.size()); 545ea2e6eecSWilly Tu nextLevel.push_back({thisLeftover, p.value, false}); 546ea2e6eecSWilly Tu p.complete = true; 5479712f8acSEd Tanous } 54877dd8813SKowalski, Kamil 549ea2e6eecSWilly Tu result = readJsonHelper(j, res, nextLevel) && result; 550ea2e6eecSWilly Tu break; 551ea2e6eecSWilly Tu } 552ea2e6eecSWilly Tu 553bd79bce8SPatrick Williams result = 554bd79bce8SPatrick Williams std::visit( 5555ea927bbSEd Tanous [&item, &unpackSpec, &res](auto& val) { 556ea2e6eecSWilly Tu using ContainedT = 557ea2e6eecSWilly Tu std::remove_pointer_t<std::decay_t<decltype(val)>>; 558ea2e6eecSWilly Tu return details::unpackValue<ContainedT>( 559d91415c4SEd Tanous item.second, unpackSpec.key, res, *val); 560ea2e6eecSWilly Tu }, 561ea2e6eecSWilly Tu unpackSpec.value) && 562ea2e6eecSWilly Tu result; 563ea2e6eecSWilly Tu 564ea2e6eecSWilly Tu unpackSpec.complete = true; 565ea2e6eecSWilly Tu break; 566ea2e6eecSWilly Tu } 567ea2e6eecSWilly Tu 568ea2e6eecSWilly Tu if (unpackIndex == toUnpack.size()) 569ea2e6eecSWilly Tu { 570d91415c4SEd Tanous messages::propertyUnknown(res, item.first); 571ea2e6eecSWilly Tu result = false; 572ea2e6eecSWilly Tu } 573ea2e6eecSWilly Tu } 574ea2e6eecSWilly Tu 575ea2e6eecSWilly Tu for (PerUnpack& perUnpack : toUnpack) 576ea2e6eecSWilly Tu { 57755f79e6fSEd Tanous if (!perUnpack.complete) 578ea2e6eecSWilly Tu { 579ea2e6eecSWilly Tu bool isOptional = std::visit( 5805ea927bbSEd Tanous [](auto& val) { 581ea2e6eecSWilly Tu using ContainedType = 582ea2e6eecSWilly Tu std::remove_pointer_t<std::decay_t<decltype(val)>>; 583ea2e6eecSWilly Tu return details::IsOptional<ContainedType>::value; 584ea2e6eecSWilly Tu }, 585ea2e6eecSWilly Tu perUnpack.value); 586ea2e6eecSWilly Tu if (isOptional) 587ea2e6eecSWilly Tu { 588ea2e6eecSWilly Tu continue; 589ea2e6eecSWilly Tu } 590ea2e6eecSWilly Tu messages::propertyMissing(res, perUnpack.key); 591ea2e6eecSWilly Tu result = false; 592ea2e6eecSWilly Tu } 593ea2e6eecSWilly Tu } 594ea2e6eecSWilly Tu return result; 595ea2e6eecSWilly Tu } 596ea2e6eecSWilly Tu 597b6164cbeSEd Tanous inline bool readJsonHelper(nlohmann::json& jsonRequest, crow::Response& res, 598b6164cbeSEd Tanous std::span<PerUnpack> toUnpack) 599b6164cbeSEd Tanous { 600b6164cbeSEd Tanous nlohmann::json::object_t* obj = 601b6164cbeSEd Tanous jsonRequest.get_ptr<nlohmann::json::object_t*>(); 602b6164cbeSEd Tanous if (obj == nullptr) 603b6164cbeSEd Tanous { 604b6164cbeSEd Tanous BMCWEB_LOG_DEBUG("Json value is not an object"); 605b6164cbeSEd Tanous messages::unrecognizedRequestBody(res); 606b6164cbeSEd Tanous return false; 607b6164cbeSEd Tanous } 608b6164cbeSEd Tanous return readJsonHelperObject(*obj, res, toUnpack); 609b6164cbeSEd Tanous } 610b6164cbeSEd Tanous 61189492a15SPatrick Williams inline void packVariant(std::span<PerUnpack> /*toPack*/) {} 612ea2e6eecSWilly Tu 613ea2e6eecSWilly Tu template <typename FirstType, typename... UnpackTypes> 614ea2e6eecSWilly Tu void packVariant(std::span<PerUnpack> toPack, std::string_view key, 6155ea927bbSEd Tanous FirstType&& first, UnpackTypes&&... in) 616ea2e6eecSWilly Tu { 617ea2e6eecSWilly Tu if (toPack.empty()) 618ea2e6eecSWilly Tu { 619ea2e6eecSWilly Tu return; 620ea2e6eecSWilly Tu } 621ea2e6eecSWilly Tu toPack[0].key = key; 622ea2e6eecSWilly Tu toPack[0].value = &first; 623ea2e6eecSWilly Tu // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay) 624ea2e6eecSWilly Tu packVariant(toPack.subspan(1), std::forward<UnpackTypes&&>(in)...); 625ea2e6eecSWilly Tu } 626ea2e6eecSWilly Tu 627ea2e6eecSWilly Tu template <typename FirstType, typename... UnpackTypes> 628b6164cbeSEd Tanous bool readJsonObject(nlohmann::json::object_t& jsonRequest, crow::Response& res, 629b6164cbeSEd Tanous std::string_view key, FirstType&& first, 630b6164cbeSEd Tanous UnpackTypes&&... in) 631ea2e6eecSWilly Tu { 632ea2e6eecSWilly Tu const std::size_t n = sizeof...(UnpackTypes) + 2; 633ea2e6eecSWilly Tu std::array<PerUnpack, n / 2> toUnpack2; 6345ea927bbSEd Tanous packVariant(toUnpack2, key, std::forward<FirstType>(first), 6355ea927bbSEd Tanous std::forward<UnpackTypes&&>(in)...); 636b6164cbeSEd Tanous return readJsonHelperObject(jsonRequest, res, toUnpack2); 637ea2e6eecSWilly Tu } 638ea2e6eecSWilly Tu 639b6164cbeSEd Tanous template <typename FirstType, typename... UnpackTypes> 640b6164cbeSEd Tanous bool readJson(nlohmann::json& jsonRequest, crow::Response& res, 641b6164cbeSEd Tanous std::string_view key, FirstType&& first, UnpackTypes&&... in) 642b6164cbeSEd Tanous { 643b6164cbeSEd Tanous nlohmann::json::object_t* obj = 644b6164cbeSEd Tanous jsonRequest.get_ptr<nlohmann::json::object_t*>(); 645b6164cbeSEd Tanous if (obj == nullptr) 646b6164cbeSEd Tanous { 647b6164cbeSEd Tanous BMCWEB_LOG_DEBUG("Json value is not an object"); 648b6164cbeSEd Tanous messages::unrecognizedRequestBody(res); 649b6164cbeSEd Tanous return false; 650b6164cbeSEd Tanous } 6515be2b14aSEd Tanous return readJsonObject(*obj, res, key, std::forward<FirstType>(first), 6525be2b14aSEd Tanous std::forward<UnpackTypes&&>(in)...); 653b6164cbeSEd Tanous } 654b6164cbeSEd Tanous 655b6164cbeSEd Tanous inline std::optional<nlohmann::json::object_t> 656ea2e6eecSWilly Tu readJsonPatchHelper(const crow::Request& req, crow::Response& res) 6570627a2c7SEd Tanous { 6580627a2c7SEd Tanous nlohmann::json jsonRequest; 6590627a2c7SEd Tanous if (!json_util::processJsonFromRequest(res, req, jsonRequest)) 6600627a2c7SEd Tanous { 66162598e31SEd Tanous BMCWEB_LOG_DEBUG("Json value not readable"); 662ea2e6eecSWilly Tu return std::nullopt; 6630627a2c7SEd Tanous } 664357bb8f8SEd Tanous nlohmann::json::object_t* object = 665357bb8f8SEd Tanous jsonRequest.get_ptr<nlohmann::json::object_t*>(); 666357bb8f8SEd Tanous if (object == nullptr || object->empty()) 66715ed6780SWilly Tu { 66862598e31SEd Tanous BMCWEB_LOG_DEBUG("Json value is empty"); 66915ed6780SWilly Tu messages::emptyJSON(res); 670ea2e6eecSWilly Tu return std::nullopt; 671ea2e6eecSWilly Tu } 672357bb8f8SEd Tanous std::erase_if(*object, 673357bb8f8SEd Tanous [](const std::pair<std::string, nlohmann::json>& item) { 674357bb8f8SEd Tanous return item.first.starts_with("@odata."); 675357bb8f8SEd Tanous }); 676357bb8f8SEd Tanous if (object->empty()) 677357bb8f8SEd Tanous { 678357bb8f8SEd Tanous // If the update request only contains OData annotations, the service 679357bb8f8SEd Tanous // should return the HTTP 400 Bad Request status code with the 680357bb8f8SEd Tanous // NoOperation message from the Base Message Registry, ... 681357bb8f8SEd Tanous messages::noOperation(res); 682357bb8f8SEd Tanous return std::nullopt; 683357bb8f8SEd Tanous } 684357bb8f8SEd Tanous 685b6164cbeSEd Tanous return {std::move(*object)}; 686ea2e6eecSWilly Tu } 687ea2e6eecSWilly Tu 688ea2e6eecSWilly Tu template <typename... UnpackTypes> 689ea2e6eecSWilly Tu bool readJsonPatch(const crow::Request& req, crow::Response& res, 690ea2e6eecSWilly Tu std::string_view key, UnpackTypes&&... in) 691ea2e6eecSWilly Tu { 69276b038f2SEd Tanous std::optional<nlohmann::json::object_t> jsonRequest = 69376b038f2SEd Tanous readJsonPatchHelper(req, res); 694e01d0c36SEd Tanous if (!jsonRequest) 695ea2e6eecSWilly Tu { 69615ed6780SWilly Tu return false; 69715ed6780SWilly Tu } 69876b038f2SEd Tanous if (jsonRequest->empty()) 699b6164cbeSEd Tanous { 700b6164cbeSEd Tanous messages::emptyJSON(res); 701b6164cbeSEd Tanous return false; 702b6164cbeSEd Tanous } 70315ed6780SWilly Tu 70476b038f2SEd Tanous return readJsonObject(*jsonRequest, res, key, 705b6164cbeSEd Tanous std::forward<UnpackTypes&&>(in)...); 70615ed6780SWilly Tu } 70715ed6780SWilly Tu 70815ed6780SWilly Tu template <typename... UnpackTypes> 70915ed6780SWilly Tu bool readJsonAction(const crow::Request& req, crow::Response& res, 710ea2e6eecSWilly Tu const char* key, UnpackTypes&&... in) 71115ed6780SWilly Tu { 71215ed6780SWilly Tu nlohmann::json jsonRequest; 71315ed6780SWilly Tu if (!json_util::processJsonFromRequest(res, req, jsonRequest)) 71415ed6780SWilly Tu { 71562598e31SEd Tanous BMCWEB_LOG_DEBUG("Json value not readable"); 71615ed6780SWilly Tu return false; 71715ed6780SWilly Tu } 718b6164cbeSEd Tanous nlohmann::json::object_t* object = 719b6164cbeSEd Tanous jsonRequest.get_ptr<nlohmann::json::object_t*>(); 720b6164cbeSEd Tanous if (object == nullptr) 721b6164cbeSEd Tanous { 722b6164cbeSEd Tanous BMCWEB_LOG_DEBUG("Json value is empty"); 723b6164cbeSEd Tanous messages::emptyJSON(res); 724b6164cbeSEd Tanous return false; 725b6164cbeSEd Tanous } 726b6164cbeSEd Tanous return readJsonObject(*object, res, key, 727b6164cbeSEd Tanous std::forward<UnpackTypes&&>(in)...); 7280627a2c7SEd Tanous } 729185444b1SNan Zhou 730185444b1SNan Zhou // Determines if two json objects are less, based on the presence of the 731185444b1SNan Zhou // @odata.id key 7324e196b9aSEd Tanous inline int objectKeyCmp(std::string_view key, const nlohmann::json& a, 7334e196b9aSEd Tanous const nlohmann::json& b) 734185444b1SNan Zhou { 735185444b1SNan Zhou using object_t = nlohmann::json::object_t; 736185444b1SNan Zhou const object_t* aObj = a.get_ptr<const object_t*>(); 737185444b1SNan Zhou const object_t* bObj = b.get_ptr<const object_t*>(); 738185444b1SNan Zhou 739185444b1SNan Zhou if (aObj == nullptr) 740185444b1SNan Zhou { 741185444b1SNan Zhou if (bObj == nullptr) 742185444b1SNan Zhou { 743185444b1SNan Zhou return 0; 744185444b1SNan Zhou } 745185444b1SNan Zhou return -1; 746185444b1SNan Zhou } 747185444b1SNan Zhou if (bObj == nullptr) 748185444b1SNan Zhou { 749185444b1SNan Zhou return 1; 750185444b1SNan Zhou } 7514e196b9aSEd Tanous object_t::const_iterator aIt = aObj->find(key); 7524e196b9aSEd Tanous object_t::const_iterator bIt = bObj->find(key); 7534e196b9aSEd Tanous // If either object doesn't have the key, they get "sorted" to the 7544e196b9aSEd Tanous // beginning. 755185444b1SNan Zhou if (aIt == aObj->end()) 756185444b1SNan Zhou { 757185444b1SNan Zhou if (bIt == bObj->end()) 758185444b1SNan Zhou { 759185444b1SNan Zhou return 0; 760185444b1SNan Zhou } 761185444b1SNan Zhou return -1; 762185444b1SNan Zhou } 763185444b1SNan Zhou if (bIt == bObj->end()) 764185444b1SNan Zhou { 765185444b1SNan Zhou return 1; 766185444b1SNan Zhou } 767185444b1SNan Zhou const nlohmann::json::string_t* nameA = 768185444b1SNan Zhou aIt->second.get_ptr<const std::string*>(); 769185444b1SNan Zhou const nlohmann::json::string_t* nameB = 770185444b1SNan Zhou bIt->second.get_ptr<const std::string*>(); 771185444b1SNan Zhou // If either object doesn't have a string as the key, they get "sorted" to 7724e196b9aSEd Tanous // the beginning. 773185444b1SNan Zhou if (nameA == nullptr) 774185444b1SNan Zhou { 775185444b1SNan Zhou if (nameB == nullptr) 776185444b1SNan Zhou { 777185444b1SNan Zhou return 0; 778185444b1SNan Zhou } 779185444b1SNan Zhou return -1; 780185444b1SNan Zhou } 781185444b1SNan Zhou if (nameB == nullptr) 782185444b1SNan Zhou { 783185444b1SNan Zhou return 1; 784185444b1SNan Zhou } 785e7bcf475SJayanth Othayoth if (key != "@odata.id") 786e7bcf475SJayanth Othayoth { 787e7bcf475SJayanth Othayoth return alphanumComp(*nameA, *nameB); 788e7bcf475SJayanth Othayoth } 789185444b1SNan Zhou 790e7bcf475SJayanth Othayoth boost::system::result<boost::urls::url_view> aUrl = 791e7bcf475SJayanth Othayoth boost::urls::parse_relative_ref(*nameA); 792e7bcf475SJayanth Othayoth boost::system::result<boost::urls::url_view> bUrl = 793e7bcf475SJayanth Othayoth boost::urls::parse_relative_ref(*nameB); 794e7bcf475SJayanth Othayoth if (!aUrl) 795185444b1SNan Zhou { 796e7bcf475SJayanth Othayoth if (!bUrl) 797185444b1SNan Zhou { 798185444b1SNan Zhou return 0; 799185444b1SNan Zhou } 800185444b1SNan Zhou return -1; 801185444b1SNan Zhou } 802e7bcf475SJayanth Othayoth if (!bUrl) 803e7bcf475SJayanth Othayoth { 804e7bcf475SJayanth Othayoth return 1; 805e7bcf475SJayanth Othayoth } 806e7bcf475SJayanth Othayoth 807e7bcf475SJayanth Othayoth auto segmentsAIt = aUrl->segments().begin(); 808e7bcf475SJayanth Othayoth auto segmentsBIt = bUrl->segments().begin(); 809e7bcf475SJayanth Othayoth 810e7bcf475SJayanth Othayoth while (true) 811e7bcf475SJayanth Othayoth { 812e7bcf475SJayanth Othayoth if (segmentsAIt == aUrl->segments().end()) 813e7bcf475SJayanth Othayoth { 814e7bcf475SJayanth Othayoth if (segmentsBIt == bUrl->segments().end()) 815e7bcf475SJayanth Othayoth { 816e7bcf475SJayanth Othayoth return 0; 817e7bcf475SJayanth Othayoth } 818e7bcf475SJayanth Othayoth return -1; 819e7bcf475SJayanth Othayoth } 820e7bcf475SJayanth Othayoth if (segmentsBIt == bUrl->segments().end()) 821185444b1SNan Zhou { 822185444b1SNan Zhou return 1; 823185444b1SNan Zhou } 824185444b1SNan Zhou int res = alphanumComp(*segmentsAIt, *segmentsBIt); 825185444b1SNan Zhou if (res != 0) 826185444b1SNan Zhou { 827185444b1SNan Zhou return res; 828185444b1SNan Zhou } 829185444b1SNan Zhou 830185444b1SNan Zhou segmentsAIt++; 831185444b1SNan Zhou segmentsBIt++; 832185444b1SNan Zhou } 833e7bcf475SJayanth Othayoth return 0; 834185444b1SNan Zhou }; 835185444b1SNan Zhou 8364e196b9aSEd Tanous // kept for backward compatibility 8374e196b9aSEd Tanous inline int odataObjectCmp(const nlohmann::json& left, 8384e196b9aSEd Tanous const nlohmann::json& right) 8394e196b9aSEd Tanous { 8404e196b9aSEd Tanous return objectKeyCmp("@odata.id", left, right); 8414e196b9aSEd Tanous } 8424e196b9aSEd Tanous 843185444b1SNan Zhou struct ODataObjectLess 844185444b1SNan Zhou { 8454e196b9aSEd Tanous std::string_view key; 8464e196b9aSEd Tanous 8474e196b9aSEd Tanous explicit ODataObjectLess(std::string_view keyIn) : key(keyIn) {} 8484e196b9aSEd Tanous 849185444b1SNan Zhou bool operator()(const nlohmann::json& left, 850185444b1SNan Zhou const nlohmann::json& right) const 851185444b1SNan Zhou { 8524e196b9aSEd Tanous return objectKeyCmp(key, left, right) < 0; 853185444b1SNan Zhou } 854185444b1SNan Zhou }; 855185444b1SNan Zhou 856185444b1SNan Zhou // Sort the JSON array by |element[key]|. 857185444b1SNan Zhou // Elements without |key| or type of |element[key]| is not string are smaller 858185444b1SNan Zhou // those whose |element[key]| is string. 8594e196b9aSEd Tanous inline void sortJsonArrayByKey(nlohmann::json::array_t& array, 8604e196b9aSEd Tanous std::string_view key) 8614e196b9aSEd Tanous { 8624e196b9aSEd Tanous std::ranges::sort(array, ODataObjectLess(key)); 8634e196b9aSEd Tanous } 8644e196b9aSEd Tanous 8654e196b9aSEd Tanous // Sort the JSON array by |element[key]|. 8664e196b9aSEd Tanous // Elements without |key| or type of |element[key]| is not string are smaller 8674e196b9aSEd Tanous // those whose |element[key]| is string. 868185444b1SNan Zhou inline void sortJsonArrayByOData(nlohmann::json::array_t& array) 869185444b1SNan Zhou { 8704e196b9aSEd Tanous std::ranges::sort(array, ODataObjectLess("@odata.id")); 871185444b1SNan Zhou } 872185444b1SNan Zhou 8738a7c4b47SNan Zhou // Returns the estimated size of the JSON value 8748a7c4b47SNan Zhou // The implementation walks through every key and every value, accumulates the 8758a7c4b47SNan Zhou // total size of keys and values. 8768a7c4b47SNan Zhou // Ideally, we should use a custom allocator that nlohmann JSON supports. 8778a7c4b47SNan Zhou 8788a7c4b47SNan Zhou // Assumption made: 8798a7c4b47SNan Zhou // 1. number: 8 characters 8808a7c4b47SNan Zhou // 2. boolean: 5 characters (False) 8818a7c4b47SNan Zhou // 3. string: len(str) + 2 characters (quote) 8828a7c4b47SNan Zhou // 4. bytes: len(bytes) characters 8838a7c4b47SNan Zhou // 5. null: 4 characters (null) 8848a7c4b47SNan Zhou uint64_t getEstimatedJsonSize(const nlohmann::json& root); 8858a7c4b47SNan Zhou 88677dd8813SKowalski, Kamil } // namespace json_util 88777dd8813SKowalski, Kamil } // namespace redfish 888