From 4eab5e0c70d38cd87f2e00b535c64007a364ac17 Mon Sep 17 00:00:00 2001 From: Muhammad Nawaz Date: Fri, 26 Jan 2024 18:12:56 +0500 Subject: [PATCH] Fix for URL with `?` but no query parameter --- data/openAPI_example.json | 19 +++++++++++++++++++ src/validators/validators_store.cpp | 7 ------- test/unittest/src/oas_validator.cpp | 3 +++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/data/openAPI_example.json b/data/openAPI_example.json index d6a3730..beb6174 100644 --- a/data/openAPI_example.json +++ b/data/openAPI_example.json @@ -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": [ diff --git a/src/validators/validators_store.cpp b/src/validators/validators_store.cpp index 6502009..5002cf7 100644 --- a/src/validators/validators_store.cpp +++ b/src/validators/validators_store.cpp @@ -58,13 +58,6 @@ ValidationError ValidatorsStore::ValidatePathParams(std::unordered_mapGetErrHeader() + 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 starts; std::unordered_map start_map; for (const auto& param_validator : query_param_validators_) { diff --git a/test/unittest/src/oas_validator.cpp b/test/unittest/src/oas_validator.cpp index bf21143..fc13718 100644 --- a/test/unittest/src/oas_validator.cpp +++ b/test/unittest/src/oas_validator.cpp @@ -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¶m2=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)