From 1ef70f0a2b78f0b87dd078af4e961f48cda886e7 Mon Sep 17 00:00:00 2001 From: RomanKis Date: Fri, 24 Nov 2017 15:06:50 +0200 Subject: [PATCH] 8862: Can't emptying values by magento 2 api --- .../Webapi/ServiceInputProcessor.php | 31 +++++++++++---- .../Test/Unit/ServiceInputProcessorTest.php | 38 +++++++++++++++++++ 2 files changed, 62 insertions(+), 7 deletions(-) diff --git a/lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php b/lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php index faca0c1530a53..6558890698082 100644 --- a/lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php +++ b/lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php @@ -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); @@ -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); } @@ -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; + } } diff --git a/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php b/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php index 6f5a18916e04d..fe85e59221847 100644 --- a/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php +++ b/lib/internal/Magento/Framework/Webapi/Test/Unit/ServiceInputProcessorTest.php @@ -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' + ], + ]; + } }