Skip to content

Commit 34b9ae7

Browse files
committed
AC-465: Allow to configure input limit for RESTful endpoints
1 parent d1cef6c commit 34b9ae7

File tree

4 files changed

+66
-37
lines changed

4 files changed

+66
-37
lines changed

app/code/Magento/Webapi/Controller/Rest/InputParamsResolver.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\Webapi\Exception;
1818
use Magento\Framework\Webapi\ServiceInputProcessor;
1919
use Magento\Framework\Webapi\Rest\Request as RestRequest;
20+
use Magento\Framework\Webapi\Validator\EntityArrayValidator\InputArraySizeLimitValue;
2021
use Magento\Webapi\Controller\Rest\Router\Route;
2122

2223
/**
@@ -60,6 +61,11 @@ class InputParamsResolver
6061
*/
6162
private $methodsMap;
6263

64+
/**
65+
* @var InputArraySizeLimitValue
66+
*/
67+
private $inputArraySizeLimitValue;
68+
6369
/**
6470
* Initialize dependencies
6571
*
@@ -69,14 +75,16 @@ class InputParamsResolver
6975
* @param Router $router
7076
* @param RequestValidator $requestValidator
7177
* @param MethodsMap|null $methodsMap
78+
* @param InputArraySizeLimitValue|null $inputArraySizeLimitValue
7279
*/
7380
public function __construct(
7481
RestRequest $request,
7582
ParamsOverrider $paramsOverrider,
7683
ServiceInputProcessor $serviceInputProcessor,
7784
Router $router,
7885
RequestValidator $requestValidator,
79-
MethodsMap $methodsMap = null
86+
MethodsMap $methodsMap = null,
87+
?InputArraySizeLimitValue $inputArraySizeLimitValue = null
8088
) {
8189
$this->request = $request;
8290
$this->paramsOverrider = $paramsOverrider;
@@ -85,6 +93,8 @@ public function __construct(
8593
$this->requestValidator = $requestValidator;
8694
$this->methodsMap = $methodsMap ?: ObjectManager::getInstance()
8795
->get(MethodsMap::class);
96+
$this->inputArraySizeLimitValue = $inputArraySizeLimitValue ?? ObjectManager::getInstance()
97+
->get(InputArraySizeLimitValue::class);
8898
}
8999

90100
/**
@@ -97,12 +107,12 @@ public function resolve()
97107
{
98108
$this->requestValidator->validate();
99109
$route = $this->getRoute();
110+
$this->inputArraySizeLimitValue->set($route->getInputArraySizeLimit());
100111

101112
return $this->serviceInputProcessor->process(
102113
$route->getServiceClass(),
103114
$route->getServiceMethod(),
104115
$this->getInputData(),
105-
$route->getInputArraySizeLimit()
106116
);
107117
}
108118

app/code/Magento/Webapi/Controller/Soap/Request/Handler.php

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,19 @@
88

99
namespace Magento\Webapi\Controller\Soap\Request;
1010

11+
use InvalidArgumentException;
1112
use Magento\Framework\Api\ExtensibleDataInterface;
1213
use Magento\Framework\Api\MetadataObjectInterface;
1314
use Magento\Framework\Api\SimpleDataObjectConverter;
1415
use Magento\Framework\App\ObjectManager;
16+
use Magento\Framework\ObjectManagerInterface;
1517
use Magento\Framework\Webapi\Authorization;
1618
use Magento\Framework\Exception\AuthorizationException;
1719
use Magento\Framework\Reflection\DataObjectProcessor;
1820
use Magento\Framework\Webapi\ServiceInputProcessor;
1921
use Magento\Framework\Webapi\Request as SoapRequest;
2022
use Magento\Framework\Webapi\Exception as WebapiException;
23+
use Magento\Framework\Webapi\Validator\EntityArrayValidator\InputArraySizeLimitValue;
2124
use Magento\Webapi\Controller\Rest\ParamsOverrider;
2225
use Magento\Webapi\Model\Soap\Config as SoapConfig;
2326
use Magento\Framework\Reflection\MethodsMap;
@@ -40,7 +43,7 @@ class Handler
4043
protected $_request;
4144

4245
/**
43-
* @var \Magento\Framework\ObjectManagerInterface
46+
* @var ObjectManagerInterface
4447
*/
4548
protected $_objectManager;
4649

@@ -79,29 +82,36 @@ class Handler
7982
*/
8083
private $paramsOverrider;
8184

85+
/**
86+
* @var InputArraySizeLimitValue
87+
*/
88+
private $inputArraySizeLimitValue;
89+
8290
/**
8391
* Initialize dependencies.
8492
*
8593
* @param SoapRequest $request
86-
* @param \Magento\Framework\ObjectManagerInterface $objectManager
94+
* @param ObjectManagerInterface $objectManager
8795
* @param SoapConfig $apiConfig
8896
* @param Authorization $authorization
8997
* @param SimpleDataObjectConverter $dataObjectConverter
9098
* @param ServiceInputProcessor $serviceInputProcessor
9199
* @param DataObjectProcessor $dataObjectProcessor
92100
* @param MethodsMap $methodsMapProcessor
93101
* @param ParamsOverrider|null $paramsOverrider
102+
* @param InputArraySizeLimitValue|null $inputArraySizeLimitValue
94103
*/
95104
public function __construct(
96105
SoapRequest $request,
97-
\Magento\Framework\ObjectManagerInterface $objectManager,
106+
ObjectManagerInterface $objectManager,
98107
SoapConfig $apiConfig,
99108
Authorization $authorization,
100109
SimpleDataObjectConverter $dataObjectConverter,
101110
ServiceInputProcessor $serviceInputProcessor,
102111
DataObjectProcessor $dataObjectProcessor,
103112
MethodsMap $methodsMapProcessor,
104-
?ParamsOverrider $paramsOverrider = null
113+
?ParamsOverrider $paramsOverrider = null,
114+
?InputArraySizeLimitValue $inputArraySizeLimitValue = null
105115
) {
106116
$this->_request = $request;
107117
$this->_objectManager = $objectManager;
@@ -112,6 +122,8 @@ public function __construct(
112122
$this->_dataObjectProcessor = $dataObjectProcessor;
113123
$this->methodsMapProcessor = $methodsMapProcessor;
114124
$this->paramsOverrider = $paramsOverrider ?? ObjectManager::getInstance()->get(ParamsOverrider::class);
125+
$this->inputArraySizeLimitValue = $inputArraySizeLimitValue ?? ObjectManager::getInstance()
126+
->get(InputArraySizeLimitValue::class);
115127
}
116128

117129
/**
@@ -179,12 +191,12 @@ private function prepareOperationInput(string $serviceClass, array $methodMetada
179191
$arguments = reset($arguments);
180192
$arguments = $this->_dataObjectConverter->convertStdObjectToArray($arguments, true);
181193
$arguments = $this->paramsOverrider->override($arguments, $methodMetadata[ServiceMetadata::KEY_ROUTE_PARAMS]);
194+
$this->inputArraySizeLimitValue->set($methodMetadata[ServiceMetadata::KEY_INPUT_ARRAY_SIZE_LIMIT]);
182195

183196
return $this->serviceInputProcessor->process(
184197
$serviceClass,
185198
$methodMetadata[ServiceMetadata::KEY_METHOD],
186-
$arguments,
187-
$methodMetadata[ServiceMetadata::KEY_INPUT_ARRAY_SIZE_LIMIT]
199+
$arguments
188200
);
189201
}
190202

@@ -195,8 +207,9 @@ private function prepareOperationInput(string $serviceClass, array $methodMetada
195207
* @param string $serviceMethod
196208
* @param array $arguments
197209
* @return array
198-
* @deprecated 100.3.2
210+
* @throws WebapiException
199211
* @see Handler::prepareOperationInput()
212+
* @deprecated 100.3.2
200213
*/
201214
protected function _prepareRequestData($serviceClass, $serviceMethod, $arguments)
202215
{
@@ -214,7 +227,7 @@ protected function _prepareRequestData($serviceClass, $serviceMethod, $arguments
214227
* @param string $serviceClassName
215228
* @param string $serviceMethodName
216229
* @return array
217-
* @throws \InvalidArgumentException
230+
* @throws InvalidArgumentException
218231
*/
219232
protected function _prepareResponseData($data, $serviceClassName, $serviceMethodName)
220233
{
@@ -241,7 +254,7 @@ protected function _prepareResponseData($data, $serviceClassName, $serviceMethod
241254
} elseif (is_scalar($data) || $data === null) {
242255
$result = $data;
243256
} else {
244-
throw new \InvalidArgumentException("Service returned result in invalid format.");
257+
throw new InvalidArgumentException("Service returned result in invalid format.");
245258
}
246259
return [self::RESULT_NODE_NAME => $result];
247260
}

app/code/Magento/WebapiAsync/Controller/Rest/Asynchronous/InputParamsResolver.php

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,14 @@
88

99
namespace Magento\WebapiAsync\Controller\Rest\Asynchronous;
1010

11+
use Magento\Framework\App\ObjectManager;
1112
use Magento\Framework\Exception\AuthorizationException;
1213
use Magento\Framework\Exception\InputException;
14+
use Magento\Framework\Exception\LocalizedException;
1315
use Magento\Framework\Webapi\Exception;
1416
use Magento\Framework\Webapi\Rest\Request as RestRequest;
1517
use Magento\Framework\Webapi\ServiceInputProcessor;
18+
use Magento\Framework\Webapi\Validator\EntityArrayValidator\InputArraySizeLimitValue;
1619
use Magento\Webapi\Controller\Rest\InputParamsResolver as WebapiInputParamsResolver;
1720
use Magento\Webapi\Controller\Rest\ParamsOverrider;
1821
use Magento\Webapi\Controller\Rest\RequestValidator;
@@ -53,6 +56,11 @@ class InputParamsResolver
5356
*/
5457
private $isBulk;
5558

59+
/**
60+
* @var InputArraySizeLimitValue|null
61+
*/
62+
private $inputArraySizeLimitValue;
63+
5664
/**
5765
* Initialize dependencies.
5866
*
@@ -62,6 +70,7 @@ class InputParamsResolver
6270
* @param Router $router
6371
* @param RequestValidator $requestValidator
6472
* @param WebapiInputParamsResolver $inputParamsResolver
73+
* @param InputArraySizeLimitValue|null $inputArraySizeLimitValue
6574
* @param bool $isBulk
6675
*/
6776
public function __construct(
@@ -71,7 +80,8 @@ public function __construct(
7180
Router $router,
7281
RequestValidator $requestValidator,
7382
WebapiInputParamsResolver $inputParamsResolver,
74-
$isBulk = false
83+
bool $isBulk = false,
84+
?InputArraySizeLimitValue $inputArraySizeLimitValue = null
7585
) {
7686
$this->request = $request;
7787
$this->paramsOverrider = $paramsOverrider;
@@ -80,6 +90,8 @@ public function __construct(
8090
$this->requestValidator = $requestValidator;
8191
$this->inputParamsResolver = $inputParamsResolver;
8292
$this->isBulk = $isBulk;
93+
$this->inputArraySizeLimitValue = $inputArraySizeLimitValue ?? ObjectManager::getInstance()
94+
->get(InputArraySizeLimitValue::class);
8395
}
8496

8597
/**
@@ -91,20 +103,29 @@ public function __construct(
91103
* @return array
92104
* @throws InputException if no value is provided for required parameters
93105
* @throws Exception
94-
* @throws AuthorizationException
106+
* @throws AuthorizationException|LocalizedException
95107
*/
96108
public function resolve()
97109
{
98110
if ($this->isBulk === false) {
99111
return [$this->inputParamsResolver->resolve()];
100112
}
113+
101114
$this->requestValidator->validate();
102115
$webapiResolvedParams = [];
103116
$route = $this->getRoute();
117+
$routeServiceClass = $route->getServiceClass();
118+
$routeServiceMethod = $route->getServiceMethod();
119+
$this->inputArraySizeLimitValue->set($route->getInputArraySizeLimit());
104120

105121
foreach ($this->getInputData() as $key => $singleEntityParams) {
106-
$webapiResolvedParams[$key] = $this->resolveBulkItemParams($singleEntityParams, $route);
122+
$webapiResolvedParams[$key] = $this->resolveBulkItemParams(
123+
$singleEntityParams,
124+
$routeServiceClass,
125+
$routeServiceMethod
126+
);
107127
}
128+
108129
return $webapiResolvedParams;
109130
}
110131

@@ -132,6 +153,7 @@ public function getInputData()
132153
* Returns route.
133154
*
134155
* @return Route
156+
* @throws Exception
135157
*/
136158
public function getRoute()
137159
{
@@ -148,17 +170,13 @@ public function getRoute()
148170
* we don't need to merge body params with url params and use only body params
149171
*
150172
* @param array $inputData data to send to method in key-value format
151-
* @param Route $route
173+
* @param string $serviceClass route Service Class
174+
* @param string $serviceMethod route Service Method
152175
* @return array list of parameters that can be used to call the service method
153-
* @throws Exception
176+
* @throws Exception|LocalizedException
154177
*/
155-
private function resolveBulkItemParams(array $inputData, Route $route): array
178+
private function resolveBulkItemParams(array $inputData, string $serviceClass, string $serviceMethod): array
156179
{
157-
return $this->serviceInputProcessor->process(
158-
$route->getServiceClass(),
159-
$route->getServiceMethod(),
160-
$inputData,
161-
$route->getInputArraySizeLimit()
162-
);
180+
return $this->serviceInputProcessor->process($serviceClass, $serviceMethod, $inputData);
163181
}
164182
}

lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use Magento\Framework\Webapi\Exception as WebapiException;
2424
use Magento\Framework\Webapi\CustomAttribute\PreprocessorInterface;
2525
use Laminas\Code\Reflection\ClassReflection;
26-
use Magento\Framework\Webapi\Validator\EntityArrayValidator\InputArraySizeLimitValue;
2726
use Magento\Framework\Webapi\Validator\IOLimit\DefaultPageSizeSetter;
2827
use Magento\Framework\Webapi\Validator\ServiceInputValidatorInterface;
2928

@@ -103,11 +102,6 @@ class ServiceInputProcessor implements ServicePayloadConverterInterface
103102
*/
104103
private $defaultPageSizeSetter;
105104

106-
/**
107-
* @var InputArraySizeLimitValue
108-
*/
109-
private $inputArraySizeLimitValue;
110-
111105
/**
112106
* Initialize dependencies.
113107
*
@@ -122,7 +116,6 @@ class ServiceInputProcessor implements ServicePayloadConverterInterface
122116
* @param ServiceInputValidatorInterface|null $serviceInputValidator
123117
* @param int $defaultPageSize
124118
* @param DefaultPageSizeSetter|null $defaultPageSizeSetter
125-
* @param InputArraySizeLimitValue|null $inputArraySizeLimitValue
126119
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
127120
*/
128121
public function __construct(
@@ -136,8 +129,7 @@ public function __construct(
136129
array $customAttributePreprocessors = [],
137130
ServiceInputValidatorInterface $serviceInputValidator = null,
138131
int $defaultPageSize = 20,
139-
?DefaultPageSizeSetter $defaultPageSizeSetter = null,
140-
?InputArraySizeLimitValue $inputArraySizeLimitValue = null
132+
?DefaultPageSizeSetter $defaultPageSizeSetter = null
141133
) {
142134
$this->typeProcessor = $typeProcessor;
143135
$this->objectManager = $objectManager;
@@ -154,8 +146,6 @@ public function __construct(
154146
$this->defaultPageSize = $defaultPageSize >= 10 ? $defaultPageSize : 10;
155147
$this->defaultPageSizeSetter = $defaultPageSizeSetter ?? ObjectManager::getInstance()
156148
->get(DefaultPageSizeSetter::class);
157-
$this->inputArraySizeLimitValue = $inputArraySizeLimitValue ?? ObjectManager::getInstance()
158-
->get(InputArraySizeLimitValue::class);
159149
}
160150

161151
/**
@@ -186,16 +176,14 @@ private function getNameFinder()
186176
* @param string $serviceClassName name of the service class that we are trying to call
187177
* @param string $serviceMethodName name of the method that we are trying to call
188178
* @param array $inputArray data to send to method in key-value format
189-
* @param int|null $inputArraySizeLimit size limit for input array
190179
* @return array list of parameters that can be used to call the service method
191180
* @throws Exception
192181
* @throws LocalizedException
193182
*/
194-
public function process($serviceClassName, $serviceMethodName, array $inputArray, ?int $inputArraySizeLimit = null)
183+
public function process($serviceClassName, $serviceMethodName, array $inputArray)
195184
{
196185
$inputData = [];
197186
$inputError = [];
198-
$this->inputArraySizeLimitValue->set($inputArraySizeLimit);
199187

200188
foreach ($this->methodsMap->getMethodParams($serviceClassName, $serviceMethodName) as $param) {
201189
$paramName = $param[MethodsMap::METHOD_META_NAME];

0 commit comments

Comments
 (0)