140e9b92eSEd Tanous // SPDX-License-Identifier: Apache-2.0 240e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright OpenBMC Authors 340e9b92eSEd Tanous // SPDX-FileCopyrightText: Copyright 2018 Intel Corporation 477dd8813SKowalski, Kamil #pragma once 59712f8acSEd Tanous 6d5c80ad9SNan Zhou #include "error_messages.hpp" 7d5c80ad9SNan Zhou #include "http_request.hpp" 8d5c80ad9SNan Zhou #include "http_response.hpp" 9185444b1SNan Zhou #include "human_sort.hpp" 10d5c80ad9SNan Zhou #include "logging.hpp" 11faf100f9SEd Tanous 12e7bcf475SJayanth Othayoth #include <boost/system/result.hpp> 13e7bcf475SJayanth Othayoth #include <boost/url/parse.hpp> 14e7bcf475SJayanth Othayoth #include <boost/url/url_view.hpp> 15faf100f9SEd Tanous #include <nlohmann/json.hpp> 160627a2c7SEd Tanous 17185444b1SNan Zhou #include <algorithm> 18d5c80ad9SNan Zhou #include <array> 19d5c80ad9SNan Zhou #include <cmath> 20d5c80ad9SNan Zhou #include <cstddef> 21d5c80ad9SNan Zhou #include <cstdint> 22d5c80ad9SNan Zhou #include <limits> 23d5c80ad9SNan Zhou #include <map> 24d5c80ad9SNan Zhou #include <optional> 253544d2a7SEd Tanous #include <ranges> 26ea2e6eecSWilly Tu #include <span> 27d5c80ad9SNan Zhou #include <string> 28d5c80ad9SNan Zhou #include <string_view> 29d5c80ad9SNan Zhou #include <type_traits> 30d5c80ad9SNan Zhou #include <utility> 31d5c80ad9SNan Zhou #include <variant> 32d5c80ad9SNan Zhou #include <vector> 33d5c80ad9SNan Zhou 341e75e1ddSNan Zhou // IWYU pragma: no_forward_declare crow::Request 351214b7e7SGunnar Mills 361abe55efSEd Tanous namespace redfish 371abe55efSEd Tanous { 381abe55efSEd Tanous 391abe55efSEd Tanous namespace json_util 401abe55efSEd Tanous { 4177dd8813SKowalski, Kamil 4277dd8813SKowalski, Kamil /** 4377dd8813SKowalski, Kamil * @brief Processes request to extract JSON from its body. If it fails, adds 4477dd8813SKowalski, Kamil * MalformedJSON message to response and ends it. 4577dd8813SKowalski, Kamil * 4677dd8813SKowalski, Kamil * @param[io] res Response object 4777dd8813SKowalski, Kamil * @param[in] req Request object 4877dd8813SKowalski, Kamil * @param[out] reqJson JSON object extracted from request's body 4977dd8813SKowalski, Kamil * 5077dd8813SKowalski, Kamil * @return true if JSON is valid, false when JSON is invalid and response has 5177dd8813SKowalski, Kamil * been filled with message and ended. 5277dd8813SKowalski, Kamil */ 5355c7b7a2SEd Tanous bool processJsonFromRequest(crow::Response& res, const crow::Request& req, 5477dd8813SKowalski, Kamil nlohmann::json& reqJson); 559712f8acSEd Tanous namespace details 569712f8acSEd Tanous { 57771cfa0fSJason M. Bills 581214b7e7SGunnar Mills template <typename Type> 592c70f800SEd Tanous struct IsOptional : std::false_type 601214b7e7SGunnar Mills {}; 619712f8acSEd Tanous 62771cfa0fSJason M. Bills template <typename Type> 632c70f800SEd Tanous struct IsOptional<std::optional<Type>> : std::true_type 641214b7e7SGunnar Mills {}; 659712f8acSEd Tanous 66771cfa0fSJason M. Bills template <typename Type> 672c70f800SEd Tanous struct IsVector : std::false_type 681214b7e7SGunnar Mills {}; 69b1556427SEd Tanous 701214b7e7SGunnar Mills template <typename Type> 712c70f800SEd Tanous struct IsVector<std::vector<Type>> : std::true_type 721214b7e7SGunnar Mills {}; 73b1556427SEd Tanous 741214b7e7SGunnar Mills template <typename Type> 752c70f800SEd Tanous struct IsStdArray : std::false_type 761214b7e7SGunnar Mills {}; 77318226c2SJames Feist 78318226c2SJames Feist template <typename Type, std::size_t size> 792c70f800SEd Tanous struct IsStdArray<std::array<Type, size>> : std::true_type 801214b7e7SGunnar Mills {}; 81318226c2SJames Feist 828099c517SEd Tanous template <typename Type> 838099c517SEd Tanous struct IsVariant : std::false_type 848099c517SEd Tanous {}; 858099c517SEd Tanous 868099c517SEd Tanous template <typename... Types> 878099c517SEd Tanous struct IsVariant<std::variant<Types...>> : std::true_type 888099c517SEd Tanous {}; 898099c517SEd Tanous 90471a5eb8SAppaRao Puli enum class UnpackErrorCode 91471a5eb8SAppaRao Puli { 92471a5eb8SAppaRao Puli success, 93471a5eb8SAppaRao Puli invalidType, 94471a5eb8SAppaRao Puli outOfRange 95471a5eb8SAppaRao Puli }; 96471a5eb8SAppaRao Puli 97a6acbb31SJames Feist template <typename ToType, typename FromType> 98*8d9cf72dSEd Tanous bool checkRange(const FromType& from, std::string_view key) 99a6acbb31SJames Feist { 100a6acbb31SJames Feist if constexpr (std::is_floating_point_v<ToType>) 101a6acbb31SJames Feist { 102ee344e0fSEd Tanous if (std::isnan(from)) 103a6acbb31SJames Feist { 10462598e31SEd Tanous BMCWEB_LOG_DEBUG("Value for key {} was NAN", key); 105a6acbb31SJames Feist return false; 106a6acbb31SJames Feist } 107*8d9cf72dSEd Tanous // Assume for the moment that all floats can represent the full range 108*8d9cf72dSEd Tanous // of any int/uint in a cast. This is close enough to true for the 109*8d9cf72dSEd Tanous // precision of this json parser. 110a6acbb31SJames Feist } 111*8d9cf72dSEd Tanous else 112c09966bdSEd Tanous { 113*8d9cf72dSEd Tanous if (std::cmp_greater(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 } 119*8d9cf72dSEd Tanous if (std::cmp_less(from, std::numeric_limits<ToType>::lowest())) 120c09966bdSEd Tanous { 121c09966bdSEd Tanous BMCWEB_LOG_DEBUG("Value for key {} was less than min {}", key, 122c09966bdSEd Tanous std::numeric_limits<FromType>::lowest()); 123c09966bdSEd Tanous return false; 124c09966bdSEd Tanous } 125c09966bdSEd Tanous } 126a6acbb31SJames Feist 127a6acbb31SJames Feist return true; 128a6acbb31SJames Feist } 129a6acbb31SJames Feist 130771cfa0fSJason M. Bills template <typename Type> 131471a5eb8SAppaRao Puli UnpackErrorCode unpackValueWithErrorCode(nlohmann::json& jsonValue, 1328099c517SEd Tanous std::string_view key, Type& value); 1338099c517SEd Tanous 1348099c517SEd Tanous template <std::size_t Index = 0, typename... Args> 1358099c517SEd Tanous UnpackErrorCode unpackValueVariant(nlohmann::json& j, std::string_view key, 1368099c517SEd Tanous std::variant<Args...>& v) 1378099c517SEd Tanous { 1388099c517SEd Tanous if constexpr (Index < std::variant_size_v<std::variant<Args...>>) 1398099c517SEd Tanous { 140ed4de7a8SEd Tanous std::variant_alternative_t<Index, std::variant<Args...>> type{}; 1418099c517SEd Tanous UnpackErrorCode unpack = unpackValueWithErrorCode(j, key, type); 1428099c517SEd Tanous if (unpack == UnpackErrorCode::success) 1438099c517SEd Tanous { 1448099c517SEd Tanous v = std::move(type); 1458099c517SEd Tanous return unpack; 1468099c517SEd Tanous } 1478099c517SEd Tanous 1488099c517SEd Tanous return unpackValueVariant<Index + 1, Args...>(j, key, v); 1498099c517SEd Tanous } 1508099c517SEd Tanous return UnpackErrorCode::invalidType; 1518099c517SEd Tanous } 1528099c517SEd Tanous 1538099c517SEd Tanous template <typename Type> 1548099c517SEd Tanous UnpackErrorCode unpackValueWithErrorCode(nlohmann::json& jsonValue, 155ea2e6eecSWilly Tu std::string_view key, Type& value) 156771cfa0fSJason M. Bills { 157471a5eb8SAppaRao Puli UnpackErrorCode ret = UnpackErrorCode::success; 15841352c24SSantosh Puranik 159a6acbb31SJames Feist if constexpr (std::is_floating_point_v<Type>) 160771cfa0fSJason M. Bills { 161a6acbb31SJames Feist double helper = 0; 162a6acbb31SJames Feist double* jsonPtr = jsonValue.get_ptr<double*>(); 163771cfa0fSJason M. Bills 164771cfa0fSJason M. Bills if (jsonPtr == nullptr) 165771cfa0fSJason M. Bills { 166a6acbb31SJames Feist int64_t* intPtr = jsonValue.get_ptr<int64_t*>(); 167a6acbb31SJames Feist if (intPtr != nullptr) 168771cfa0fSJason M. Bills { 169a6acbb31SJames Feist helper = static_cast<double>(*intPtr); 170a6acbb31SJames Feist jsonPtr = &helper; 171771cfa0fSJason M. Bills } 172a6acbb31SJames Feist } 1735eb2bef2SAppaRao Puli if (jsonPtr == nullptr) 1745eb2bef2SAppaRao Puli { 175471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 1765eb2bef2SAppaRao Puli } 177cb13a392SEd Tanous if (!checkRange<Type>(*jsonPtr, key)) 178771cfa0fSJason M. Bills { 179471a5eb8SAppaRao Puli return UnpackErrorCode::outOfRange; 180771cfa0fSJason M. Bills } 181771cfa0fSJason M. Bills value = static_cast<Type>(*jsonPtr); 182771cfa0fSJason M. Bills } 183a6acbb31SJames Feist 184a6acbb31SJames Feist else if constexpr (std::is_signed_v<Type>) 185a6acbb31SJames Feist { 186a6acbb31SJames Feist int64_t* jsonPtr = jsonValue.get_ptr<int64_t*>(); 187271584abSEd Tanous if (jsonPtr == nullptr) 188271584abSEd Tanous { 189*8d9cf72dSEd Tanous // Value wasn't int, check uint 190*8d9cf72dSEd Tanous uint64_t* uJsonPtr = jsonValue.get_ptr<uint64_t*>(); 191*8d9cf72dSEd Tanous if (uJsonPtr == nullptr) 192*8d9cf72dSEd Tanous { 193471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 194271584abSEd Tanous } 195*8d9cf72dSEd Tanous if (!checkRange<Type>(*uJsonPtr, key)) 196*8d9cf72dSEd Tanous { 197*8d9cf72dSEd Tanous return UnpackErrorCode::outOfRange; 198*8d9cf72dSEd Tanous } 199*8d9cf72dSEd Tanous value = static_cast<Type>(*uJsonPtr); 200*8d9cf72dSEd Tanous } 201*8d9cf72dSEd Tanous else 202*8d9cf72dSEd Tanous { 203cb13a392SEd Tanous if (!checkRange<Type>(*jsonPtr, key)) 204a6acbb31SJames Feist { 205471a5eb8SAppaRao Puli return UnpackErrorCode::outOfRange; 206a6acbb31SJames Feist } 207a6acbb31SJames Feist value = static_cast<Type>(*jsonPtr); 208a6acbb31SJames Feist } 209*8d9cf72dSEd Tanous } 210a6acbb31SJames Feist 211bd79bce8SPatrick Williams else if constexpr ((std::is_unsigned_v<Type>) && 212bd79bce8SPatrick Williams (!std::is_same_v<bool, Type>)) 213a6acbb31SJames Feist { 214a6acbb31SJames Feist uint64_t* jsonPtr = jsonValue.get_ptr<uint64_t*>(); 215271584abSEd Tanous if (jsonPtr == nullptr) 216271584abSEd Tanous { 217*8d9cf72dSEd Tanous int64_t* ijsonPtr = jsonValue.get_ptr<int64_t*>(); 218*8d9cf72dSEd Tanous if (ijsonPtr == nullptr) 219*8d9cf72dSEd Tanous { 220471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 221271584abSEd Tanous } 222*8d9cf72dSEd Tanous if (!checkRange<Type>(*ijsonPtr, key)) 223*8d9cf72dSEd Tanous { 224*8d9cf72dSEd Tanous return UnpackErrorCode::outOfRange; 225*8d9cf72dSEd Tanous } 226*8d9cf72dSEd Tanous value = static_cast<Type>(*ijsonPtr); 227*8d9cf72dSEd Tanous } 228*8d9cf72dSEd Tanous else 229*8d9cf72dSEd Tanous { 230cb13a392SEd Tanous if (!checkRange<Type>(*jsonPtr, key)) 231a6acbb31SJames Feist { 232471a5eb8SAppaRao Puli return UnpackErrorCode::outOfRange; 233a6acbb31SJames Feist } 234a6acbb31SJames Feist value = static_cast<Type>(*jsonPtr); 235a6acbb31SJames Feist } 236*8d9cf72dSEd Tanous } 237a6acbb31SJames Feist 2380627a2c7SEd Tanous else if constexpr (std::is_same_v<nlohmann::json, Type>) 2390627a2c7SEd Tanous { 2400627a2c7SEd Tanous value = std::move(jsonValue); 2410627a2c7SEd Tanous } 2428099c517SEd Tanous else if constexpr (std::is_same_v<std::nullptr_t, Type>) 2438099c517SEd Tanous { 2448099c517SEd Tanous if (!jsonValue.is_null()) 2458099c517SEd Tanous { 2468099c517SEd Tanous return UnpackErrorCode::invalidType; 2478099c517SEd Tanous } 2488099c517SEd Tanous } 249ed4de7a8SEd Tanous else if constexpr (IsVector<Type>::value) 250ed4de7a8SEd Tanous { 251ed4de7a8SEd Tanous nlohmann::json::object_t* obj = 252ed4de7a8SEd Tanous jsonValue.get_ptr<nlohmann::json::object_t*>(); 253ed4de7a8SEd Tanous if (obj == nullptr) 254ed4de7a8SEd Tanous { 255ed4de7a8SEd Tanous return UnpackErrorCode::invalidType; 256ed4de7a8SEd Tanous } 257ed4de7a8SEd Tanous 258ed4de7a8SEd Tanous for (const auto& val : *obj) 259ed4de7a8SEd Tanous { 260ed4de7a8SEd Tanous value.emplace_back(); 261ed4de7a8SEd Tanous ret = unpackValueWithErrorCode<typename Type::value_type>( 262ed4de7a8SEd Tanous val, key, value.back()) && 263ed4de7a8SEd Tanous ret; 264ed4de7a8SEd Tanous } 265ed4de7a8SEd Tanous } 266471a5eb8SAppaRao Puli else 267471a5eb8SAppaRao Puli { 268471a5eb8SAppaRao Puli using JsonType = std::add_const_t<std::add_pointer_t<Type>>; 269471a5eb8SAppaRao Puli JsonType jsonPtr = jsonValue.get_ptr<JsonType>(); 270471a5eb8SAppaRao Puli if (jsonPtr == nullptr) 271471a5eb8SAppaRao Puli { 27262598e31SEd Tanous BMCWEB_LOG_DEBUG("Value for key {} was incorrect type: {}", key, 27362598e31SEd Tanous jsonValue.type_name()); 274471a5eb8SAppaRao Puli return UnpackErrorCode::invalidType; 275471a5eb8SAppaRao Puli } 276471a5eb8SAppaRao Puli value = std::move(*jsonPtr); 277471a5eb8SAppaRao Puli } 278471a5eb8SAppaRao Puli return ret; 279471a5eb8SAppaRao Puli } 280471a5eb8SAppaRao Puli 281471a5eb8SAppaRao Puli template <typename Type> 282ea2e6eecSWilly Tu bool unpackValue(nlohmann::json& jsonValue, std::string_view key, 283471a5eb8SAppaRao Puli crow::Response& res, Type& value) 284471a5eb8SAppaRao Puli { 285471a5eb8SAppaRao Puli bool ret = true; 286471a5eb8SAppaRao Puli 2872c70f800SEd Tanous if constexpr (IsOptional<Type>::value) 288471a5eb8SAppaRao Puli { 289471a5eb8SAppaRao Puli value.emplace(); 290471a5eb8SAppaRao Puli ret = unpackValue<typename Type::value_type>(jsonValue, key, res, 291471a5eb8SAppaRao Puli *value) && 292471a5eb8SAppaRao Puli ret; 293471a5eb8SAppaRao Puli } 2942c70f800SEd Tanous else if constexpr (IsStdArray<Type>::value) 295318226c2SJames Feist { 2960bdda665SEd Tanous nlohmann::json::array_t* arr = 2970bdda665SEd Tanous jsonValue.get_ptr<nlohmann::json::array_t*>(); 2980bdda665SEd Tanous if (arr == nullptr) 299318226c2SJames Feist { 3008b078385Srohitpai messages::propertyValueTypeError(res, jsonValue, key); 30141352c24SSantosh Puranik return false; 302318226c2SJames Feist } 303318226c2SJames Feist if (jsonValue.size() != value.size()) 304318226c2SJames Feist { 3058b078385Srohitpai messages::propertyValueTypeError(res, jsonValue, key); 30641352c24SSantosh Puranik return false; 307318226c2SJames Feist } 308318226c2SJames Feist size_t index = 0; 3090bdda665SEd Tanous for (auto& val : *arr) 310318226c2SJames Feist { 3110bdda665SEd Tanous ret = unpackValue<typename Type::value_type>(val, key, res, 31241352c24SSantosh Puranik value[index++]) && 31341352c24SSantosh Puranik ret; 314318226c2SJames Feist } 315318226c2SJames Feist } 3162c70f800SEd Tanous else if constexpr (IsVector<Type>::value) 317b1556427SEd Tanous { 3180bdda665SEd Tanous nlohmann::json::array_t* arr = 3190bdda665SEd Tanous jsonValue.get_ptr<nlohmann::json::array_t*>(); 3200bdda665SEd Tanous if (arr == nullptr) 321b1556427SEd Tanous { 3228b078385Srohitpai messages::propertyValueTypeError(res, jsonValue, key); 32341352c24SSantosh Puranik return false; 324b1556427SEd Tanous } 325b1556427SEd Tanous 3260bdda665SEd Tanous for (auto& val : *arr) 327b1556427SEd Tanous { 328b1556427SEd Tanous value.emplace_back(); 3290bdda665SEd Tanous ret = unpackValue<typename Type::value_type>(val, key, res, 33041352c24SSantosh Puranik value.back()) && 33141352c24SSantosh Puranik ret; 332b1556427SEd Tanous } 333b1556427SEd Tanous } 3348099c517SEd Tanous else if constexpr (IsVariant<Type>::value) 3358099c517SEd Tanous { 3368099c517SEd Tanous UnpackErrorCode ec = unpackValueVariant(jsonValue, key, value); 3378099c517SEd Tanous if (ec != UnpackErrorCode::success) 3388099c517SEd Tanous { 3398099c517SEd Tanous if (ec == UnpackErrorCode::invalidType) 3408099c517SEd Tanous { 3418099c517SEd Tanous messages::propertyValueTypeError(res, jsonValue, key); 3428099c517SEd Tanous } 3438099c517SEd Tanous else if (ec == UnpackErrorCode::outOfRange) 3448099c517SEd Tanous { 345340d74c8SMyung Bae messages::propertyValueOutOfRange(res, jsonValue, key); 3468099c517SEd Tanous } 3478099c517SEd Tanous return false; 3488099c517SEd Tanous } 3498099c517SEd Tanous } 350771cfa0fSJason M. Bills else 351771cfa0fSJason M. Bills { 352471a5eb8SAppaRao Puli UnpackErrorCode ec = unpackValueWithErrorCode(jsonValue, key, value); 353471a5eb8SAppaRao Puli if (ec != UnpackErrorCode::success) 354771cfa0fSJason M. Bills { 355471a5eb8SAppaRao Puli if (ec == UnpackErrorCode::invalidType) 356471a5eb8SAppaRao Puli { 3572e8c4bdaSEd Tanous messages::propertyValueTypeError(res, jsonValue, key); 358471a5eb8SAppaRao Puli } 359471a5eb8SAppaRao Puli else if (ec == UnpackErrorCode::outOfRange) 360471a5eb8SAppaRao Puli { 361340d74c8SMyung Bae messages::propertyValueOutOfRange(res, jsonValue, key); 362471a5eb8SAppaRao Puli } 36341352c24SSantosh Puranik return false; 364771cfa0fSJason M. Bills } 365771cfa0fSJason M. Bills } 366471a5eb8SAppaRao Puli 367471a5eb8SAppaRao Puli return ret; 368471a5eb8SAppaRao Puli } 369471a5eb8SAppaRao Puli 370471a5eb8SAppaRao Puli template <typename Type> 371ea2e6eecSWilly Tu bool unpackValue(nlohmann::json& jsonValue, std::string_view key, Type& value) 372471a5eb8SAppaRao Puli { 373471a5eb8SAppaRao Puli bool ret = true; 3742c70f800SEd Tanous if constexpr (IsOptional<Type>::value) 375471a5eb8SAppaRao Puli { 376471a5eb8SAppaRao Puli value.emplace(); 377471a5eb8SAppaRao Puli ret = unpackValue<typename Type::value_type>(jsonValue, key, *value) && 378471a5eb8SAppaRao Puli ret; 379471a5eb8SAppaRao Puli } 3802c70f800SEd Tanous else if constexpr (IsStdArray<Type>::value) 381471a5eb8SAppaRao Puli { 3820bdda665SEd Tanous nlohmann::json::array_t* arr = 3830bdda665SEd Tanous jsonValue.get_ptr<nlohmann::json::array_t*>(); 3840bdda665SEd Tanous if (arr == nullptr) 385471a5eb8SAppaRao Puli { 386471a5eb8SAppaRao Puli return false; 387471a5eb8SAppaRao Puli } 388471a5eb8SAppaRao Puli if (jsonValue.size() != value.size()) 389471a5eb8SAppaRao Puli { 390471a5eb8SAppaRao Puli return false; 391471a5eb8SAppaRao Puli } 392471a5eb8SAppaRao Puli size_t index = 0; 3930bdda665SEd Tanous for (const auto& val : *arr) 394471a5eb8SAppaRao Puli { 3950bdda665SEd Tanous ret = unpackValue<typename Type::value_type>(val, key, 396471a5eb8SAppaRao Puli value[index++]) && 397471a5eb8SAppaRao Puli ret; 398471a5eb8SAppaRao Puli } 399471a5eb8SAppaRao Puli } 4002c70f800SEd Tanous else if constexpr (IsVector<Type>::value) 401471a5eb8SAppaRao Puli { 4020bdda665SEd Tanous nlohmann::json::array_t* arr = 4030bdda665SEd Tanous jsonValue.get_ptr<nlohmann::json::array_t*>(); 4040bdda665SEd Tanous if (arr == nullptr) 405471a5eb8SAppaRao Puli { 406471a5eb8SAppaRao Puli return false; 407471a5eb8SAppaRao Puli } 408471a5eb8SAppaRao Puli 4090bdda665SEd Tanous for (const auto& val : *arr) 410471a5eb8SAppaRao Puli { 411471a5eb8SAppaRao Puli value.emplace_back(); 4120bdda665SEd Tanous ret = unpackValue<typename Type::value_type>(val, key, 413471a5eb8SAppaRao Puli value.back()) && 414471a5eb8SAppaRao Puli ret; 415471a5eb8SAppaRao Puli } 416471a5eb8SAppaRao Puli } 417471a5eb8SAppaRao Puli else 418471a5eb8SAppaRao Puli { 419471a5eb8SAppaRao Puli UnpackErrorCode ec = unpackValueWithErrorCode(jsonValue, key, value); 420471a5eb8SAppaRao Puli if (ec != UnpackErrorCode::success) 421471a5eb8SAppaRao Puli { 422471a5eb8SAppaRao Puli return false; 423471a5eb8SAppaRao Puli } 424471a5eb8SAppaRao Puli } 425471a5eb8SAppaRao Puli 42641352c24SSantosh Puranik return ret; 427771cfa0fSJason M. Bills } 4289712f8acSEd Tanous } // namespace details 4299712f8acSEd Tanous 430ea2e6eecSWilly Tu // clang-format off 431ea2e6eecSWilly Tu using UnpackVariant = std::variant< 432ea2e6eecSWilly Tu uint8_t*, 433ea2e6eecSWilly Tu uint16_t*, 434ea2e6eecSWilly Tu int16_t*, 435ea2e6eecSWilly Tu uint32_t*, 436ea2e6eecSWilly Tu int32_t*, 437ea2e6eecSWilly Tu uint64_t*, 438ea2e6eecSWilly Tu int64_t*, 439ea2e6eecSWilly Tu bool*, 440ea2e6eecSWilly Tu double*, 441ea2e6eecSWilly Tu std::string*, 442b6164cbeSEd Tanous nlohmann::json::object_t*, 4438099c517SEd Tanous std::variant<std::string, std::nullptr_t>*, 4448099c517SEd Tanous std::variant<uint8_t, std::nullptr_t>*, 4458099c517SEd Tanous std::variant<int16_t, std::nullptr_t>*, 4468099c517SEd Tanous std::variant<uint16_t, std::nullptr_t>*, 4478099c517SEd Tanous std::variant<int32_t, std::nullptr_t>*, 4488099c517SEd Tanous std::variant<uint32_t, std::nullptr_t>*, 4498099c517SEd Tanous std::variant<int64_t, std::nullptr_t>*, 4508099c517SEd Tanous std::variant<uint64_t, std::nullptr_t>*, 4518099c517SEd Tanous std::variant<double, std::nullptr_t>*, 4528099c517SEd Tanous std::variant<bool, std::nullptr_t>*, 453ea2e6eecSWilly Tu std::vector<uint8_t>*, 454ea2e6eecSWilly Tu std::vector<uint16_t>*, 455ea2e6eecSWilly Tu std::vector<int16_t>*, 456ea2e6eecSWilly Tu std::vector<uint32_t>*, 457ea2e6eecSWilly Tu std::vector<int32_t>*, 458ea2e6eecSWilly Tu std::vector<uint64_t>*, 459ea2e6eecSWilly Tu std::vector<int64_t>*, 460ea2e6eecSWilly Tu //std::vector<bool>*, 461ea2e6eecSWilly Tu std::vector<double>*, 462ea2e6eecSWilly Tu std::vector<std::string>*, 463b6164cbeSEd Tanous std::vector<nlohmann::json::object_t>*, 464ea2e6eecSWilly Tu std::optional<uint8_t>*, 465ea2e6eecSWilly Tu std::optional<uint16_t>*, 466ea2e6eecSWilly Tu std::optional<int16_t>*, 467ea2e6eecSWilly Tu std::optional<uint32_t>*, 468ea2e6eecSWilly Tu std::optional<int32_t>*, 469ea2e6eecSWilly Tu std::optional<uint64_t>*, 470ea2e6eecSWilly Tu std::optional<int64_t>*, 471ea2e6eecSWilly Tu std::optional<bool>*, 472ea2e6eecSWilly Tu std::optional<double>*, 473ea2e6eecSWilly Tu std::optional<std::string>*, 474b6164cbeSEd Tanous std::optional<nlohmann::json::object_t>*, 475ea2e6eecSWilly Tu std::optional<std::vector<uint8_t>>*, 476ea2e6eecSWilly Tu std::optional<std::vector<uint16_t>>*, 477ea2e6eecSWilly Tu std::optional<std::vector<int16_t>>*, 478ea2e6eecSWilly Tu std::optional<std::vector<uint32_t>>*, 479ea2e6eecSWilly Tu std::optional<std::vector<int32_t>>*, 480ea2e6eecSWilly Tu std::optional<std::vector<uint64_t>>*, 481ea2e6eecSWilly Tu std::optional<std::vector<int64_t>>*, 482ea2e6eecSWilly Tu //std::optional<std::vector<bool>>*, 483ea2e6eecSWilly Tu std::optional<std::vector<double>>*, 484ea2e6eecSWilly Tu std::optional<std::vector<std::string>>*, 4858099c517SEd Tanous std::optional<std::vector<nlohmann::json::object_t>>*, 4868099c517SEd Tanous std::optional<std::variant<std::string, std::nullptr_t>>*, 4878099c517SEd Tanous std::optional<std::variant<uint8_t, std::nullptr_t>>*, 4888099c517SEd Tanous std::optional<std::variant<int16_t, std::nullptr_t>>*, 4898099c517SEd Tanous std::optional<std::variant<uint16_t, std::nullptr_t>>*, 4908099c517SEd Tanous std::optional<std::variant<int32_t, std::nullptr_t>>*, 4918099c517SEd Tanous std::optional<std::variant<uint32_t, std::nullptr_t>>*, 4928099c517SEd Tanous std::optional<std::variant<int64_t, std::nullptr_t>>*, 4938099c517SEd Tanous std::optional<std::variant<uint64_t, std::nullptr_t>>*, 4948099c517SEd Tanous std::optional<std::variant<double, std::nullptr_t>>*, 4958099c517SEd Tanous std::optional<std::variant<bool, std::nullptr_t>>*, 4968099c517SEd Tanous std::optional<std::vector<std::variant<nlohmann::json::object_t, std::nullptr_t>>>*, 497ed4de7a8SEd Tanous std::optional<std::vector<std::variant<std::string, nlohmann::json::object_t, std::nullptr_t>>>*, 498ed4de7a8SEd Tanous 499ed4de7a8SEd Tanous // Note, these types are kept for historical completeness, but should not be used, 500ed4de7a8SEd Tanous // As they do not provide object type safety. Instead, rely on nlohmann::json::object_t 501ed4de7a8SEd Tanous // Will be removed Q2 2025 502ed4de7a8SEd Tanous nlohmann::json*, 503ed4de7a8SEd Tanous std::optional<std::vector<nlohmann::json>>*, 504ed4de7a8SEd Tanous std::vector<nlohmann::json>*, 505ed4de7a8SEd Tanous std::optional<nlohmann::json>* 506ea2e6eecSWilly Tu >; 507ea2e6eecSWilly Tu // clang-format on 508ea2e6eecSWilly Tu 509ea2e6eecSWilly Tu struct PerUnpack 510ea2e6eecSWilly Tu { 511ea2e6eecSWilly Tu std::string_view key; 512ea2e6eecSWilly Tu UnpackVariant value; 513ea2e6eecSWilly Tu bool complete = false; 514ea2e6eecSWilly Tu }; 515ea2e6eecSWilly Tu 516b6164cbeSEd Tanous inline bool readJsonHelperObject(nlohmann::json::object_t& obj, 517b6164cbeSEd Tanous crow::Response& res, 518ea2e6eecSWilly Tu std::span<PerUnpack> toUnpack) 5199712f8acSEd Tanous { 52041352c24SSantosh Puranik bool result = true; 521b6164cbeSEd Tanous for (auto& item : obj) 5229712f8acSEd Tanous { 523ea2e6eecSWilly Tu size_t unpackIndex = 0; 524ea2e6eecSWilly Tu for (; unpackIndex < toUnpack.size(); unpackIndex++) 525ea2e6eecSWilly Tu { 526ea2e6eecSWilly Tu PerUnpack& unpackSpec = toUnpack[unpackIndex]; 527ea2e6eecSWilly Tu std::string_view key = unpackSpec.key; 528ea2e6eecSWilly Tu size_t keysplitIndex = key.find('/'); 529ea2e6eecSWilly Tu std::string_view leftover; 530ea2e6eecSWilly Tu if (keysplitIndex != std::string_view::npos) 531ea2e6eecSWilly Tu { 532ea2e6eecSWilly Tu leftover = key.substr(keysplitIndex + 1); 533ea2e6eecSWilly Tu key = key.substr(0, keysplitIndex); 534ea2e6eecSWilly Tu } 535ea2e6eecSWilly Tu 536d91415c4SEd Tanous if (key != item.first || unpackSpec.complete) 537ea2e6eecSWilly Tu { 538ea2e6eecSWilly Tu continue; 539ea2e6eecSWilly Tu } 540ea2e6eecSWilly Tu 541ea2e6eecSWilly Tu // Sublevel key 542ea2e6eecSWilly Tu if (!leftover.empty()) 543ea2e6eecSWilly Tu { 544ea2e6eecSWilly Tu // Include the slash in the key so we can compare later 545ea2e6eecSWilly Tu key = unpackSpec.key.substr(0, keysplitIndex + 1); 5469a560319SEd Tanous nlohmann::json::object_t j; 5479a560319SEd Tanous result = details::unpackValue<nlohmann::json::object_t>( 5489a560319SEd Tanous item.second, key, res, j) && 54941352c24SSantosh Puranik result; 55055f79e6fSEd Tanous if (!result) 551ea2e6eecSWilly Tu { 552ea2e6eecSWilly Tu return result; 5539712f8acSEd Tanous } 5549712f8acSEd Tanous 555ea2e6eecSWilly Tu std::vector<PerUnpack> nextLevel; 556ea2e6eecSWilly Tu for (PerUnpack& p : toUnpack) 557ea2e6eecSWilly Tu { 558ea2e6eecSWilly Tu if (!p.key.starts_with(key)) 559ea2e6eecSWilly Tu { 560ea2e6eecSWilly Tu continue; 561ea2e6eecSWilly Tu } 562ea2e6eecSWilly Tu std::string_view thisLeftover = p.key.substr(key.size()); 563ea2e6eecSWilly Tu nextLevel.push_back({thisLeftover, p.value, false}); 564ea2e6eecSWilly Tu p.complete = true; 5659712f8acSEd Tanous } 56677dd8813SKowalski, Kamil 5679a560319SEd Tanous result = readJsonHelperObject(j, res, nextLevel) && result; 568ea2e6eecSWilly Tu break; 569ea2e6eecSWilly Tu } 570ea2e6eecSWilly Tu 571bd79bce8SPatrick Williams result = 572bd79bce8SPatrick Williams std::visit( 5735ea927bbSEd Tanous [&item, &unpackSpec, &res](auto& val) { 574ea2e6eecSWilly Tu using ContainedT = 575ea2e6eecSWilly Tu std::remove_pointer_t<std::decay_t<decltype(val)>>; 576ea2e6eecSWilly Tu return details::unpackValue<ContainedT>( 577d91415c4SEd Tanous item.second, unpackSpec.key, res, *val); 578ea2e6eecSWilly Tu }, 579ea2e6eecSWilly Tu unpackSpec.value) && 580ea2e6eecSWilly Tu result; 581ea2e6eecSWilly Tu 582ea2e6eecSWilly Tu unpackSpec.complete = true; 583ea2e6eecSWilly Tu break; 584ea2e6eecSWilly Tu } 585ea2e6eecSWilly Tu 586ea2e6eecSWilly Tu if (unpackIndex == toUnpack.size()) 587ea2e6eecSWilly Tu { 588d91415c4SEd Tanous messages::propertyUnknown(res, item.first); 589ea2e6eecSWilly Tu result = false; 590ea2e6eecSWilly Tu } 591ea2e6eecSWilly Tu } 592ea2e6eecSWilly Tu 593ea2e6eecSWilly Tu for (PerUnpack& perUnpack : toUnpack) 594ea2e6eecSWilly Tu { 59555f79e6fSEd Tanous if (!perUnpack.complete) 596ea2e6eecSWilly Tu { 597ea2e6eecSWilly Tu bool isOptional = std::visit( 5985ea927bbSEd Tanous [](auto& val) { 599ea2e6eecSWilly Tu using ContainedType = 600ea2e6eecSWilly Tu std::remove_pointer_t<std::decay_t<decltype(val)>>; 601ea2e6eecSWilly Tu return details::IsOptional<ContainedType>::value; 602ea2e6eecSWilly Tu }, 603ea2e6eecSWilly Tu perUnpack.value); 604ea2e6eecSWilly Tu if (isOptional) 605ea2e6eecSWilly Tu { 606ea2e6eecSWilly Tu continue; 607ea2e6eecSWilly Tu } 608ea2e6eecSWilly Tu messages::propertyMissing(res, perUnpack.key); 609ea2e6eecSWilly Tu result = false; 610ea2e6eecSWilly Tu } 611ea2e6eecSWilly Tu } 612ea2e6eecSWilly Tu return result; 613ea2e6eecSWilly Tu } 614ea2e6eecSWilly Tu 61589492a15SPatrick Williams inline void packVariant(std::span<PerUnpack> /*toPack*/) {} 616ea2e6eecSWilly Tu 617ea2e6eecSWilly Tu template <typename FirstType, typename... UnpackTypes> 618ea2e6eecSWilly Tu void packVariant(std::span<PerUnpack> toPack, std::string_view key, 6195ea927bbSEd Tanous FirstType&& first, UnpackTypes&&... in) 620ea2e6eecSWilly Tu { 621ea2e6eecSWilly Tu if (toPack.empty()) 622ea2e6eecSWilly Tu { 623ea2e6eecSWilly Tu return; 624ea2e6eecSWilly Tu } 625ea2e6eecSWilly Tu toPack[0].key = key; 626ea2e6eecSWilly Tu toPack[0].value = &first; 627ea2e6eecSWilly Tu // NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay) 628ea2e6eecSWilly Tu packVariant(toPack.subspan(1), std::forward<UnpackTypes&&>(in)...); 629ea2e6eecSWilly Tu } 630ea2e6eecSWilly Tu 631ea2e6eecSWilly Tu template <typename FirstType, typename... UnpackTypes> 632b6164cbeSEd Tanous bool readJsonObject(nlohmann::json::object_t& jsonRequest, crow::Response& res, 633b6164cbeSEd Tanous std::string_view key, FirstType&& first, 634b6164cbeSEd Tanous UnpackTypes&&... in) 635ea2e6eecSWilly Tu { 636ea2e6eecSWilly Tu const std::size_t n = sizeof...(UnpackTypes) + 2; 637ea2e6eecSWilly Tu std::array<PerUnpack, n / 2> toUnpack2; 6385ea927bbSEd Tanous packVariant(toUnpack2, key, std::forward<FirstType>(first), 6395ea927bbSEd Tanous std::forward<UnpackTypes&&>(in)...); 640b6164cbeSEd Tanous return readJsonHelperObject(jsonRequest, res, toUnpack2); 641ea2e6eecSWilly Tu } 642ea2e6eecSWilly Tu 643b6164cbeSEd Tanous template <typename FirstType, typename... UnpackTypes> 644b6164cbeSEd Tanous bool readJson(nlohmann::json& jsonRequest, crow::Response& res, 645b6164cbeSEd Tanous std::string_view key, FirstType&& first, UnpackTypes&&... in) 646b6164cbeSEd Tanous { 647b6164cbeSEd Tanous nlohmann::json::object_t* obj = 648b6164cbeSEd Tanous jsonRequest.get_ptr<nlohmann::json::object_t*>(); 649b6164cbeSEd Tanous if (obj == nullptr) 650b6164cbeSEd Tanous { 651b6164cbeSEd Tanous BMCWEB_LOG_DEBUG("Json value is not an object"); 652b6164cbeSEd Tanous messages::unrecognizedRequestBody(res); 653b6164cbeSEd Tanous return false; 654b6164cbeSEd Tanous } 6555be2b14aSEd Tanous return readJsonObject(*obj, res, key, std::forward<FirstType>(first), 6565be2b14aSEd Tanous std::forward<UnpackTypes&&>(in)...); 657b6164cbeSEd Tanous } 658b6164cbeSEd Tanous 659504af5a0SPatrick Williams inline std::optional<nlohmann::json::object_t> readJsonPatchHelper( 660504af5a0SPatrick Williams const crow::Request& req, crow::Response& res) 6610627a2c7SEd Tanous { 6620627a2c7SEd Tanous nlohmann::json jsonRequest; 6630627a2c7SEd Tanous if (!json_util::processJsonFromRequest(res, req, jsonRequest)) 6640627a2c7SEd Tanous { 66562598e31SEd Tanous BMCWEB_LOG_DEBUG("Json value not readable"); 666ea2e6eecSWilly Tu return std::nullopt; 6670627a2c7SEd Tanous } 668357bb8f8SEd Tanous nlohmann::json::object_t* object = 669357bb8f8SEd Tanous jsonRequest.get_ptr<nlohmann::json::object_t*>(); 670357bb8f8SEd Tanous if (object == nullptr || object->empty()) 67115ed6780SWilly Tu { 67262598e31SEd Tanous BMCWEB_LOG_DEBUG("Json value is empty"); 67315ed6780SWilly Tu messages::emptyJSON(res); 674ea2e6eecSWilly Tu return std::nullopt; 675ea2e6eecSWilly Tu } 676357bb8f8SEd Tanous std::erase_if(*object, 677357bb8f8SEd Tanous [](const std::pair<std::string, nlohmann::json>& item) { 678357bb8f8SEd Tanous return item.first.starts_with("@odata."); 679357bb8f8SEd Tanous }); 680357bb8f8SEd Tanous if (object->empty()) 681357bb8f8SEd Tanous { 682357bb8f8SEd Tanous // If the update request only contains OData annotations, the service 683357bb8f8SEd Tanous // should return the HTTP 400 Bad Request status code with the 684357bb8f8SEd Tanous // NoOperation message from the Base Message Registry, ... 685357bb8f8SEd Tanous messages::noOperation(res); 686357bb8f8SEd Tanous return std::nullopt; 687357bb8f8SEd Tanous } 688357bb8f8SEd Tanous 689b6164cbeSEd Tanous return {std::move(*object)}; 690ea2e6eecSWilly Tu } 691ea2e6eecSWilly Tu 6923c9e6b1cSChandramohan Harkude inline const nlohmann::json* findNestedKey(std::string_view key, 6933c9e6b1cSChandramohan Harkude const nlohmann::json& value) 6943c9e6b1cSChandramohan Harkude { 6953c9e6b1cSChandramohan Harkude size_t keysplitIndex = key.find('/'); 6963c9e6b1cSChandramohan Harkude std::string_view leftover; 6973c9e6b1cSChandramohan Harkude nlohmann::json::const_iterator it; 6983c9e6b1cSChandramohan Harkude if (keysplitIndex != std::string_view::npos) 6993c9e6b1cSChandramohan Harkude { 7003c9e6b1cSChandramohan Harkude const nlohmann::json::object_t* obj = 7013c9e6b1cSChandramohan Harkude value.get_ptr<const nlohmann::json::object_t*>(); 7023c9e6b1cSChandramohan Harkude if (obj == nullptr || obj->empty()) 7033c9e6b1cSChandramohan Harkude { 7043c9e6b1cSChandramohan Harkude BMCWEB_LOG_ERROR("Requested key wasn't an object"); 7053c9e6b1cSChandramohan Harkude return nullptr; 7063c9e6b1cSChandramohan Harkude } 7073c9e6b1cSChandramohan Harkude 7083c9e6b1cSChandramohan Harkude leftover = key.substr(keysplitIndex + 1); 7093c9e6b1cSChandramohan Harkude std::string_view keypart = key.substr(0, keysplitIndex); 7103c9e6b1cSChandramohan Harkude it = value.find(keypart); 7113c9e6b1cSChandramohan Harkude if (it == value.end()) 7123c9e6b1cSChandramohan Harkude { 7133c9e6b1cSChandramohan Harkude // Entry didn't have key 7143c9e6b1cSChandramohan Harkude return nullptr; 7153c9e6b1cSChandramohan Harkude } 7163c9e6b1cSChandramohan Harkude return findNestedKey(leftover, it.value()); 7173c9e6b1cSChandramohan Harkude } 7183c9e6b1cSChandramohan Harkude 7193c9e6b1cSChandramohan Harkude it = value.find(key); 7203c9e6b1cSChandramohan Harkude if (it == value.end()) 7213c9e6b1cSChandramohan Harkude { 7223c9e6b1cSChandramohan Harkude return nullptr; 7233c9e6b1cSChandramohan Harkude } 7243c9e6b1cSChandramohan Harkude return &*it; 7253c9e6b1cSChandramohan Harkude } 7263c9e6b1cSChandramohan Harkude 727ea2e6eecSWilly Tu template <typename... UnpackTypes> 728ea2e6eecSWilly Tu bool readJsonPatch(const crow::Request& req, crow::Response& res, 729ea2e6eecSWilly Tu std::string_view key, UnpackTypes&&... in) 730ea2e6eecSWilly Tu { 73176b038f2SEd Tanous std::optional<nlohmann::json::object_t> jsonRequest = 73276b038f2SEd Tanous readJsonPatchHelper(req, res); 733e01d0c36SEd Tanous if (!jsonRequest) 734ea2e6eecSWilly Tu { 73515ed6780SWilly Tu return false; 73615ed6780SWilly Tu } 73776b038f2SEd Tanous if (jsonRequest->empty()) 738b6164cbeSEd Tanous { 739b6164cbeSEd Tanous messages::emptyJSON(res); 740b6164cbeSEd Tanous return false; 741b6164cbeSEd Tanous } 74215ed6780SWilly Tu 74376b038f2SEd Tanous return readJsonObject(*jsonRequest, res, key, 744b6164cbeSEd Tanous std::forward<UnpackTypes&&>(in)...); 74515ed6780SWilly Tu } 74615ed6780SWilly Tu 747c1a75ebcSrohitpai inline std::optional<nlohmann::json::json_pointer> 748c1a75ebcSrohitpai createJsonPointerFromFragment(std::string_view input) 749c1a75ebcSrohitpai { 750c1a75ebcSrohitpai auto hashPos = input.find('#'); 751c1a75ebcSrohitpai if (hashPos == std::string_view::npos || hashPos + 1 >= input.size()) 752c1a75ebcSrohitpai { 753c1a75ebcSrohitpai BMCWEB_LOG_ERROR( 754c1a75ebcSrohitpai "createJsonPointerFromFragment() No fragment found after #"); 755c1a75ebcSrohitpai return std::nullopt; 756c1a75ebcSrohitpai } 757c1a75ebcSrohitpai 758c1a75ebcSrohitpai std::string_view fragment = input.substr(hashPos + 1); 759c1a75ebcSrohitpai return nlohmann::json::json_pointer(std::string(fragment)); 760c1a75ebcSrohitpai } 761c1a75ebcSrohitpai 76215ed6780SWilly Tu template <typename... UnpackTypes> 76315ed6780SWilly Tu bool readJsonAction(const crow::Request& req, crow::Response& res, 764ea2e6eecSWilly Tu const char* key, UnpackTypes&&... in) 76515ed6780SWilly Tu { 76615ed6780SWilly Tu nlohmann::json jsonRequest; 76715ed6780SWilly Tu if (!json_util::processJsonFromRequest(res, req, jsonRequest)) 76815ed6780SWilly Tu { 76962598e31SEd Tanous BMCWEB_LOG_DEBUG("Json value not readable"); 77015ed6780SWilly Tu return false; 77115ed6780SWilly Tu } 772b6164cbeSEd Tanous nlohmann::json::object_t* object = 773b6164cbeSEd Tanous jsonRequest.get_ptr<nlohmann::json::object_t*>(); 774b6164cbeSEd Tanous if (object == nullptr) 775b6164cbeSEd Tanous { 776b6164cbeSEd Tanous BMCWEB_LOG_DEBUG("Json value is empty"); 777b6164cbeSEd Tanous messages::emptyJSON(res); 778b6164cbeSEd Tanous return false; 779b6164cbeSEd Tanous } 780b6164cbeSEd Tanous return readJsonObject(*object, res, key, 781b6164cbeSEd Tanous std::forward<UnpackTypes&&>(in)...); 7820627a2c7SEd Tanous } 783185444b1SNan Zhou 784185444b1SNan Zhou // Determines if two json objects are less, based on the presence of the 785185444b1SNan Zhou // @odata.id key 7864e196b9aSEd Tanous inline int objectKeyCmp(std::string_view key, const nlohmann::json& a, 7874e196b9aSEd Tanous const nlohmann::json& b) 788185444b1SNan Zhou { 789185444b1SNan Zhou using object_t = nlohmann::json::object_t; 790185444b1SNan Zhou const object_t* aObj = a.get_ptr<const object_t*>(); 791185444b1SNan Zhou const object_t* bObj = b.get_ptr<const object_t*>(); 792185444b1SNan Zhou 793185444b1SNan Zhou if (aObj == nullptr) 794185444b1SNan Zhou { 795185444b1SNan Zhou if (bObj == nullptr) 796185444b1SNan Zhou { 797185444b1SNan Zhou return 0; 798185444b1SNan Zhou } 799185444b1SNan Zhou return -1; 800185444b1SNan Zhou } 801185444b1SNan Zhou if (bObj == nullptr) 802185444b1SNan Zhou { 803185444b1SNan Zhou return 1; 804185444b1SNan Zhou } 8054e196b9aSEd Tanous object_t::const_iterator aIt = aObj->find(key); 8064e196b9aSEd Tanous object_t::const_iterator bIt = bObj->find(key); 8074e196b9aSEd Tanous // If either object doesn't have the key, they get "sorted" to the 8084e196b9aSEd Tanous // beginning. 809185444b1SNan Zhou if (aIt == aObj->end()) 810185444b1SNan Zhou { 811185444b1SNan Zhou if (bIt == bObj->end()) 812185444b1SNan Zhou { 813185444b1SNan Zhou return 0; 814185444b1SNan Zhou } 815185444b1SNan Zhou return -1; 816185444b1SNan Zhou } 817185444b1SNan Zhou if (bIt == bObj->end()) 818185444b1SNan Zhou { 819185444b1SNan Zhou return 1; 820185444b1SNan Zhou } 821185444b1SNan Zhou const nlohmann::json::string_t* nameA = 822185444b1SNan Zhou aIt->second.get_ptr<const std::string*>(); 823185444b1SNan Zhou const nlohmann::json::string_t* nameB = 824185444b1SNan Zhou bIt->second.get_ptr<const std::string*>(); 825185444b1SNan Zhou // If either object doesn't have a string as the key, they get "sorted" to 8264e196b9aSEd Tanous // the beginning. 827185444b1SNan Zhou if (nameA == nullptr) 828185444b1SNan Zhou { 829185444b1SNan Zhou if (nameB == nullptr) 830185444b1SNan Zhou { 831185444b1SNan Zhou return 0; 832185444b1SNan Zhou } 833185444b1SNan Zhou return -1; 834185444b1SNan Zhou } 835185444b1SNan Zhou if (nameB == nullptr) 836185444b1SNan Zhou { 837185444b1SNan Zhou return 1; 838185444b1SNan Zhou } 839e7bcf475SJayanth Othayoth if (key != "@odata.id") 840e7bcf475SJayanth Othayoth { 841e7bcf475SJayanth Othayoth return alphanumComp(*nameA, *nameB); 842e7bcf475SJayanth Othayoth } 843185444b1SNan Zhou 844e7bcf475SJayanth Othayoth boost::system::result<boost::urls::url_view> aUrl = 845e7bcf475SJayanth Othayoth boost::urls::parse_relative_ref(*nameA); 846e7bcf475SJayanth Othayoth boost::system::result<boost::urls::url_view> bUrl = 847e7bcf475SJayanth Othayoth boost::urls::parse_relative_ref(*nameB); 848e7bcf475SJayanth Othayoth if (!aUrl) 849185444b1SNan Zhou { 850e7bcf475SJayanth Othayoth if (!bUrl) 851185444b1SNan Zhou { 852185444b1SNan Zhou return 0; 853185444b1SNan Zhou } 854185444b1SNan Zhou return -1; 855185444b1SNan Zhou } 856e7bcf475SJayanth Othayoth if (!bUrl) 857e7bcf475SJayanth Othayoth { 858e7bcf475SJayanth Othayoth return 1; 859e7bcf475SJayanth Othayoth } 860e7bcf475SJayanth Othayoth 861e7bcf475SJayanth Othayoth auto segmentsAIt = aUrl->segments().begin(); 862e7bcf475SJayanth Othayoth auto segmentsBIt = bUrl->segments().begin(); 863e7bcf475SJayanth Othayoth 864e7bcf475SJayanth Othayoth while (true) 865e7bcf475SJayanth Othayoth { 866e7bcf475SJayanth Othayoth if (segmentsAIt == aUrl->segments().end()) 867e7bcf475SJayanth Othayoth { 868e7bcf475SJayanth Othayoth if (segmentsBIt == bUrl->segments().end()) 869e7bcf475SJayanth Othayoth { 870e7bcf475SJayanth Othayoth return 0; 871e7bcf475SJayanth Othayoth } 872e7bcf475SJayanth Othayoth return -1; 873e7bcf475SJayanth Othayoth } 874e7bcf475SJayanth Othayoth if (segmentsBIt == bUrl->segments().end()) 875185444b1SNan Zhou { 876185444b1SNan Zhou return 1; 877185444b1SNan Zhou } 878185444b1SNan Zhou int res = alphanumComp(*segmentsAIt, *segmentsBIt); 879185444b1SNan Zhou if (res != 0) 880185444b1SNan Zhou { 881185444b1SNan Zhou return res; 882185444b1SNan Zhou } 883185444b1SNan Zhou 884185444b1SNan Zhou segmentsAIt++; 885185444b1SNan Zhou segmentsBIt++; 886185444b1SNan Zhou } 887e7bcf475SJayanth Othayoth return 0; 888185444b1SNan Zhou }; 889185444b1SNan Zhou 8904e196b9aSEd Tanous // kept for backward compatibility 8914e196b9aSEd Tanous inline int odataObjectCmp(const nlohmann::json& left, 8924e196b9aSEd Tanous const nlohmann::json& right) 8934e196b9aSEd Tanous { 8944e196b9aSEd Tanous return objectKeyCmp("@odata.id", left, right); 8954e196b9aSEd Tanous } 8964e196b9aSEd Tanous 897185444b1SNan Zhou struct ODataObjectLess 898185444b1SNan Zhou { 8994e196b9aSEd Tanous std::string_view key; 9004e196b9aSEd Tanous 9014e196b9aSEd Tanous explicit ODataObjectLess(std::string_view keyIn) : key(keyIn) {} 9024e196b9aSEd Tanous 903185444b1SNan Zhou bool operator()(const nlohmann::json& left, 904185444b1SNan Zhou const nlohmann::json& right) const 905185444b1SNan Zhou { 9064e196b9aSEd Tanous return objectKeyCmp(key, left, right) < 0; 907185444b1SNan Zhou } 908185444b1SNan Zhou }; 909185444b1SNan Zhou 910185444b1SNan Zhou // Sort the JSON array by |element[key]|. 911185444b1SNan Zhou // Elements without |key| or type of |element[key]| is not string are smaller 912185444b1SNan Zhou // those whose |element[key]| is string. 9134e196b9aSEd Tanous inline void sortJsonArrayByKey(nlohmann::json::array_t& array, 9144e196b9aSEd Tanous std::string_view key) 9154e196b9aSEd Tanous { 9164e196b9aSEd Tanous std::ranges::sort(array, ODataObjectLess(key)); 9174e196b9aSEd Tanous } 9184e196b9aSEd Tanous 9194e196b9aSEd Tanous // Sort the JSON array by |element[key]|. 9204e196b9aSEd Tanous // Elements without |key| or type of |element[key]| is not string are smaller 9214e196b9aSEd Tanous // those whose |element[key]| is string. 922185444b1SNan Zhou inline void sortJsonArrayByOData(nlohmann::json::array_t& array) 923185444b1SNan Zhou { 9244e196b9aSEd Tanous std::ranges::sort(array, ODataObjectLess("@odata.id")); 925185444b1SNan Zhou } 926185444b1SNan Zhou 9278a7c4b47SNan Zhou // Returns the estimated size of the JSON value 9288a7c4b47SNan Zhou // The implementation walks through every key and every value, accumulates the 9298a7c4b47SNan Zhou // total size of keys and values. 9308a7c4b47SNan Zhou // Ideally, we should use a custom allocator that nlohmann JSON supports. 9318a7c4b47SNan Zhou 9328a7c4b47SNan Zhou // Assumption made: 9338a7c4b47SNan Zhou // 1. number: 8 characters 9348a7c4b47SNan Zhou // 2. boolean: 5 characters (False) 9358a7c4b47SNan Zhou // 3. string: len(str) + 2 characters (quote) 9368a7c4b47SNan Zhou // 4. bytes: len(bytes) characters 9378a7c4b47SNan Zhou // 5. null: 4 characters (null) 9388a7c4b47SNan Zhou uint64_t getEstimatedJsonSize(const nlohmann::json& root); 9398a7c4b47SNan Zhou 94077dd8813SKowalski, Kamil } // namespace json_util 94177dd8813SKowalski, Kamil } // namespace redfish 942