Skip to content

Commit 6e3d1e7

Browse files
committed
Method mapping: Thread HEAD as GET
1 parent 364b362 commit 6e3d1e7

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

include/oas_validator_imp.hpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
class OASValidatorImp
1616
{
1717
public:
18-
explicit OASValidatorImp(const std::string& oas_specs);
18+
explicit OASValidatorImp(const std::string& oas_specs, bool head_mapped_get = false);
1919
ValidationError ValidateRoute(const std::string& method, const std::string& http_path, std::string& error_msg);
2020
ValidationError ValidateBody(const std::string& method, const std::string& http_path, const std::string& json_body,
2121
std::string& error_msg);
@@ -45,12 +45,17 @@ class OASValidatorImp
4545
PathTrie path_trie{};
4646
};
4747

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

5152
ValidationError GetValidators(const std::string& method, const std::string& http_path, ValidatorsStore*& validators,
5253
std::string& error_msg, std::unordered_map<size_t, ParamRange>* param_idxs = nullptr,
5354
std::string* query = nullptr);
55+
ValidationError GetValidators(const std::string& method, const std::string& mapped_method,
56+
const std::string& http_path, ValidatorsStore*& validators, std::string& error_msg,
57+
std::unordered_map<size_t, ParamRange>* param_idxs = nullptr,
58+
std::string* query = nullptr);
5459
static std::vector<std::string> Split(const std::string& str);
5560
static rapidjson::Value* ResolvePath(rapidjson::Document& doc, const std::string& path);
5661
static void ParseSpecs(const std::string& oas_specs, rapidjson::Document& doc);

src/oas_validator_imp.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@
99
#include <rapidjson/istreamwrapper.h>
1010
#include <sstream>
1111

12-
OASValidatorImp::OASValidatorImp(const std::string& oas_specs)
12+
OASValidatorImp::OASValidatorImp(const std::string& oas_specs, bool head_mapped_get)
13+
: head_mapped_get_(head_mapped_get)
1314
{
1415
rapidjson::Document doc;
1516
ParseSpecs(oas_specs, doc);
@@ -175,7 +176,21 @@ ValidationError OASValidatorImp::GetValidators(const std::string& method, const
175176
auto err_code = method_validator_.Validate(method, error_msg);
176177
CHECK_ERROR(err_code)
177178

178-
auto enum_method = kStringToMethod.at(method);
179+
err_code = GetValidators(method, method, http_path, validators, error_msg, param_idxs, query);
180+
181+
if (head_mapped_get_ && ValidationError::INVALID_ROUTE == err_code && (method == "head" || method == "HEAD")) {
182+
return GetValidators(method, "get", http_path, validators, error_msg, param_idxs, query);
183+
}
184+
185+
return err_code;
186+
}
187+
188+
ValidationError OASValidatorImp::GetValidators(const std::string& method, const std::string& mapped_method,
189+
const std::string& http_path, ValidatorsStore*& validators,
190+
std::string& error_msg,
191+
std::unordered_map<size_t, ParamRange>* param_idxs, std::string* query)
192+
{
193+
auto enum_method = kStringToMethod.at(mapped_method);
179194

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

0 commit comments

Comments
 (0)