Skip to content

Commit

Permalink
MAGETWO-84994: 8862: Can't emptying values by magento 2 api #916
Browse files Browse the repository at this point in the history
 - Merge Pull Request magento-engcom/magento2ce#916 from RomaKis/magento2:8862
 - Merged commits:
   1. 1ef70f0
  • Loading branch information
Oleksii Korshenko committed Dec 12, 2017
2 parents a1ee98a + 1ef70f0 commit 9e6d519
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 7 deletions.
31 changes: 24 additions & 7 deletions lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,10 @@ protected function _createDataObjectForTypeAndArrayValue($type, $customAttribute
*/
public function convertValue($data, $type)
{
if ($data === '') {
return $data;
}

$isArrayType = $this->typeProcessor->isArrayType($type);
if ($isArrayType && isset($data['item'])) {
$data = $this->_removeSoapItemNode($data);
Expand All @@ -325,13 +329,7 @@ public function convertValue($data, $type)
/** Complex type or array of complex types */
if ($isArrayType) {
// Initializing the result for array type else it will return null for empty array
$result = is_array($data) ? [] : null;
$itemType = $this->typeProcessor->getArrayItemType($type);
if (is_array($data)) {
foreach ($data as $key => $item) {
$result[$key] = $this->_createFromArray($itemType, $item);
}
}
$result = $this->getResultForArrayType($data, $type);
} else {
$result = $this->_createFromArray($type, $data);
}
Expand Down Expand Up @@ -385,4 +383,23 @@ protected function processInputError($inputError)
}
}
}

/**
* @param mixed $data
* @param string $type
*
* @return array|null
*/
private function getResultForArrayType($data, $type)
{
$result = is_array($data) ? [] : null;
$itemType = $this->typeProcessor->getArrayItemType($type);
if (is_array($data)) {
foreach ($data as $key => $item) {
$result[$key] = $this->_createFromArray($itemType, $item);
}
}

return $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -569,4 +569,42 @@ public function invalidCustomAttributesDataProvider()
]
];
}

/**
* Test if $data == '', then we have to get the same ''.
*
* @param string $data
* @param string $type
*
* @dataProvider convertValueWithEmptyValueDataProvider
*/
public function testConvertValueWithEmptyValue($data, $type)
{
$actualData = $this->serviceInputProcessor->convertValue($data, $type);

$this->assertEquals($data, $actualData);
}

/**
* DataProvider for testConvertValueWithEmptyValue.
*
* @return array
*/
public function convertValueWithEmptyValueDataProvider()
{
return [
[
'',
'string'
],
[
'',
'int'
],
[
'',
'float'
],
];
}
}

0 comments on commit 9e6d519

Please sign in to comment.