Skip to content

Commit e26e723

Browse files
Merge branch 'AC-465' into 2.4-bugfixes-121621
2 parents 7900123 + 76e7d37 commit e26e723

File tree

31 files changed

+912
-261
lines changed

31 files changed

+912
-261
lines changed

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

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,25 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\Webapi\Controller\Rest;
810

911
use Magento\Framework\Api\SimpleDataObjectConverter;
1012
use Magento\Framework\App\ObjectManager;
13+
use Magento\Framework\Exception\AuthorizationException;
14+
use Magento\Framework\Exception\InputException;
15+
use Magento\Framework\Exception\LocalizedException;
1116
use Magento\Framework\Reflection\MethodsMap;
1217
use Magento\Framework\Webapi\Exception;
1318
use Magento\Framework\Webapi\ServiceInputProcessor;
1419
use Magento\Framework\Webapi\Rest\Request as RestRequest;
20+
use Magento\Framework\Webapi\Validator\EntityArrayValidator\InputArraySizeLimitValue;
1521
use Magento\Webapi\Controller\Rest\Router\Route;
1622

1723
/**
1824
* This class is responsible for retrieving resolved input data
25+
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
1926
*/
2027
class InputParamsResolver
2128
{
@@ -54,6 +61,11 @@ class InputParamsResolver
5461
*/
5562
private $methodsMap;
5663

64+
/**
65+
* @var InputArraySizeLimitValue
66+
*/
67+
private $inputArraySizeLimitValue;
68+
5769
/**
5870
* Initialize dependencies
5971
*
@@ -63,14 +75,16 @@ class InputParamsResolver
6375
* @param Router $router
6476
* @param RequestValidator $requestValidator
6577
* @param MethodsMap|null $methodsMap
78+
* @param InputArraySizeLimitValue|null $inputArraySizeLimitValue
6679
*/
6780
public function __construct(
6881
RestRequest $request,
6982
ParamsOverrider $paramsOverrider,
7083
ServiceInputProcessor $serviceInputProcessor,
7184
Router $router,
7285
RequestValidator $requestValidator,
73-
MethodsMap $methodsMap = null
86+
MethodsMap $methodsMap = null,
87+
?InputArraySizeLimitValue $inputArraySizeLimitValue = null
7488
) {
7589
$this->request = $request;
7690
$this->paramsOverrider = $paramsOverrider;
@@ -79,29 +93,34 @@ public function __construct(
7993
$this->requestValidator = $requestValidator;
8094
$this->methodsMap = $methodsMap ?: ObjectManager::getInstance()
8195
->get(MethodsMap::class);
96+
$this->inputArraySizeLimitValue = $inputArraySizeLimitValue ?? ObjectManager::getInstance()
97+
->get(InputArraySizeLimitValue::class);
8298
}
8399

84100
/**
85101
* Process and resolve input parameters
86102
*
87103
* @return array
88-
* @throws Exception
104+
* @throws Exception|AuthorizationException|LocalizedException
89105
*/
90106
public function resolve()
91107
{
92108
$this->requestValidator->validate();
93109
$route = $this->getRoute();
94-
$serviceMethodName = $route->getServiceMethod();
95-
$serviceClassName = $route->getServiceClass();
96-
$inputData = $this->getInputData();
110+
$this->inputArraySizeLimitValue->set($route->getInputArraySizeLimit());
97111

98-
return $this->serviceInputProcessor->process($serviceClassName, $serviceMethodName, $inputData);
112+
return $this->serviceInputProcessor->process(
113+
$route->getServiceClass(),
114+
$route->getServiceMethod(),
115+
$this->getInputData(),
116+
);
99117
}
100118

101119
/**
102120
* Get API input data
103121
*
104122
* @return array
123+
* @throws InputException|Exception
105124
*/
106125
public function getInputData()
107126
{
@@ -131,6 +150,7 @@ public function getInputData()
131150
* Retrieve current route.
132151
*
133152
* @return Route
153+
* @throws Exception
134154
*/
135155
public function getRoute()
136156
{
@@ -174,9 +194,9 @@ private function validateParameters(
174194
}
175195
}
176196
if (!empty($paramOverriders)) {
177-
throw new \UnexpectedValueException(
178-
__('The current request does not expect the next parameters: ' . implode(', ', $paramOverriders))
179-
);
197+
$message = 'The current request does not expect the next parameters: '
198+
. implode(', ', $paramOverriders);
199+
throw new \UnexpectedValueException(__($message)->__toString());
180200
}
181201
}
182202
}

app/code/Magento/Webapi/Controller/Rest/Router/Route.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
<?php
22
/**
3-
* Route to services available via REST API.
4-
*
53
* Copyright © Magento, Inc. All rights reserved.
64
* See COPYING.txt for license details.
75
*/
6+
7+
declare(strict_types=1);
8+
89
namespace Magento\Webapi\Controller\Rest\Router;
910

1011
use Magento\Framework\App\RequestInterface as Request;
1112
use Magento\Framework\App\RouterInterface;
1213

14+
/**
15+
* Route to services available via REST API.
16+
*/
1317
class Route implements RouterInterface
1418
{
1519
/**
@@ -47,6 +51,11 @@ class Route implements RouterInterface
4751
*/
4852
protected $route;
4953

54+
/**
55+
* @var int|null
56+
*/
57+
private $inputArraySizeLimit;
58+
5059
/**
5160
* @param string $route
5261
*/
@@ -251,4 +260,24 @@ public function getRoutePath()
251260
{
252261
return $this->route;
253262
}
263+
264+
/**
265+
* Get array size limit of input data
266+
*
267+
* @return int|null
268+
*/
269+
public function getInputArraySizeLimit(): ?int
270+
{
271+
return $this->inputArraySizeLimit;
272+
}
273+
274+
/**
275+
* Set array size limit of input data
276+
*
277+
* @param int|null $limit
278+
*/
279+
public function setInputArraySizeLimit(?int $limit): void
280+
{
281+
$this->inputArraySizeLimit = $limit;
282+
}
254283
}

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

Lines changed: 50 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44
* See COPYING.txt for license details.
55
*/
66

7+
declare(strict_types=1);
8+
79
namespace Magento\Webapi\Controller\Soap\Request;
810

11+
use InvalidArgumentException;
912
use Magento\Framework\Api\ExtensibleDataInterface;
1013
use Magento\Framework\Api\MetadataObjectInterface;
1114
use Magento\Framework\Api\SimpleDataObjectConverter;
1215
use Magento\Framework\App\ObjectManager;
16+
use Magento\Framework\ObjectManagerInterface;
1317
use Magento\Framework\Webapi\Authorization;
1418
use Magento\Framework\Exception\AuthorizationException;
1519
use Magento\Framework\Reflection\DataObjectProcessor;
1620
use Magento\Framework\Webapi\ServiceInputProcessor;
17-
use Magento\Framework\Webapi\Request as SoapRequest;
21+
use Magento\Framework\Webapi\Request as WebapiRequest;
1822
use Magento\Framework\Webapi\Exception as WebapiException;
23+
use Magento\Framework\Webapi\Validator\EntityArrayValidator\InputArraySizeLimitValue;
1924
use Magento\Webapi\Controller\Rest\ParamsOverrider;
2025
use Magento\Webapi\Model\Soap\Config as SoapConfig;
2126
use Magento\Framework\Reflection\MethodsMap;
@@ -30,45 +35,45 @@
3035
*/
3136
class Handler
3237
{
33-
const RESULT_NODE_NAME = 'result';
38+
public const RESULT_NODE_NAME = 'result';
3439

3540
/**
36-
* @var \Magento\Framework\Webapi\Request
41+
* @var WebapiRequest
3742
*/
3843
protected $_request;
3944

4045
/**
41-
* @var \Magento\Framework\ObjectManagerInterface
46+
* @var ObjectManagerInterface
4247
*/
4348
protected $_objectManager;
4449

4550
/**
46-
* @var \Magento\Webapi\Model\Soap\Config
51+
* @var SoapConfig
4752
*/
4853
protected $_apiConfig;
4954

5055
/**
51-
* @var \Magento\Framework\Webapi\Authorization
56+
* @var Authorization
5257
*/
5358
protected $authorization;
5459

5560
/**
56-
* @var \Magento\Framework\Api\SimpleDataObjectConverter
61+
* @var SimpleDataObjectConverter
5762
*/
5863
protected $_dataObjectConverter;
5964

6065
/**
61-
* @var \Magento\Framework\Webapi\ServiceInputProcessor
66+
* @var ServiceInputProcessor
6267
*/
6368
protected $serviceInputProcessor;
6469

6570
/**
66-
* @var \Magento\Framework\Reflection\DataObjectProcessor
71+
* @var DataObjectProcessor
6772
*/
6873
protected $_dataObjectProcessor;
6974

7075
/**
71-
* @var \Magento\Framework\Reflection\MethodsMap
76+
* @var MethodsMap
7277
*/
7378
protected $methodsMapProcessor;
7479

@@ -77,29 +82,37 @@ class Handler
7782
*/
7883
private $paramsOverrider;
7984

85+
/**
86+
* @var InputArraySizeLimitValue
87+
*/
88+
private $inputArraySizeLimitValue;
89+
8090
/**
8191
* Initialize dependencies.
8292
*
83-
* @param SoapRequest $request
84-
* @param \Magento\Framework\ObjectManagerInterface $objectManager
93+
* @param WebapiRequest $request
94+
* @param ObjectManagerInterface $objectManager
8595
* @param SoapConfig $apiConfig
8696
* @param Authorization $authorization
8797
* @param SimpleDataObjectConverter $dataObjectConverter
8898
* @param ServiceInputProcessor $serviceInputProcessor
8999
* @param DataObjectProcessor $dataObjectProcessor
90100
* @param MethodsMap $methodsMapProcessor
91101
* @param ParamsOverrider|null $paramsOverrider
102+
* @param InputArraySizeLimitValue|null $inputArraySizeLimitValue
103+
* @SuppressWarnings(PHPMD.ExcessiveParameterList)
92104
*/
93105
public function __construct(
94-
SoapRequest $request,
95-
\Magento\Framework\ObjectManagerInterface $objectManager,
106+
WebapiRequest $request,
107+
ObjectManagerInterface $objectManager,
96108
SoapConfig $apiConfig,
97109
Authorization $authorization,
98110
SimpleDataObjectConverter $dataObjectConverter,
99111
ServiceInputProcessor $serviceInputProcessor,
100112
DataObjectProcessor $dataObjectProcessor,
101113
MethodsMap $methodsMapProcessor,
102-
?ParamsOverrider $paramsOverrider = null
114+
?ParamsOverrider $paramsOverrider = null,
115+
?InputArraySizeLimitValue $inputArraySizeLimitValue = null
103116
) {
104117
$this->_request = $request;
105118
$this->_objectManager = $objectManager;
@@ -110,6 +123,8 @@ public function __construct(
110123
$this->_dataObjectProcessor = $dataObjectProcessor;
111124
$this->methodsMapProcessor = $methodsMapProcessor;
112125
$this->paramsOverrider = $paramsOverrider ?? ObjectManager::getInstance()->get(ParamsOverrider::class);
126+
$this->inputArraySizeLimitValue = $inputArraySizeLimitValue ?? ObjectManager::getInstance()
127+
->get(InputArraySizeLimitValue::class);
113128
}
114129

115130
/**
@@ -144,10 +159,24 @@ public function __call($operation, $arguments)
144159
}
145160
$service = $this->_objectManager->get($serviceClass);
146161
$inputData = $this->prepareOperationInput($serviceClass, $serviceMethodInfo, $arguments);
147-
$outputData = call_user_func_array([$service, $serviceMethod], $inputData);
162+
$outputData = $this->runServiceMethod($service, $serviceMethod, $inputData);
148163
return $this->_prepareResponseData($outputData, $serviceClass, $serviceMethod);
149164
}
150165

166+
/**
167+
* Runs service method
168+
*
169+
* @param object $service
170+
* @param string $serviceMethod
171+
* @param array $inputData
172+
* @return false|mixed
173+
*/
174+
private function runServiceMethod($service, $serviceMethod, $inputData)
175+
{
176+
// phpcs:ignore Magento2.Functions.DiscouragedFunction
177+
return call_user_func_array([$service, $serviceMethod], $inputData);
178+
}
179+
151180
/**
152181
* Convert arguments received from SOAP server to arguments to pass to a service.
153182
*
@@ -156,14 +185,14 @@ public function __call($operation, $arguments)
156185
* @param array $arguments
157186
* @return array
158187
* @throws WebapiException
159-
* @throws \Magento\Framework\Exception\InputException
160188
*/
161189
private function prepareOperationInput(string $serviceClass, array $methodMetadata, array $arguments): array
162190
{
163191
/** SoapServer wraps parameters into array. Thus this wrapping should be removed to get access to parameters. */
164192
$arguments = reset($arguments);
165193
$arguments = $this->_dataObjectConverter->convertStdObjectToArray($arguments, true);
166194
$arguments = $this->paramsOverrider->override($arguments, $methodMetadata[ServiceMetadata::KEY_ROUTE_PARAMS]);
195+
$this->inputArraySizeLimitValue->set($methodMetadata[ServiceMetadata::KEY_INPUT_ARRAY_SIZE_LIMIT]);
167196

168197
return $this->serviceInputProcessor->process(
169198
$serviceClass,
@@ -179,8 +208,9 @@ private function prepareOperationInput(string $serviceClass, array $methodMetada
179208
* @param string $serviceMethod
180209
* @param array $arguments
181210
* @return array
182-
* @deprecated 100.3.2
211+
* @throws WebapiException
183212
* @see Handler::prepareOperationInput()
213+
* @deprecated 100.3.2
184214
*/
185215
protected function _prepareRequestData($serviceClass, $serviceMethod, $arguments)
186216
{
@@ -198,7 +228,7 @@ protected function _prepareRequestData($serviceClass, $serviceMethod, $arguments
198228
* @param string $serviceClassName
199229
* @param string $serviceMethodName
200230
* @return array
201-
* @throws \InvalidArgumentException
231+
* @throws InvalidArgumentException
202232
*/
203233
protected function _prepareResponseData($data, $serviceClassName, $serviceMethodName)
204234
{
@@ -225,7 +255,7 @@ protected function _prepareResponseData($data, $serviceClassName, $serviceMethod
225255
} elseif (is_scalar($data) || $data === null) {
226256
$result = $data;
227257
} else {
228-
throw new \InvalidArgumentException("Service returned result in invalid format.");
258+
throw new InvalidArgumentException("Service returned result in invalid format.");
229259
}
230260
return [self::RESULT_NODE_NAME => $result];
231261
}

0 commit comments

Comments
 (0)