Skip to content

Commit

Permalink
Method mapping: Thread HEAD as GET
Browse files Browse the repository at this point in the history
  • Loading branch information
nawaz1991 committed Jul 6, 2024
1 parent 364b362 commit 6e3d1e7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
7 changes: 6 additions & 1 deletion include/oas_validator_imp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
class OASValidatorImp
{
public:
explicit OASValidatorImp(const std::string& oas_specs);
explicit OASValidatorImp(const std::string& oas_specs, bool head_mapped_get = false);
ValidationError ValidateRoute(const std::string& method, const std::string& http_path, std::string& error_msg);
ValidationError ValidateBody(const std::string& method, const std::string& http_path, const std::string& json_body,
std::string& error_msg);
Expand Down Expand Up @@ -45,12 +45,17 @@ class OASValidatorImp
PathTrie path_trie{};
};

bool head_mapped_get_;
std::array<PerMethod, static_cast<size_t>(HttpMethod::COUNT)> oas_validators_{};
MethodValidator method_validator_{};

ValidationError GetValidators(const std::string& method, const std::string& http_path, ValidatorsStore*& validators,
std::string& error_msg, std::unordered_map<size_t, ParamRange>* param_idxs = nullptr,
std::string* query = nullptr);
ValidationError GetValidators(const std::string& method, const std::string& mapped_method,
const std::string& http_path, ValidatorsStore*& validators, std::string& error_msg,
std::unordered_map<size_t, ParamRange>* param_idxs = nullptr,
std::string* query = nullptr);
static std::vector<std::string> Split(const std::string& str);
static rapidjson::Value* ResolvePath(rapidjson::Document& doc, const std::string& path);
static void ParseSpecs(const std::string& oas_specs, rapidjson::Document& doc);
Expand Down
19 changes: 17 additions & 2 deletions src/oas_validator_imp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#include <rapidjson/istreamwrapper.h>
#include <sstream>

OASValidatorImp::OASValidatorImp(const std::string& oas_specs)
OASValidatorImp::OASValidatorImp(const std::string& oas_specs, bool head_mapped_get)
: head_mapped_get_(head_mapped_get)
{
rapidjson::Document doc;
ParseSpecs(oas_specs, doc);
Expand Down Expand Up @@ -175,7 +176,21 @@ ValidationError OASValidatorImp::GetValidators(const std::string& method, const
auto err_code = method_validator_.Validate(method, error_msg);
CHECK_ERROR(err_code)

auto enum_method = kStringToMethod.at(method);
err_code = GetValidators(method, method, http_path, validators, error_msg, param_idxs, query);

if (head_mapped_get_ && ValidationError::INVALID_ROUTE == err_code && (method == "head" || method == "HEAD")) {
return GetValidators(method, "get", http_path, validators, error_msg, param_idxs, query);
}

return err_code;
}

ValidationError OASValidatorImp::GetValidators(const std::string& method, const std::string& mapped_method,
const std::string& http_path, ValidatorsStore*& validators,
std::string& error_msg,
std::unordered_map<size_t, ParamRange>* param_idxs, std::string* query)
{
auto enum_method = kStringToMethod.at(mapped_method);

auto query_pos = http_path.find('?');
if (std::string::npos != query_pos && query) {
Expand Down

0 comments on commit 6e3d1e7

Please sign in to comment.