Skip to content

Commit

Permalink
Merge pull request #15 from nawaz1991/14-bug-exception-thrown-in-quer…
Browse files Browse the repository at this point in the history
…y-parameters-validation

Fix for URL with `?` but no query parameter
  • Loading branch information
nawaz1991 authored Jan 26, 2024
2 parents 3044181 + ce1c84e commit 39c182d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
19 changes: 19 additions & 0 deletions data/openAPI_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -1051,6 +1051,25 @@
}
}
},
"/test/query_optional": {
"get": {
"parameters": [
{
"name": "param",
"in": "query",
"required": false,
"schema": {
"type": "integer"
}
}
],
"responses": {
"200": {
"description": "Successfully received data."
}
}
}
},
"/test/complex_scenario1": {
"get": {
"parameters": [
Expand Down
7 changes: 0 additions & 7 deletions src/validators/validators_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ ValidationError ValidatorsStore::ValidatePathParams(std::unordered_map<size_t, P

ValidationError ValidatorsStore::ValidateQueryParams(const std::string& query, std::string& error_msg)
{
if (1 == query_param_validators_.size()) {
if (query.at(0) != '?') {
error_msg = query_param_validators_.front().validator->GetErrHeader() + R"("description": "Query string should start with '?'"}})";
return ValidationError::INVALID_QUERY_PARAM;
}
return query_param_validators_.front().validator->ValidateParam(query.data() + 1, query.data() + query.size(), error_msg);
}
std::set<size_t> starts;
std::unordered_map<std::string, size_t> start_map;
for (const auto& param_validator : query_param_validators_) {
Expand Down
3 changes: 3 additions & 0 deletions test/unittest/src/oas_validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ TEST_F(OASValidatorTest, ValidateQueryParam)
EXPECT_EQ(ValidationError::NONE, validator_->ValidateQueryParam("GET", "/test/query_integer_form_true?param=123", err_msg));
EXPECT_EQ(ValidationError::INVALID_QUERY_PARAM, validator_->ValidateQueryParam("GET", "/test/query_integer_form_true?param=123str", err_msg));
EXPECT_EQ(ValidationError::NONE, validator_->ValidateQueryParam("GET", "/test/query_two_integer_form_mixed?param1=123&param2=456", err_msg));
EXPECT_EQ(ValidationError::NONE, validator_->ValidateQueryParam("GET", "/test/query_optional", err_msg));
EXPECT_EQ(ValidationError::NONE, validator_->ValidateQueryParam("GET", "/test/query_optional?", err_msg));
EXPECT_EQ(ValidationError::NONE, validator_->ValidateQueryParam("GET", "/test/query_optional?param=10", err_msg));
}

TEST_F(OASValidatorTest, ValidateHeaders)
Expand Down

0 comments on commit 39c182d

Please sign in to comment.