From ce7a3316d8211ea6447f877e9dd6cc3b3c58d153 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Mon, 15 Nov 2021 02:40:55 +0200 Subject: [PATCH 1/2] QUERY: fix default value type (#411). QueryParams/Headers: use unordered_multimap (#402) --- .../codegen/api_controller/base_define.hpp | 19 +++++++++++-------- .../core/data/mapping/type/Primitive.cpp | 2 +- .../core/data/mapping/type/Primitive.hpp | 11 +++++++++-- src/oatpp/core/data/share/LazyStringMap.hpp | 3 +-- src/oatpp/network/Url.hpp | 4 ++-- src/oatpp/web/protocol/http/Http.hpp | 6 +++--- test/oatpp/web/app/Controller.hpp | 2 +- 7 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/oatpp/codegen/api_controller/base_define.hpp b/src/oatpp/codegen/api_controller/base_define.hpp index 21e90505..06db803a 100644 --- a/src/oatpp/codegen/api_controller/base_define.hpp +++ b/src/oatpp/codegen/api_controller/base_define.hpp @@ -187,14 +187,17 @@ if(!__param_validation_check_##NAME){ \ } #define OATPP_MACRO_API_CONTROLLER_QUERY_3(TYPE, NAME, QUALIFIER, DEFAULT) \ -const auto& __param_str_val_##NAME = __request->getQueryParameter(QUALIFIER, DEFAULT); \ -bool __param_validation_check_##NAME; \ -const auto& NAME = ApiController::TypeInterpretation::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \ -if(!__param_validation_check_##NAME){ \ - return ApiController::handleError(Status::CODE_400, \ - oatpp::String("Invalid QUERY parameter '") + \ - QUALIFIER + \ - "'. Expected type is '" #TYPE "'"); \ +TYPE NAME = DEFAULT; \ +const auto& __param_str_val_##NAME = __request->getQueryParameter(QUALIFIER); \ +if(__param_str_val_##NAME) { \ + bool __param_validation_check_##NAME; \ + NAME = ApiController::TypeInterpretation::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \ + if(!__param_validation_check_##NAME){ \ + return ApiController::handleError(Status::CODE_400, \ + oatpp::String("Invalid QUERY parameter '") + \ + QUALIFIER + \ + "'. Expected type is '" #TYPE "'"); \ + } \ } #define OATPP_MACRO_API_CONTROLLER_QUERY(TYPE, PARAM_LIST) \ diff --git a/src/oatpp/core/data/mapping/type/Primitive.cpp b/src/oatpp/core/data/mapping/type/Primitive.cpp index 731760de..347cd019 100644 --- a/src/oatpp/core/data/mapping/type/Primitive.cpp +++ b/src/oatpp/core/data/mapping/type/Primitive.cpp @@ -79,7 +79,7 @@ bool String::equalsCI_ASCII(const char* other) { return ciLabel == other; } -std::string String::getValue(const std::string& defaultValue) { +std::string String::getValue(const std::string& defaultValue) const { if(m_ptr) { return *m_ptr; } diff --git a/src/oatpp/core/data/mapping/type/Primitive.hpp b/src/oatpp/core/data/mapping/type/Primitive.hpp index af3c7b66..ec2496e3 100644 --- a/src/oatpp/core/data/mapping/type/Primitive.hpp +++ b/src/oatpp/core/data/mapping/type/Primitive.hpp @@ -218,7 +218,7 @@ public: * @param defaultValue - value to return in case stored value is `nullptr`. * @return - value or `defaultValue` if stored value is `nullptr`. */ - std::string getValue(const std::string& defaultValue); + std::string getValue(const std::string& defaultValue) const; template::value, void>::type @@ -357,7 +357,7 @@ public: return *this->m_ptr; } - TValueType getValue(const TValueType& defaultValue) { + TValueType getValue(const TValueType& defaultValue) const { if(this->m_ptr) { return *this->m_ptr; } @@ -436,6 +436,13 @@ public: return false; } + bool getValue(bool defaultValue) const { + if(this->m_ptr) { + return *this->m_ptr; + } + return defaultValue; + } + }; /** diff --git a/src/oatpp/core/data/share/LazyStringMap.hpp b/src/oatpp/core/data/share/LazyStringMap.hpp index e86aca9c..049dd886 100644 --- a/src/oatpp/core/data/share/LazyStringMap.hpp +++ b/src/oatpp/core/data/share/LazyStringMap.hpp @@ -28,7 +28,6 @@ #include "./MemoryLabel.hpp" #include "oatpp/core/concurrency/SpinLock.hpp" -#include #include namespace oatpp { namespace data { namespace share { @@ -337,7 +336,7 @@ using LazyStringMap = LazyStringMapTemplate> * Convenience template for &l:LazyStringMapTemplate;. Based on `std::unordered_map`. */ template -using LazyStringMultimap = LazyStringMapTemplate>; +using LazyStringMultimap = LazyStringMapTemplate>; }}} diff --git a/src/oatpp/network/Url.hpp b/src/oatpp/network/Url.hpp index 147c32f3..bad64300 100644 --- a/src/oatpp/network/Url.hpp +++ b/src/oatpp/network/Url.hpp @@ -43,9 +43,9 @@ public: typedef oatpp::data::share::StringKeyLabel StringKeyLabel; public: /** - * Parameters - map string to string. + * Parameters - map string to string. &id:oatpp::data::share::LazyStringMultimap;. */ - typedef oatpp::data::share::LazyStringMap Parameters; + typedef oatpp::data::share::LazyStringMultimap Parameters; public: diff --git a/src/oatpp/web/protocol/http/Http.hpp b/src/oatpp/web/protocol/http/Http.hpp index fd860b04..2c5979dd 100644 --- a/src/oatpp/web/protocol/http/Http.hpp +++ b/src/oatpp/web/protocol/http/Http.hpp @@ -40,15 +40,15 @@ namespace oatpp { namespace web { namespace protocol { namespace http { /** * Typedef for headers map. Headers map key is case-insensitive. - * For more info see &id:oatpp::data::share::LazyStringMap;. + * For more info see &id:oatpp::data::share::LazyStringMultimap;. */ typedef oatpp::data::share::LazyStringMultimap Headers; /** * Typedef for query parameters map. - * For more info see &id:oatpp::data::share::LazyStringMap;. + * For more info see &id:oatpp::data::share::LazyStringMultimap;. */ -typedef oatpp::data::share::LazyStringMap QueryParams; +typedef oatpp::data::share::LazyStringMultimap QueryParams; /** * Http status. diff --git a/test/oatpp/web/app/Controller.hpp b/test/oatpp/web/app/Controller.hpp index b9f995ca..692195cd 100644 --- a/test/oatpp/web/app/Controller.hpp +++ b/test/oatpp/web/app/Controller.hpp @@ -119,7 +119,7 @@ public: } ENDPOINT("GET", "queries/optional", getWithOptQueries, - QUERY(String, name, "name", "Default"), QUERY(Int32, age, "age", "101")) { + QUERY(String, name, "name", "Default"), QUERY(Int32, age, "age", 101)) { auto dto = TestDto::createShared(); dto->testValue = "name=" + name + "&age=" + oatpp::utils::conversion::int32ToStr(*age); return createDtoResponse(Status::CODE_200, dto); From c2b026f87a38a450a590640b20f41fe0c7fbc798 Mon Sep 17 00:00:00 2001 From: lganzzzo Date: Mon, 15 Nov 2021 02:47:22 +0200 Subject: [PATCH 2/2] BufferBody: check buffer for nullptr. --- src/oatpp/web/protocol/http/outgoing/BufferBody.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/oatpp/web/protocol/http/outgoing/BufferBody.cpp b/src/oatpp/web/protocol/http/outgoing/BufferBody.cpp index a6134f08..0fe6811b 100644 --- a/src/oatpp/web/protocol/http/outgoing/BufferBody.cpp +++ b/src/oatpp/web/protocol/http/outgoing/BufferBody.cpp @@ -27,7 +27,7 @@ namespace oatpp { namespace web { namespace protocol { namespace http { namespace outgoing { BufferBody::BufferBody(const oatpp::String &buffer, const data::share::StringKeyLabel &contentType) - : m_buffer(buffer) + : m_buffer(buffer ? buffer : "") , m_contentType(contentType) , m_inlineData((void*) m_buffer->data(), m_buffer->size()) {}