From 404f423d719d8a4040c93e5c1f32425ccd2f99d0 Mon Sep 17 00:00:00 2001 From: Muhammad Nawaz Date: Mon, 8 Jul 2024 12:30:16 +0500 Subject: [PATCH] Method mapping: Added support to map HTTP methods --- include/oas_validator_imp.hpp | 2 ++ src/oas_validator_imp.cpp | 21 ++++++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/include/oas_validator_imp.hpp b/include/oas_validator_imp.hpp index eb07707..445a049 100644 --- a/include/oas_validator_imp.hpp +++ b/include/oas_validator_imp.hpp @@ -71,6 +71,8 @@ class OASValidatorImp std::unordered_map& per_path_validator); void ResolveReferences(rapidjson::Value& value, rapidjson::Document& doc, rapidjson::Document::AllocatorType& allocator); + static std::unordered_map> + BuildCaseInsensitiveMap(const std::unordered_map>& method_map); }; #endif // OAS_VALIDATION_HPP diff --git a/src/oas_validator_imp.cpp b/src/oas_validator_imp.cpp index e9fa0dc..b1f0f82 100644 --- a/src/oas_validator_imp.cpp +++ b/src/oas_validator_imp.cpp @@ -5,13 +5,14 @@ // [ END OF LICENSE c6bd0f49d040fca8d8a9cb05868e66aa63f0e2e0 ] #include "oas_validator_imp.hpp" +#include #include #include #include OASValidatorImp::OASValidatorImp(const std::string& oas_specs, const std::unordered_map>& method_map) - : method_map_(method_map) + : method_map_(BuildCaseInsensitiveMap(method_map)) { rapidjson::Document doc; ParseSpecs(oas_specs, doc); @@ -374,6 +375,24 @@ void OASValidatorImp::ResolveReferences(rapidjson::Value& value, rapidjson::Docu } } +std::unordered_map> OASValidatorImp::BuildCaseInsensitiveMap( + const std::unordered_map>& method_map) +{ + + std::unordered_map> case_insensitive_map; + + for (const auto& entry : method_map) { + std::string lower_key(entry.first); + std::transform(lower_key.begin(), lower_key.end(), lower_key.begin(), ::tolower); + std::string upper_key(entry.first); + std::transform(upper_key.begin(), upper_key.end(), upper_key.begin(), ::toupper); + case_insensitive_map[lower_key] = entry.second; + case_insensitive_map[upper_key] = entry.second; + } + + return case_insensitive_map; +} + const std::unordered_map OASValidatorImp::kStringToMethod = { {"GET", HttpMethod::GET}, {"POST", HttpMethod::POST}, {"PUT", HttpMethod::PUT}, {"DELETE", HttpMethod::DELETE}, {"HEAD", HttpMethod::HEAD}, {"OPTIONS", HttpMethod::OPTIONS},