Skip to content

Commit 3fd3d3f

Browse files
committed
Method mapping: Added support to map HTTP methods
1 parent 3cead04 commit 3fd3d3f

2 files changed

Lines changed: 22 additions & 1 deletion

File tree

include/oas_validator_imp.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ class OASValidatorImp
7171
std::unordered_map<std::string, ValidatorsStore*>& per_path_validator);
7272
void ResolveReferences(rapidjson::Value& value, rapidjson::Document& doc,
7373
rapidjson::Document::AllocatorType& allocator);
74+
static std::unordered_map<std::string, std::unordered_set<std::string>>
75+
BuildCaseInsensitiveMap(const std::unordered_map<std::string, std::unordered_set<std::string>>& method_map);
7476
};
7577

7678
#endif // OAS_VALIDATION_HPP

src/oas_validator_imp.cpp

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
// [ END OF LICENSE c6bd0f49d040fca8d8a9cb05868e66aa63f0e2e0 ]
66

77
#include "oas_validator_imp.hpp"
8+
#include <algorithm>
89
#include <fstream>
910
#include <rapidjson/istreamwrapper.h>
1011
#include <sstream>
1112

1213
OASValidatorImp::OASValidatorImp(const std::string& oas_specs,
1314
const std::unordered_map<std::string, std::unordered_set<std::string>>& method_map)
14-
: method_map_(method_map)
15+
: method_map_(BuildCaseInsensitiveMap(method_map))
1516
{
1617
rapidjson::Document doc;
1718
ParseSpecs(oas_specs, doc);
@@ -374,6 +375,24 @@ void OASValidatorImp::ResolveReferences(rapidjson::Value& value, rapidjson::Docu
374375
}
375376
}
376377

378+
std::unordered_map<std::string, std::unordered_set<std::string>> OASValidatorImp::BuildCaseInsensitiveMap(
379+
const std::unordered_map<std::string, std::unordered_set<std::string>>& method_map)
380+
{
381+
382+
std::unordered_map<std::string, std::unordered_set<std::string>> case_insensitive_map;
383+
384+
for (const auto& entry : method_map) {
385+
std::string lower_key(entry.first);
386+
std::transform(lower_key.begin(), lower_key.end(), lower_key.begin(), ::tolower);
387+
std::string upper_key(entry.first);
388+
std::transform(upper_key.begin(), upper_key.end(), upper_key.begin(), ::toupper);
389+
case_insensitive_map[lower_key] = entry.second;
390+
case_insensitive_map[upper_key] = entry.second;
391+
}
392+
393+
return case_insensitive_map;
394+
}
395+
377396
const std::unordered_map<std::string, HttpMethod> OASValidatorImp::kStringToMethod = {
378397
{"GET", HttpMethod::GET}, {"POST", HttpMethod::POST}, {"PUT", HttpMethod::PUT},
379398
{"DELETE", HttpMethod::DELETE}, {"HEAD", HttpMethod::HEAD}, {"OPTIONS", HttpMethod::OPTIONS},

0 commit comments

Comments
 (0)