diff options
author | PV99 <pridhvi.vegesna@mongodb.com> | 2020-07-14 22:34:25 +0000 |
---|---|---|
committer | Evergreen Agent <no-reply@evergreen.mongodb.com> | 2020-07-17 00:43:31 +0000 |
commit | cfd99814027a4972be64742b3dd3de2695c88b84 (patch) | |
tree | 8f5ad9ad0a3a08d8053a2bfb0f0d947dcac8dccb | |
parent | 8b9af875a947b862c6bf82754ae7eaf582a9ab3f (diff) | |
download | mongo-cfd99814027a4972be64742b3dd3de2695c88b84.tar.gz |
SERVER-48877 Create an APIParameters class
-rw-r--r-- | src/mongo/db/initialize_api_parameters.cpp | 47 | ||||
-rw-r--r-- | src/mongo/db/initialize_api_parameters.h | 44 | ||||
-rw-r--r-- | src/mongo/db/service_entry_point_common.cpp | 3 | ||||
-rw-r--r-- | src/mongo/s/commands/strategy.cpp | 3 |
4 files changed, 87 insertions, 10 deletions
diff --git a/src/mongo/db/initialize_api_parameters.cpp b/src/mongo/db/initialize_api_parameters.cpp index e6f31cbb8c5..c78f1d130f4 100644 --- a/src/mongo/db/initialize_api_parameters.cpp +++ b/src/mongo/db/initialize_api_parameters.cpp @@ -31,22 +31,55 @@ namespace mongo { -APIParametersFromClient initializeAPIParameters(const BSONObj& requestBody) { +const APIParametersFromClient initializeAPIParameters(const BSONObj& requestBody) { - auto vAPI = APIParametersFromClient::parse("APIParametersFromClient"_sd, requestBody); + auto apiParamsFromClient = + APIParametersFromClient::parse("APIParametersFromClient"_sd, requestBody); - if (vAPI.getApiDeprecationErrors() || vAPI.getApiStrict()) { + if (apiParamsFromClient.getApiDeprecationErrors() || apiParamsFromClient.getApiStrict()) { uassert(4886600, "Provided apiStrict and/or apiDeprecationErrors without passing apiVersion", - vAPI.getApiVersion()); + apiParamsFromClient.getApiVersion()); } - if (vAPI.getApiVersion()) { + if (apiParamsFromClient.getApiVersion()) { uassert(ErrorCodes::APIVersionError, "API version must be \"1\"", - "1" == vAPI.getApiVersion().value()); + "1" == apiParamsFromClient.getApiVersion().value()); } - return vAPI; + + return apiParamsFromClient; +} + +const OperationContext::Decoration<APIParameters> handle = + OperationContext::declareDecoration<APIParameters>(); + +APIParameters& APIParameters::get(OperationContext* opCtx) { + return handle(opCtx); +} + +APIParameters::APIParameters() + : _apiVersion("1"), _apiStrict(false), _apiDeprecationErrors(false) {} + +APIParameters APIParameters::fromClient(const APIParametersFromClient& apiParamsFromClient) { + APIParameters apiParameters = APIParameters(); + auto apiVersion = apiParamsFromClient.getApiVersion(); + auto apiStrict = apiParamsFromClient.getApiStrict(); + auto apiDeprecationErrors = apiParamsFromClient.getApiDeprecationErrors(); + + if (apiVersion) { + apiParameters.setAPIVersion(apiVersion.value()); + } + + if (apiStrict) { + apiParameters.setAPIStrict(apiStrict.value()); + } + + if (apiDeprecationErrors) { + apiParameters.setAPIDeprecationErrors(apiDeprecationErrors.value()); + } + + return apiParameters; } } // namespace mongo diff --git a/src/mongo/db/initialize_api_parameters.h b/src/mongo/db/initialize_api_parameters.h index 69d216fc7cc..a8c1ee141e4 100644 --- a/src/mongo/db/initialize_api_parameters.h +++ b/src/mongo/db/initialize_api_parameters.h @@ -30,6 +30,7 @@ #pragma once #include "mongo/db/initialize_api_parameters_gen.h" +#include "mongo/db/operation_context.h" namespace mongo { @@ -37,6 +38,47 @@ namespace mongo { * Parses a command's API Version parameters from a request and stores the apiVersion, apiStrict, * and apiDeprecationErrors fields. */ -APIParametersFromClient initializeAPIParameters(const BSONObj& requestBody); +const APIParametersFromClient initializeAPIParameters(const BSONObj& requestBody); + +/** + * Decorates operation context with methods to retrieve apiVersion, apiStrict, and + * apiDeprecationErrors. + */ +class APIParameters { + +public: + APIParameters(); + static APIParameters& get(OperationContext* opCtx); + static APIParameters fromClient(const APIParametersFromClient& apiParamsFromClient); + + const StringData getAPIVersion() const { + return _apiVersion; + } + + void setAPIVersion(StringData apiVersion) { + _apiVersion = apiVersion; + } + + const bool getAPIStrict() const { + return _apiStrict; + } + + void setAPIStrict(bool apiStrict) { + _apiStrict = apiStrict; + } + + const bool getAPIDeprecationErrors() const { + return _apiDeprecationErrors; + } + + void setAPIDeprecationErrors(bool apiDeprecationErrors) { + _apiDeprecationErrors = apiDeprecationErrors; + } + +private: + StringData _apiVersion; + bool _apiStrict; + bool _apiDeprecationErrors; +}; } // namespace mongo
\ No newline at end of file diff --git a/src/mongo/db/service_entry_point_common.cpp b/src/mongo/db/service_entry_point_common.cpp index 4dc77547321..de6a27c556b 100644 --- a/src/mongo/db/service_entry_point_common.cpp +++ b/src/mongo/db/service_entry_point_common.cpp @@ -933,7 +933,8 @@ void execCommandDatabase(OperationContext* opCtx, auto const replCoord = repl::ReplicationCoordinator::get(opCtx); - auto apiParams = initializeAPIParameters(request.body); + auto const apiParamsFromClient = initializeAPIParameters(request.body); + APIParameters::get(opCtx) = APIParameters::fromClient(apiParamsFromClient); sessionOptions = initializeOperationSessionInfo( opCtx, diff --git a/src/mongo/s/commands/strategy.cpp b/src/mongo/s/commands/strategy.cpp index 35d00622d7c..63c62d6641f 100644 --- a/src/mongo/s/commands/strategy.cpp +++ b/src/mongo/s/commands/strategy.cpp @@ -347,7 +347,8 @@ void runCommand(OperationContext* opCtx, // Fill out all currentOp details. CurOp::get(opCtx)->setGenericOpRequestDetails(opCtx, nss, command, request.body, opType); - auto apiParams = initializeAPIParameters(request.body); + auto const apiParamsFromClient = initializeAPIParameters(request.body); + APIParameters::get(opCtx) = APIParameters::fromClient(apiParamsFromClient); auto osi = initializeOperationSessionInfo(opCtx, request.body, |