Skip to content

Commit

Permalink
Fix for warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
nawaz1991 committed Jun 15, 2024
1 parent b33ce8e commit d477ed4
Show file tree
Hide file tree
Showing 19 changed files with 175 additions and 55 deletions.
13 changes: 13 additions & 0 deletions include/oas_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ class OASValidator
*/
explicit OASValidator(const std::string& oas_specs);

/**
* @brief Copy constructor.
* @param other The OASValidator object to be copied.
*/
OASValidator(const OASValidator& other);

/**
* @brief Copy assignment operator.
* @param other The OASValidator object to be copied.
* @return Reference to the copied OASValidator object.
*/
OASValidator& operator=(const OASValidator& other);

/**
* @brief Validates the HTTP method and route against the OpenAPI specification.
*
Expand Down
10 changes: 5 additions & 5 deletions include/oas_validator_imp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,17 @@ class OASValidatorImp

struct PerMethod
{
std::unordered_map<std::string, ValidatorsStore*> per_path_validators;
PathTrie path_trie;
std::unordered_map<std::string, ValidatorsStore*> per_path_validators{};
PathTrie path_trie{};
};

std::array<PerMethod, static_cast<size_t>(HttpMethod::COUNT)> oas_validators_;
MethodValidator method_validator_;
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);
static std::vector<std::string> Split(const std::string &str);
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);
void ProcessPath(const rapidjson::Value::ConstMemberIterator& path_itr, std::vector<std::string>& ref_keys);
Expand Down
23 changes: 13 additions & 10 deletions include/utils/path_trie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,37 @@

#ifndef PATH_TRIE_HPP
#define PATH_TRIE_HPP
#include "utils/common.hpp"

#include "utils/common.hpp"
#include <string>
#include <unordered_map>

class PathTrie
{
public:
PathTrie()
: root_(new Node())
{
}
PathTrie();
PathTrie(const PathTrie& other); // Copy constructor
PathTrie& operator=(const PathTrie& other); // Copy assignment operator
~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);
~PathTrie();

private:
struct Node
{
std::string dir;
bool is_param{false};
int frag_idx{0};
std::unordered_map<std::string, Node*> children;
Node() = default;
std::string dir{};
bool is_param = false;
int frag_idx = 0;
std::unordered_map<std::string, Node*> children{};
};

void DeleteNode(Node* node);
void CopyNode(Node*& this_node, Node* other_node);

Node* root_;
};

Expand Down
2 changes: 0 additions & 2 deletions include/validators/base_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class BaseValidator
ValidationError code_on_error_;
std::string err_header_;

void InitErrHeader();

private:
static const std::unordered_map<ValidationError, std::string> kErrHeaders;
};
Expand Down
8 changes: 5 additions & 3 deletions include/validators/json_validator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
class JsonValidator: public BaseValidator
{
private:
rapidjson::SchemaDocument* schema_ = nullptr;
rapidjson::SchemaValidator* validator_ = nullptr;
std::mutex mutex_;
rapidjson::SchemaDocument* schema_;
rapidjson::SchemaValidator* validator_;
std::mutex mutex_{};

void CreateErrorMessages(const rapidjson::GenericValue<rapidjson::UTF8<>, rapidjson::CrtAllocator>& errors,
const std::string& context, std::string& error_msg, bool recursive = false);
Expand All @@ -29,6 +29,8 @@ class JsonValidator: public BaseValidator
public:
JsonValidator(const rapidjson::Value& schema_val, const std::vector<std::string>& ref_keys,
ValidationError err_code);
JsonValidator(const JsonValidator&) = delete;
JsonValidator& operator=(const JsonValidator&) = delete;
ValidationError Validate(const std::string& json_str, std::string& error_msg) override;
~JsonValidator() override;
};
Expand Down
8 changes: 8 additions & 0 deletions include/validators/param_validators.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class ParamValidator: public JsonValidator

public:
ParamValidator(const ParamInfo& param_info, const std::vector<std::string>& ref_keys, ValidationError err_code);
ParamValidator(const ParamValidator&) = delete;
ParamValidator& operator=(const ParamValidator&) = delete;

ValidationError ValidateParam(const char* beg, const char* end, std::string& error_msg);
bool IsRequired() const;
Expand All @@ -47,13 +49,17 @@ class PathParamValidator final: public ParamValidator
{
public:
explicit PathParamValidator(const rapidjson::Value& param_val, const std::vector<std::string>& keys);
PathParamValidator(const PathParamValidator&) = delete;
PathParamValidator& operator=(const PathParamValidator&) = delete;
~PathParamValidator() override = default;
};

class QueryParamValidator final: public ParamValidator
{
public:
explicit QueryParamValidator(const rapidjson::Value& param_val, const std::vector<std::string>& keys);
QueryParamValidator(const QueryParamValidator&) = delete;
QueryParamValidator& operator=(const QueryParamValidator&) = delete;
bool IsEmptyAllowed() const;
~QueryParamValidator() override = default;

Expand All @@ -65,6 +71,8 @@ class HeaderParamValidator final: public ParamValidator
{
public:
explicit HeaderParamValidator(const rapidjson::Value& param_val, const std::vector<std::string>& keys);
HeaderParamValidator(const HeaderParamValidator&) = delete;
HeaderParamValidator& operator=(const HeaderParamValidator&) = delete;
~HeaderParamValidator() override = default;
};
#endif // PARAM_VALIDATOR_HPP
59 changes: 51 additions & 8 deletions include/validators/validators_store.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,15 @@
#include "validators/body_validator.hpp"
#include "validators/param_validators.hpp"

#include <utility>
#include <vector>
class ValidatorsStore
{
public:
ValidatorsStore() = default;
explicit ValidatorsStore(const rapidjson::Value& schema_val, const std::vector<std::string>& ref_keys);
ValidatorsStore(const ValidatorsStore&) = delete;
ValidatorsStore& operator=(const ValidatorsStore&) = delete;
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);
Expand All @@ -30,22 +33,62 @@ class ValidatorsStore
private:
struct PathParamValidatorInfo
{
int idx;
PathParamValidator* validator;
PathParamValidatorInfo(size_t idx, PathParamValidator* validator)
: idx(idx)
, validator(validator)
{
}

PathParamValidatorInfo(const PathParamValidatorInfo& other) = default;

PathParamValidatorInfo& operator=(const PathParamValidatorInfo& other)
{
if (this == &other) {
return *this;
}
idx = other.idx;
validator = other.validator;
return *this;
}

size_t idx = std::string::npos;
PathParamValidator* validator = nullptr;
};

struct QueryParamValidatorInfo
{
std::string name;
QueryParamValidator* validator;
QueryParamValidatorInfo(std::string name, QueryParamValidator* validator)
: name(std::move(name))
, validator(validator)
{
}

QueryParamValidatorInfo(const QueryParamValidatorInfo& other) = default;

QueryParamValidatorInfo& operator=(const QueryParamValidatorInfo& other)
{
if (this == &other) {
return *this;
}
name = other.name;
validator = other.validator;
return *this;
}

std::string name{};
QueryParamValidator* validator = nullptr;

~QueryParamValidatorInfo()
{
}
};

BodyValidator* body_validator_ = nullptr;
std::vector<PathParamValidatorInfo> path_param_validators_;
std::vector<QueryParamValidatorInfo> query_param_validators_;
std::unordered_map<std::string, HeaderParamValidator*> header_param_validators_;
std::vector<PathParamValidatorInfo> path_param_validators_{};
std::vector<QueryParamValidatorInfo> query_param_validators_{};
std::unordered_map<std::string, HeaderParamValidator*> header_param_validators_{};

static std::unordered_map<std::string, int> GetPathParamIndices(const std::string& path);
static std::unordered_map<std::string, size_t> GetPathParamIndices(const std::string& path);
};

#endif // OAS_VALIDATORS_HPP
10 changes: 5 additions & 5 deletions src/deserializers/base_deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ 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;
for (size_t i = 0; i < 256; ++i) {
table[i] = (i >= '0' && i <= '9') ? static_cast<char>(i - '0')
: (i >= 'A' && i <= 'F') ? static_cast<char>(i - 'A' + 10)
: (i >= 'a' && i <= 'f') ? static_cast<char>(i - 'a' + 10)
: static_cast<char>(-1);
}
return table;
}();
2 changes: 1 addition & 1 deletion src/deserializers/content_deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ std::string ContentDeserializer::Deserialize(const char* beg, const char* const
}

std::string ret;
ret.reserve(end - cursor);
ret.reserve(static_cast<std::string::size_type>(end - cursor));

while (cursor < end) {
char c = *cursor++;
Expand Down
4 changes: 4 additions & 0 deletions src/deserializers/object_deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ std::string ObjectDeserializer::Deserialize(const char* beg, const char* const e
case PrimitiveType::STRING:
DeserializeString(cursor, end, vk_separator_, ret);
break;
default:
throw DeserializationException("Invalid primitive type for '" + param_name_ + "'");
}
if (*cursor == vk_separator_) {
ret.push_back(',');
Expand Down Expand Up @@ -97,6 +99,8 @@ std::string ObjectDeserializer::Deserialize(const char* beg, const char* const e
case PrimitiveType::STRING:
DeserializeString(cursor, end, vk_separator_, ret);
break;
default:
throw DeserializationException("Invalid primitive type for '" + param_name_ + "'");
}
if (*cursor == vk_separator_) {
ret.push_back(',');
Expand Down
2 changes: 2 additions & 0 deletions src/deserializers/primitive_deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ std::string PrimitiveDeserializer::Deserialize(const char* beg, const char* cons
case PrimitiveType::STRING:
DeserializeString(cursor, end, ret);
break;
default:
throw DeserializationException("Invalid primitive type for '" + param_name_ + "'");
}

CheckEnd(cursor, end);
Expand Down
16 changes: 16 additions & 0 deletions src/oas_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@ OASValidator::OASValidator(const std::string& oas_specs)
{
}

OASValidator::OASValidator(const OASValidator& other)
: impl_(new OASValidatorImp(*other.impl_))
{
}

OASValidator& OASValidator::operator=(const OASValidator& other)
{
if (this == &other) {
return *this;
}

delete impl_;
impl_ = new OASValidatorImp(*other.impl_);
return *this;
}

ValidationError OASValidator::ValidateRoute(const std::string& method, const std::string& http_path,
std::string& error_msg)
{
Expand Down
2 changes: 1 addition & 1 deletion src/oas_validator_imp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ ValidationError OASValidatorImp::GetValidators(const std::string& method, const
return ValidationError::NONE;
}

std::vector<std::string> OASValidatorImp::Split(const std::string &str)
std::vector<std::string> OASValidatorImp::Split(const std::string& str)
{
std::vector<std::string> tokens;
std::string token;
Expand Down
48 changes: 41 additions & 7 deletions src/utils/path_trie.cpp
Original file line number Diff line number Diff line change
@@ -1,12 +1,46 @@
/*
* Copyright (c) 2023 Muhammad Nawaz
* Licensed under the MIT License. See LICENSE file for more information.
*/
// [ END OF LICENSE 7982e23aed1dc8eda1fdc979fee617354ae998dc ]

#include "utils/path_trie.hpp"
#include "utils/common.hpp"

PathTrie::PathTrie()
: root_(new Node())
{
}

PathTrie::PathTrie(const PathTrie& other)
: root_(nullptr)
{
if (other.root_) {
root_ = new Node(*other.root_);
CopyNode(root_, other.root_);
}
}

PathTrie& PathTrie::operator=(const PathTrie& other)
{
if (this == &other)
return *this;

DeleteNode(root_);
if (other.root_) {
root_ = new Node(*other.root_);
CopyNode(root_, other.root_);
} else {
root_ = nullptr;
}

return *this;
}

void PathTrie::CopyNode(Node*& thisNode, Node* otherNode)
{
if (otherNode) {
thisNode = new Node(*otherNode);
for (const auto& child : otherNode->children) {
CopyNode(thisNode->children[child.first], child.second);
}
}
}

void PathTrie::Insert(const std::string& path)
{
auto* node = root_;
Expand Down Expand Up @@ -121,4 +155,4 @@ void PathTrie::DeleteNode(Node* node)
}
delete node;
#endif // LUA_OAS_VALIDATOR
}
}
Loading

0 comments on commit d477ed4

Please sign in to comment.