Skip to content

Commit

Permalink
Formatted code and clang-tidy fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nawaz1991 committed Jul 9, 2024
1 parent 5fb2bcf commit 9a7ba31
Show file tree
Hide file tree
Showing 33 changed files with 725 additions and 422 deletions.
28 changes: 18 additions & 10 deletions example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ int main()
std::cout << err_msg << std::endl;
}

if (ValidationError::NONE != oas_validator.ValidatePathParam("GET", "/test/integer_simple_true/not_an_integer", err_msg)) {
if (ValidationError::NONE !=
oas_validator.ValidatePathParam("GET", "/test/integer_simple_true/not_an_integer", err_msg)) {
std::cout << err_msg << std::endl;
}

if (ValidationError::NONE != oas_validator.ValidateQueryParam("GET", "/test/query_integer_form_true?param=not_an_integer", err_msg)) {
if (ValidationError::NONE !=
oas_validator.ValidateQueryParam("GET", "/test/query_integer_form_true?param=not_an_integer", err_msg)) {
std::cout << err_msg << std::endl;
}

Expand All @@ -44,14 +46,20 @@ int main()
std::cout << err_msg << std::endl;
}

if (ValidationError::NONE != oas_validator.ValidateRequest("POST",
"/test/all/123/abc/str1,str2/"
"field1,0,field2,string?param4=string1&param4=string2&param5=field1,0,field2,string&param6=field1,0,field2,string&"
"param7=field1,0,field2,string&param8=field1,0,field2,string&param9=field1,0,field2,string&param10=false",
"{\"field1\":123,\"field2\":\"abc\",\"field3\":[\"abc\",\"def\"],\"field4\":{\"subfield1\":123,\"subfield2\":"
"\"abc\"},\"field5\":{\"subfield1\":123},\"field6\":true,\"field7\":[123,456],\"field8\":[123,456],\"field9\":"
"\"abc\",\"field10\":\"option1\",\"field11\":{\"field\":123},\"field12\":[{\"name\":\"abc\"},{\"name\":\"def\"}]}",
headers, err_msg)) {
if (ValidationError::NONE !=
oas_validator.ValidateRequest(
"POST",
"/test/all/123/abc/str1,str2/"
"field1,0,field2,string?param4=string1&param4=string2&param5=field1,0,field2,string&param6=field1,0,field2,"
"string&"
"param7=field1,0,field2,string&param8=field1,0,field2,string&param9=field1,0,field2,string&param10=false",
"{\"field1\":123,\"field2\":\"abc\",\"field3\":[\"abc\",\"def\"],\"field4\":{\"subfield1\":123,"
"\"subfield2\":"
"\"abc\"},\"field5\":{\"subfield1\":123},\"field6\":true,\"field7\":[123,456],\"field8\":[123,456],"
"\"field9\":"
"\"abc\",\"field10\":\"option1\",\"field11\":{\"field\":123},\"field12\":[{\"name\":\"abc\"},{\"name\":"
"\"def\"}]}",
headers, err_msg)) {
std::cout << err_msg << std::endl;
}
}
3 changes: 2 additions & 1 deletion include/deserializers/array_deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
class ArrayDeserializer final: public BaseDeserializer
{
public:
explicit ArrayDeserializer(const std::string& param_name, char start, bool skip_name, PrimitiveType items_type, char separator, bool has_running_name, bool has_20_separator);
explicit ArrayDeserializer(const std::string& param_name, char start, bool skip_name, PrimitiveType items_type,
char separator, bool has_running_name, bool has_20_separator);

std::string Deserialize(const char* beg, const char* const end) override;
~ArrayDeserializer() override = default;
Expand Down
12 changes: 8 additions & 4 deletions include/deserializers/base_deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ class BaseDeserializer
if (start_ == *cursor) {
++cursor;
} else {
throw DeserializationException("Parameter '" + param_name_ + "' should start with '" + std::string(1, start_) + "'");
throw DeserializationException("Parameter '" + param_name_ + "' should start with '" +
std::string(1, start_) + "'");
}
}
}
Expand All @@ -72,7 +73,8 @@ class BaseDeserializer

