Skip to content

More robust fix to https://github.com/magento-engcom/msi/issues/1524 #1545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

use Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterface;
use Magento\InventorySourceSelectionApi\Api\Data\ItemRequestInterface;
use Magento\InventorySourceSelectionApi\Api\Data\ItemRequestInterfaceFactory;

/**
* @inheritdoc
Expand All @@ -27,34 +26,13 @@ class InventoryRequest implements InventoryRequestInterface
private $items;

/**
* @var ItemRequestInterfaceFactory
*/
private $itemRequestFactory;

/**
* @param ItemRequestInterfaceFactory $itemRequestFactory
* @param int $stockId
* @param ItemRequestInterface[] $items
*/
public function __construct(
ItemRequestInterfaceFactory $itemRequestFactory,
int $stockId,
array $items
) {
public function __construct(int $stockId = null, array $items = null)
{
$this->stockId = $stockId;
$this->itemRequestFactory = $itemRequestFactory;

//TODO: Temporary fix for resolving issue with webApi (https://github.com/magento-engcom/msi/issues/1524)
foreach ($items as $item) {
if (false === $item instanceof ItemRequestInterface) {
$this->items[] = $this->itemRequestFactory->create([
'sku' => $item['sku'],
'qty' => $item['qty']
]);
} else {
$this->items[] = $item;
}
}
$this->items = $items;
}

/**
Expand All @@ -76,15 +54,15 @@ public function getItems(): array
/**
* @inheritdoc
*/
public function setStockId(int $stockId): void
public function setStockId($stockId)
{
$this->stockId = $stockId;
}

/**
* @inheritdoc
*/
public function setItems(array $items): void
public function setItems($items)
{
$this->items = $items;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,15 @@ public function getQty(): float
/**
* @inheritdoc
*/
public function setSku(string $sku): void
public function setSku($sku)
{
$this->sku = $sku;
}

/**
* @inheritdoc
*/
public function setQty(float $qty): void
public function setQty($qty)
{
$this->qty = $qty;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ public function __construct(
}

/**
* @inheritdoc
* @param InventoryRequestInterface $inventoryRequest
* @param string $algorithmCode
* @return SourceSelectionResultInterface
*/
public function execute(
InventoryRequestInterface $inventoryRequest,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ public function getItems(): array;
* @param int $stockId
* @return void
*/
public function setStockId(int $stockId): void;
public function setStockId($stockId);

/**
* Set Items
*
* @param \Magento\InventorySourceSelectionApi\Api\Data\ItemRequestInterface[] $items
* @return void
*/
public function setItems(array $items): void;
public function setItems($items);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,16 @@ public function getQty(): float;
/**
* Set SKU
*
* @param string $sku
* @param $sku
* @return void
*/
public function setSku(string $sku): void;
public function setSku($sku);

/**
* Set Quantity
*
* @param float $qty
* @param $qty
* @return void
*/
public function setQty(float $qty): void;
public function setQty($qty);
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
interface SourceSelectionServiceInterface
{
/**
* @param \Magento\InventorySourceSelectionApi\Api\Data\InventoryRequestInterface $inventoryRequest
* @param InventoryRequestInterface $inventoryRequest
* @param string $algorithmCode
* @return \Magento\InventorySourceSelectionApi\Api\Data\SourceSelectionResultInterface
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

use Magento\Framework\Exception\SerializationException;
use Magento\Framework\Reflection\Test\Unit\Fixture\TSample;
use Magento\Framework\Reflection\Test\Unit\Fixture\TSampleInterface;
use Magento\Framework\Reflection\TypeProcessor;
use Zend\Code\Reflection\ClassReflection;

Expand Down Expand Up @@ -278,7 +279,7 @@ public function arrayParamTypeDataProvider()
{
return [
['method name' => 'addData', 'type' => 'array[]'],
['method name' => 'addObjectList', 'type' => 'TSampleInterface[]']
['method name' => 'addObjectList', 'type' => TSampleInterface::class . '[]']
];
}

Expand Down
59 changes: 56 additions & 3 deletions lib/internal/Magento/Framework/Reflection/TypeProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Magento\Framework\Reflection;

use Doctrine\Common\Annotations\TokenParser;
use Magento\Framework\Exception\SerializationException;
use Magento\Framework\Phrase;
use Zend\Code\Reflection\ClassReflection;
Expand Down Expand Up @@ -512,7 +513,7 @@ public function processSimpleAndAnyType($value, $type)
public function getParamType(ParameterReflection $param)
{
$type = $param->detectType();
if ($type == 'null') {
if ($type === 'null') {
throw new \LogicException(sprintf(
'@param annotation is incorrect for the parameter "%s" in the method "%s:%s".'
. ' First declared type should not be null. E.g. string|null',
Expand All @@ -521,14 +522,66 @@ public function getParamType(ParameterReflection $param)
$param->getDeclaringFunction()->name
));
}
if ($type == 'array') {
if ($type === 'array') {
// try to determine class, if it's array of objects
$paramDocBlock = $this->getParamDocBlockTag($param);
$paramTypes = $paramDocBlock->getTypes();
$paramType = array_shift($paramTypes);

$paramType = $this->resolveFullyQualifiedClassName($param->getDeclaringClass(), $paramType);

return strpos($paramType, '[]') !== false ? $paramType : "{$paramType}[]";
}
return $type;

return $this->resolveFullyQualifiedClassName($param->getDeclaringClass(), $type);
}

/**
* Resolve fully qualified type name in the class alias context
* @param ClassReflection $sourceClass
* @param string $typeName
* @return string
*/
public function resolveFullyQualifiedClassName(ClassReflection $sourceClass, string $typeName): string
{
$typeName = trim($typeName);

// Resolve fully qualified name
$sourceFileName = $sourceClass->getDeclaringFile();
$source = $sourceFileName->getContents();
$parser = new TokenParser($source);

$namespace = $sourceClass->getNamespaceName();
$aliases = $parser->parseUseStatements($namespace);

// Not a class, but a basic type
if ((strtolower($typeName[0])) === $typeName[0]) {
return $typeName;
}

preg_match('/^(.+?)(\[\])?$/', $typeName, $matches);
$typeName = $matches[1];
$isArray = $matches[2] ? '[]' : '';

$pos = strpos($typeName, '\\');
if ($pos === 0) {
return substr($typeName, 1);
}

if ($pos === false) {
$namespacePrefix = $typeName;
$partialClassName = '';
} else {
$namespacePrefix = substr($typeName, 0, $pos);
$partialClassName = substr($typeName, $pos);
}

$namespacePrefix = strtolower($namespacePrefix);
if (isset($aliases[$namespacePrefix])) {
return $aliases[$namespacePrefix] . $partialClassName . $isArray;
}

return $namespace . '\\' . $typeName . $isArray;
}

/**
Expand Down
12 changes: 10 additions & 2 deletions lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ private function getConstructorData(string $className, array $data): array
$preferenceClass = $this->config->getPreference($className);
$class = new ClassReflection($preferenceClass ?: $className);

$constructor = $class->getConstructor();
$constructor = $class->getMethod('__construct');
if ($constructor === null) {
return [];
}
Expand All @@ -184,7 +184,15 @@ private function getConstructorData(string $className, array $data): array
$parameters = $constructor->getParameters();
foreach ($parameters as $parameter) {
if (isset($data[$parameter->getName()])) {
$res[$parameter->getName()] = $data[$parameter->getName()];
$parameterType = $this->typeProcessor->getParamType($parameter);

try {
$res[$parameter->getName()] = $this->convertValue($data[$parameter->getName()], $parameterType);
} catch (\ReflectionException $e) {
// Parameter was not correclty declared or the class is uknown.
// By not returing the contructor value, we will automatically fall back to the "setters" way.
continue;
}
}
}

Expand Down