Skip to content

Commit 109cff6

Browse files
author
Oleksii Korshenko
committed
MAGETWO-52616: Bug Fixes Contribution
Merge remote-tracking branch 'origin/MAGETWO-51229-API-wrong-processing-custom-attributes' into pull-request
2 parents 70aaafb + 252e708 commit 109cff6

File tree

2 files changed

+95
-49
lines changed

2 files changed

+95
-49
lines changed

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

Lines changed: 43 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -201,39 +201,25 @@ protected function convertCustomAttributeValue($customAttributesValueArray, $dat
201201
$result = [];
202202
$dataObjectClassName = ltrim($dataObjectClassName, '\\');
203203

204-
$camelCaseAttributeCodeKey = lcfirst(
205-
SimpleDataObjectConverter::snakeCaseToUpperCamelCase(AttributeValue::ATTRIBUTE_CODE)
206-
);
207204
foreach ($customAttributesValueArray as $key => $customAttribute) {
208205
if (!is_array($customAttribute)) {
209206
$customAttribute = [AttributeValue::ATTRIBUTE_CODE => $key, AttributeValue::VALUE => $customAttribute];
210207
}
211-
if (isset($customAttribute[AttributeValue::ATTRIBUTE_CODE])) {
212-
$customAttributeCode = $customAttribute[AttributeValue::ATTRIBUTE_CODE];
213-
} elseif (isset($customAttribute[$camelCaseAttributeCodeKey])) {
214-
$customAttributeCode = $customAttribute[$camelCaseAttributeCodeKey];
215-
} else {
216-
$customAttributeCode = null;
217-
}
208+
209+
list($customAttributeCode, $customAttributeValue) = $this->processCustomAttribute($customAttribute);
218210

219211
$type = $this->customAttributeTypeLocator->getType($customAttributeCode, $dataObjectClassName);
220-
$customAttributeValue = $customAttribute[AttributeValue::VALUE];
212+
$type = $type ? $type : TypeProcessor::ANY_TYPE;
221213

222-
if ($this->typeProcessor->isTypeAny($type) || $this->typeProcessor->isTypeSimple($type)
223-
|| !is_array($customAttributeValue)
224-
) {
225-
try {
226-
$attributeValue = $this->convertValue($customAttributeValue, $type);
227-
} catch (SerializationException $e) {
228-
throw new SerializationException(
229-
new Phrase(
230-
'Attribute "%attribute_code" has invalid value. %details',
231-
['attribute_code' => $customAttributeCode, 'details' => $e->getMessage()]
232-
)
233-
);
214+
if (is_array($customAttributeValue)) {
215+
//If type for AttributeValue's value as array is mixed, further processing is not possible
216+
if ($type === TypeProcessor::ANY_TYPE) {
217+
$attributeValue = $customAttributeValue;
218+
} else {
219+
$attributeValue = $this->_createDataObjectForTypeAndArrayValue($type, $customAttributeValue);
234220
}
235221
} else {
236-
$attributeValue = $this->_createDataObjectForTypeAndArrayValue($type, $customAttributeValue);
222+
$attributeValue = $this->convertValue($customAttributeValue, $type);
237223
}
238224
//Populate the attribute value data object once the value for custom attribute is derived based on type
239225
$result[$customAttributeCode] = $this->attributeValueFactory->create()
@@ -244,6 +230,39 @@ protected function convertCustomAttributeValue($customAttributesValueArray, $dat
244230
return $result;
245231
}
246232

233+
/**
234+
* Derive the custom attribute code and value.
235+
*
236+
* @param string[] $customAttribute
237+
* @return string[]
238+
*/
239+
private function processCustomAttribute($customAttribute)
240+
{
241+
$camelCaseAttributeCodeKey = lcfirst(
242+
SimpleDataObjectConverter::snakeCaseToUpperCamelCase(AttributeValue::ATTRIBUTE_CODE)
243+
);
244+
// attribute code key could be snake or camel case, depending on whether SOAP or REST is used.
245+
if (isset($customAttribute[AttributeValue::ATTRIBUTE_CODE])) {
246+
$customAttributeCode = $customAttribute[AttributeValue::ATTRIBUTE_CODE];
247+
} elseif (isset($customAttribute[$camelCaseAttributeCodeKey])) {
248+
$customAttributeCode = $customAttribute[$camelCaseAttributeCodeKey];
249+
} else {
250+
$customAttributeCode = null;
251+
}
252+
253+
if (!$customAttributeCode && !isset($customAttribute[AttributeValue::VALUE])) {
254+
throw new SerializationException(new Phrase('There is an empty custom attribute specified.'));
255+
} else if (!$customAttributeCode) {
256+
throw new SerializationException(new Phrase('A custom attribute is specified without an attribute code.'));
257+
} else if (!isset($customAttribute[AttributeValue::VALUE])) {
258+
throw new SerializationException(
259+
new Phrase('Value is not set for attribute code "' . $customAttributeCode . '"')
260+
);
261+
}
262+
263+
return [$customAttributeCode, $customAttribute[AttributeValue::VALUE]];
264+
}
265+
247266
/**
248267
* Creates a data object type from a given type name and a PHP array.
249268
*

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

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -442,31 +442,6 @@ public function customAttributesDataProvider()
442442
['entityId' => 15, 'name' => 'Second'],
443443
]),
444444
],
445-
'customAttributeNonExistentCustomAttributeCode' => [
446-
'customAttributeType' => 'integer',
447-
'inputData' => [
448-
'param' => [
449-
'customAttributes' => [
450-
[
451-
'non_existent_attribute_code_' => TestService::CUSTOM_ATTRIBUTE_CODE,
452-
'value' => TestService::DEFAULT_VALUE
453-
]
454-
]
455-
]
456-
],
457-
'expectedObject'=> $this->getObjectWithCustomAttributes('emptyData')
458-
],
459-
'customAttributeObjectNonExistentCustomAttributeCodeValue' => [
460-
'customAttributeType' => 'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\SimpleArray',
461-
'inputData' => [
462-
'param' => [
463-
'customAttributes' => [
464-
['attribute_code' => 'nonExistentAttributeCode', 'value' => ['ids' => [1, 2, 3, 4]]]
465-
]
466-
]
467-
],
468-
'expectedObject'=> $this->getObjectWithCustomAttributes('emptyData')
469-
],
470445
];
471446
}
472447

@@ -527,4 +502,56 @@ protected function getObjectWithCustomAttributes($type, $value = [])
527502
]]
528503
);
529504
}
505+
506+
/**
507+
* Cover invalid custom attribute data
508+
*
509+
* @dataProvider invalidCustomAttributesDataProvider
510+
* @expectedException \Magento\Framework\Webapi\Exception
511+
*/
512+
public function testCustomAttributesExceptions($inputData)
513+
{
514+
$this->serviceInputProcessor->process(
515+
'Magento\Framework\Webapi\Test\Unit\ServiceInputProcessor\TestService',
516+
'ObjectWithCustomAttributesMethod',
517+
$inputData
518+
);
519+
}
520+
521+
public function invalidCustomAttributesDataProvider()
522+
{
523+
return [
524+
[
525+
'inputData' => [
526+
'param' => [
527+
'customAttributes' => [
528+
[]
529+
]
530+
]
531+
]
532+
],
533+
[
534+
'inputData' => [
535+
'param' => [
536+
'customAttributes' => [
537+
[
538+
'value' => TestService::DEFAULT_VALUE
539+
]
540+
]
541+
]
542+
]
543+
],
544+
[
545+
'inputData' => [
546+
'param' => [
547+
'customAttributes' => [
548+
[
549+
'attribute_code' => TestService::CUSTOM_ATTRIBUTE_CODE,
550+
]
551+
]
552+
]
553+
]
554+
]
555+
];
556+
}
530557
}

0 commit comments

Comments
 (0)