inline void CheckNSkipName(const char*& cursor, const char* const end) const
{
if (std::distance(cursor, end) < static_cast<long>(param_name_.size()) || !std::equal(param_name_.begin(), param_name_.end(), cursor)) {
if (std::distance(cursor, end) < static_cast<long>(param_name_.size()) ||
!std::equal(param_name_.begin(), param_name_.end(), cursor)) {
throw DeserializationException("Parameter name mismatch for the parameter '" + param_name_ + "'");
}
cursor += param_name_.size();
Expand Down Expand Up @@ -131,7 +133,8 @@ class BaseDeserializer
has_decimal_point |= (*cursor == '.');
++cursor;
}
if (cursor > start_cursor && (cursor - start_cursor != 1 || (*start_cursor != '-' && *start_cursor != '.')) && (!has_decimal_point || *(cursor - 1) != '.')) {
if (cursor > start_cursor && (cursor - start_cursor != 1 || (*start_cursor != '-' && *start_cursor != '.')) &&
(!has_decimal_point || *(cursor - 1) != '.')) {
ret.append(start_cursor, cursor);
} else {
throw DeserializationException("Invalid 'number' format for '" + param_name_ + "'");
Expand Down Expand Up @@ -168,7 +171,8 @@ class BaseDeserializer
ret.push_back('"');
}

inline void DeserializeString(const char*& cursor, const char* const end, const char terminator, std::string& ret) const
inline void DeserializeString(const char*& cursor, const char* const end, const char terminator,
std::string& ret) const
{
ret.push_back('"');
while (cursor < end && *cursor != terminator) {
Expand Down
6 changes: 4 additions & 2 deletions include/deserializers/object_deserializer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ using ObjKTMap = std::unordered_map<std::string, PrimitiveType>; // Object Key-n
class ObjectDeserializer final: public BaseDeserializer
{
public:
explicit ObjectDeserializer(const std::string& param_name, char start, bool skip_name, char kv_separator, char vk_separator, bool is_deep_obj, const ObjKTMap& kt_map);
explicit ObjectDeserializer(const std::string& param_name, char start, bool skip_name, char kv_separator,
char vk_separator, bool is_deep_obj, const ObjKTMap& kt_map);

std::string Deserialize(const char* beg, const char* const end) override;
~ObjectDeserializer() override = default;
Expand All @@ -26,7 +27,8 @@ class ObjectDeserializer final: public BaseDeserializer
const bool is_deep_obj_;
const ObjKTMap kt_map_; // key-type map

inline void DeserializeKey(const char*& cursor, const char* const end, const char terminator, std::string& key) const
inline void DeserializeKey(const char*& cursor, const char* const end, const char terminator,
std::string& key) const
{
key.push_back('"');
while (cursor < end && *cursor != terminator) {
Expand Down
23 changes: 16 additions & 7 deletions include/oas_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ class OASValidator
public:
/**
* @brief Constructor that takes the path to the OAS specification file.
* @param oas_specs File path to the OAS specification in JSON format or JSON string containing the OAS specification.
* @param oas_specs File path to the OAS specification in JSON format or JSON string containing the OAS
* specification.
*
* @note The OAS specification can be provided as a file path or as a JSON string.
*/
Expand Down Expand Up @@ -106,7 +107,8 @@ class OASValidator
*
* @note The error_msg argument will be populated with a JSON string in case of a validation error.
*/
ValidationError ValidateBody(const std::string& method, const std::string& http_path, const std::string& json_body, std::string& error_msg);
ValidationError ValidateBody(const std::string& method, const std::string& http_path, const std::string& json_body,
std::string& error_msg);

/**
* @brief Validates the path parameters of the HTTP request against the OpenAPI specification.
Expand Down Expand Up @@ -177,7 +179,9 @@ class OASValidator
* @note The error_msg argument will be populated with a JSON string in case of a validation error.
*/

ValidationError ValidateHeaders(const std::string& method, const std::string& http_path, const std::unordered_map<std::string, std::string>& headers, std::string& error_msg);
ValidationError ValidateHeaders(const std::string& method, const std::string& http_path,
const std::unordered_map<std::string, std::string>& headers,
std::string& error_msg);

/**
* @brief Validates the entire HTTP request against the OpenAPI specification.
Expand Down Expand Up @@ -232,7 +236,8 @@ class OASValidator
*
* @note The error_msg argument will be populated with a JSON string in case of a validation error.
*/
ValidationError ValidateRequest(const std::string& method, const std::string& http_path, const std::string& json_body, std::string& error_msg);
ValidationError ValidateRequest(const std::string& method, const std::string& http_path,
const std::string& json_body, std::string& error_msg);

/**
* @brief Validates the entire HTTP request, including headers, against the OpenAPI specification.
Expand Down Expand Up @@ -261,7 +266,9 @@ class OASValidator
*
* @note The error_msg argument will be populated with a JSON string in case of a validation error.
*/
ValidationError ValidateRequest(const std::string& method, const std::string& http_path, const std::unordered_map<std::string, std::string>& headers, std::string& error_msg);
ValidationError ValidateRequest(const std::string& method, const std::string& http_path,
const std::unordered_map<std::string, std::string>& headers,
std::string& error_msg);

/**
* @brief Validates the entire HTTP request, including JSON body and headers, against the OpenAPI specification.
Expand Down Expand Up @@ -293,8 +300,10 @@ class OASValidator
*
* @note The error_msg argument will be populated with a JSON string in case of a validation error.
*/
ValidationError ValidateRequest(const std::string& method, const std::string& http_path, const std::string& json_body,
const std::unordered_map<std::string, std::string>& headers, std::string& error_msg);
ValidationError ValidateRequest(const std::string& method, const std::string& http_path,
const std::string& json_body,
const std::unordered_map<std::string, std::string>& headers,
std::string& error_msg);

~OASValidator();
};
Expand Down
28 changes: 19 additions & 9 deletions include/oas_validator_imp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,23 @@ class OASValidatorImp
public:
explicit OASValidatorImp(const std::string& oas_specs);
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);
ValidationError ValidateBody(const std::string& method, const std::string& http_path, const std::string& json_body,
std::string& error_msg);
ValidationError ValidatePathParam(const std::string& method, const std::string& http_path, std::string& error_msg);
ValidationError ValidateQueryParam(const std::string& method, const std::string& http_path, std::string& error_msg);
ValidationError ValidateHeaders(const std::string& method, const std::string& http_path, const std::unordered_map<std::string, std::string>& headers, std::string& error_msg);
ValidationError ValidateHeaders(const std::string& method, const std::string& http_path,
const std::unordered_map<std::string, std::string>& headers,
std::string& error_msg);
ValidationError ValidateRequest(const std::string& method, const std::string& http_path, std::string& error_msg);
ValidationError ValidateRequest(const std::string& method, const std::string& http_path, const std::string& json_body, std::string& error_msg);
ValidationError ValidateRequest(const std::string& method, const std::string& http_path, const std::unordered_map<std::string, std::string>& headers, std::string& error_msg);
ValidationError ValidateRequest(const std::string& method, const std::string& http_path, const std::string& json_body,
const std::unordered_map<std::string, std::string>& headers, std::string& error_msg);
ValidationError ValidateRequest(const std::string& method, const std::string& http_path,
const std::string& json_body, std::string& error_msg);
ValidationError ValidateRequest(const std::string& method, const std::string& http_path,
const std::unordered_map<std::string, std::string>& headers,
std::string& error_msg);
ValidationError ValidateRequest(const std::string& method, const std::string& http_path,
const std::string& json_body,
const std::unordered_map<std::string, std::string>& headers,
std::string& error_msg);
~OASValidatorImp();

private:
Expand All @@ -40,11 +48,13 @@ class OASValidatorImp
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& 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, char delimiter);
static rapidjson::Value* ResolvePath(rapidjson::Document& doc, const std::string& path);
void ResolveReferences(rapidjson::Value& value, rapidjson::Document& doc, rapidjson::Document::AllocatorType& allocator);
void ResolveReferences(rapidjson::Value& value, rapidjson::Document& doc,
rapidjson::Document::AllocatorType& allocator);
};

#endif // OAS_VALIDATION_HPP
2 changes: 1 addition & 1 deletion include/utils/common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class ValidatorInitExc: public std::exception
};

#define CHECK_ERROR(err) \
if (ValidationError::NONE != err) { \
if (ValidationError::NONE != (err)) { \
return err; \
}

Expand Down
3 changes: 2 additions & 1 deletion include/utils/path_trie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ class PathTrie
}
void Insert(const std::string& path);
bool Search(const char* beg, const char* end, std::string& oas_path);
bool Search(const char* beg, const char* end, std::string& oas_path, std::unordered_map<size_t, ParamRange>& param_idxs);
bool Search(const char* beg, const char* end, std::string& oas_path,
std::unordered_map<size_t, ParamRange>& param_idxs);
~PathTrie();

private:
Expand Down
12 changes: 7 additions & 5 deletions include/validators/json_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,16 @@ class JsonValidator: public BaseValidator
rapidjson::SchemaValidator* validator_ = nullptr;
std::mutex mutex_;

void CreateErrorMessages(const rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>& errors, const std::string& context, std::string& error_msg,
bool recursive = false);
void HandleError(const char* error_name, const rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>& error, const std::string& context, std::string& error_msg,
bool recursive);
void CreateErrorMessages(const rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>& errors,
const std::string& context, std::string& error_msg, bool recursive = false);
void HandleError(const char* error_name,
const rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>& error,
const std::string& context, std::string& error_msg, bool recursive);
static std::string GetString(const rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>& val);

public:
JsonValidator(const rapidjson::Value& schema_val, const std::vector<std::string>& ref_keys, ValidationError err_code);
JsonValidator(const rapidjson::Value& schema_val, const std::vector<std::string>& ref_keys,
ValidationError err_code);
ValidationError ValidateJson(const std::string& json_str, std::string& error_msg) override;
~JsonValidator() override;
};
Expand Down
3 changes: 2 additions & 1 deletion include/validators/param_validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class ParamValidator: public JsonValidator
~ParamValidator() override = default;

protected:
static ParamInfo GetParamInfo(const rapidjson::Value& param_val, const std::string& default_style, bool default_explode, bool default_required,
static ParamInfo GetParamInfo(const rapidjson::Value& param_val, const std::string& default_style,
bool default_explode, bool default_required,
const std::vector<std::string>& ref_keys);

private:
Expand Down
6 changes: 4 additions & 2 deletions include/validators/validators_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ class ValidatorsStore
public:
ValidatorsStore() = default;
explicit ValidatorsStore(const rapidjson::Value& schema_val, const std::vector<std::string>& ref_keys);
void AddParamValidators(const std::string& path, const rapidjson::Value& params, std::vector<std::string>& ref_keys);
void AddParamValidators(const std::string& path, const rapidjson::Value& params,
std::vector<std::string>& ref_keys);
ValidationError ValidateBody(const std::string& json_body, std::string& error_msg);
ValidationError ValidatePathParams(std::unordered_map<size_t, ParamRange>& param_idxs, std::string& error_msg);
ValidationError ValidateQueryParams(const std::string& query, std::string& error_msg);
ValidationError ValidateHeaderParams(const std::unordered_map<std::string, std::string>& headers, std::string& error_msg);
ValidationError ValidateHeaderParams(const std::unordered_map<std::string, std::string>& headers,
std::string& error_msg);
~ValidatorsStore();

private:
Expand Down
3 changes: 2 additions & 1 deletion src/deserializers/array_deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

#include "deserializers/array_deserializer.hpp"

ArrayDeserializer::ArrayDeserializer(const std::string& param_name, char start, bool skip_name, PrimitiveType items_type, char separator, bool has_running_name,
ArrayDeserializer::ArrayDeserializer(const std::string& param_name, char start, bool skip_name,
PrimitiveType items_type, char separator, bool has_running_name,
bool has_20_separator)
: BaseDeserializer(param_name, start, skip_name)
, items_type_(items_type)
Expand Down
5 changes: 4 additions & 1 deletion src/deserializers/base_deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ BaseDeserializer::BaseDeserializer(const std::string& param_name, char start, bo
const std::array<char, 256> BaseDeserializer::kHexLookupTable = []() {
std::array<char, 256> table{};
for (int i = 0; i < 256; ++i) {
table[i] = (i >= '0' && i <= '9') ? i - '0' : (i >= 'A' && i <= 'F') ? i - 'A' + 10 : (i >= 'a' && i <= 'f') ? i - 'a' + 10 : -1;
table[i] = (i >= '0' && i <= '9') ? i - '0'
: (i >= 'A' && i <= 'F') ? i - 'A' + 10
: (i >= 'a' && i <= 'f') ? i - 'a' + 10
: -1;
}
return table;
}();
3 changes: 2 additions & 1 deletion src/deserializers/object_deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

#include "deserializers/object_deserializer.hpp"

ObjectDeserializer::ObjectDeserializer(const std::string& param_name, char start, bool skip_name, char kv_separator, char vk_separator, bool is_deep_obj, const ObjKTMap& kt_map)
ObjectDeserializer::ObjectDeserializer(const std::string& param_name, char start, bool skip_name, char kv_separator,
char vk_separator, bool is_deep_obj, const ObjKTMap& kt_map)
: BaseDeserializer(param_name, start, skip_name)
, kv_separator_(kv_separator)
, vk_separator_(vk_separator)
Expand Down
Loading

0 comments on commit 9a7ba31

Please sign in to comment.