Skip to content

Commit 3ee65c1

Browse files
author
Oleksii Korshenko
authored
MAGETWO-84994: 8862: Can't emptying values by magento 2 api #916
2 parents 043e32a + 9e6d519 commit 3ee65c1

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

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

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,10 @@ protected function _createDataObjectForTypeAndArrayValue($type, $customAttribute
315315
*/
316316
public function convertValue($data, $type)
317317
{
318+
if ($data === '') {
319+
return $data;
320+
}
321+
318322
$isArrayType = $this->typeProcessor->isArrayType($type);
319323
if ($isArrayType && isset($data['item'])) {
320324
$data = $this->_removeSoapItemNode($data);
@@ -325,13 +329,7 @@ public function convertValue($data, $type)
325329
/** Complex type or array of complex types */
326330
if ($isArrayType) {
327331
// Initializing the result for array type else it will return null for empty array
328-
$result = is_array($data) ? [] : null;
329-
$itemType = $this->typeProcessor->getArrayItemType($type);
330-
if (is_array($data)) {
331-
foreach ($data as $key => $item) {
332-
$result[$key] = $this->_createFromArray($itemType, $item);
333-
}
334-
}
332+
$result = $this->getResultForArrayType($data, $type);
335333
} else {
336334
$result = $this->_createFromArray($type, $data);
337335
}
@@ -385,4 +383,23 @@ protected function processInputError($inputError)
385383
}
386384
}
387385
}
386+
387+
/**
388+
* @param mixed $data
389+
* @param string $type
390+
*
391+
* @return array|null
392+
*/
393+
private function getResultForArrayType($data, $type)
394+
{
395+
$result = is_array($data) ? [] : null;
396+
$itemType = $this->typeProcessor->getArrayItemType($type);
397+
if (is_array($data)) {
398+
foreach ($data as $key => $item) {
399+
$result[$key] = $this->_createFromArray($itemType, $item);
400+
}
401+
}
402+
403+
return $result;
404+
}
388405
}

lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,4 +569,42 @@ public function invalidCustomAttributesDataProvider()
569569
]
570570
];
571571
}
572+
573+
/**
574+
* Test if $data == '', then we have to get the same ''.
575+
*
576+
* @param string $data
577+
* @param string $type
578+
*
579+
* @dataProvider convertValueWithEmptyValueDataProvider
580+
*/
581+
public function testConvertValueWithEmptyValue($data, $type)
582+
{
583+
$actualData = $this->serviceInputProcessor->convertValue($data, $type);
584+
585+
$this->assertEquals($data, $actualData);
586+
}
587+
588+
/**
589+
* DataProvider for testConvertValueWithEmptyValue.
590+
*
591+
* @return array
592+
*/
593+
public function convertValueWithEmptyValueDataProvider()
594+
{
595+
return [
596+
[
597+
'',
598+
'string'
599+
],
600+
[
601+
'',
602+
'int'
603+
],
604+
[
605+
'',
606+
'float'
607+
],
608+
];
609+
}
572610
}

0 commit comments

Comments
 (0)