Skip to content

Commit

Permalink
Method mapping: Added support to map HTTP methods
Browse files Browse the repository at this point in the history
  • Loading branch information
nawaz1991 committed Jul 8, 2024
1 parent b624faa commit 404f423
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
2 changes: 2 additions & 0 deletions include/oas_validator_imp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ class OASValidatorImp
std::unordered_map<std::string, ValidatorsStore*>& per_path_validator);
void ResolveReferences(rapidjson::Value& value, rapidjson::Document& doc,
rapidjson::Document::AllocatorType& allocator);
static std::unordered_map<std::string, std::unordered_set<std::string>>
BuildCaseInsensitiveMap(const std::unordered_map<std::string, std::unordered_set<std::string>>& method_map);
};

#endif // OAS_VALIDATION_HPP
21 changes: 20 additions & 1 deletion src/oas_validator_imp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
// [ END OF LICENSE c6bd0f49d040fca8d8a9cb05868e66aa63f0e2e0 ]

#include "oas_validator_imp.hpp"
#include <algorithm>
#include <fstream>
#include <rapidjson/istreamwrapper.h>
#include <sstream>

OASValidatorImp::OASValidatorImp(const std::string& oas_specs,
const std::unordered_map<std::string, std::unordered_set<std::string>>& method_map)
: method_map_(method_map)
: method_map_(BuildCaseInsensitiveMap(method_map))
{
rapidjson::Document doc;
ParseSpecs(oas_specs, doc);
Expand Down Expand Up @@ -374,6 +375,24 @@ void OASValidatorImp::ResolveReferences(rapidjson::Value& value, rapidjson::Docu
}
}

std::unordered_map<std::string, std::unordered_set<std::string>> OASValidatorImp::BuildCaseInsensitiveMap(
const std::unordered_map<std::string, std::unordered_set<std::string>>& method_map)
{

std::unordered_map<std::string, std::unordered_set<std::string>> 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<std::string, HttpMethod> OASValidatorImp::kStringToMethod = {
{"GET", HttpMethod::GET}, {"POST", HttpMethod::POST}, {"PUT", HttpMethod::PUT},
{"DELETE", HttpMethod::DELETE}, {"HEAD", HttpMethod::HEAD}, {"OPTIONS", HttpMethod::OPTIONS},
Expand Down

0 comments on commit 404f423

Please sign in to comment.