Skip to content

Commit 01314ff

Browse files
author
Cari Spruiell
committed
MAGETWO-51229: API wrong processing custom attributes
- refactor
1 parent 0386fe9 commit 01314ff

File tree

1 file changed

+12
-24
lines changed

1 file changed

+12
-24
lines changed

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

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,7 @@ protected function convertCustomAttributeValue($customAttributesValueArray, $dat
184184
$customAttribute = [AttributeValue::ATTRIBUTE_CODE => $key, AttributeValue::VALUE => $customAttribute];
185185
}
186186

187-
$customAttributeCode = $this->getCustomAttributeCode($customAttribute);
188-
if (!$customAttributeCode || !isset($customAttribute[AttributeValue::VALUE])) {
189-
$this->throwException($customAttributeCode, isset($customAttribute[AttributeValue::VALUE]));
190-
}
191-
$customAttributeValue = $customAttribute[AttributeValue::VALUE];
187+
list($customAttributeCode, $customAttributeValue) = $this->processCustomAttribute($customAttribute);
192188

193189
//Check if type is defined, else default to string
194190
$type = $this->customAttributeTypeLocator->getType($customAttributeCode, $dataObjectClassName);
@@ -214,44 +210,36 @@ protected function convertCustomAttributeValue($customAttributesValueArray, $dat
214210
}
215211

216212
/**
217-
* Get the custom attribute code
213+
* Derive the custom attribute code and value.
218214
*
219215
* @param string[] $customAttribute
220-
* @return string|null
216+
* @return string[]
221217
*/
222-
protected function getCustomAttributeCode($customAttribute)
218+
private function processCustomAttribute($customAttribute)
223219
{
224220
$camelCaseAttributeCodeKey = lcfirst(
225221
SimpleDataObjectConverter::snakeCaseToUpperCamelCase(AttributeValue::ATTRIBUTE_CODE)
226222
);
223+
// attribute code key could be snake or camel case, depending on whether SOAP or REST is used.
227224
if (isset($customAttribute[AttributeValue::ATTRIBUTE_CODE])) {
228-
return $customAttribute[AttributeValue::ATTRIBUTE_CODE];
225+
$customAttributeCode = $customAttribute[AttributeValue::ATTRIBUTE_CODE];
229226
} elseif (isset($customAttribute[$camelCaseAttributeCodeKey])) {
230-
return $customAttribute[$camelCaseAttributeCodeKey];
227+
$customAttributeCode = $customAttribute[$camelCaseAttributeCodeKey];
231228
} else {
232-
return null;
229+
$customAttributeCode = null;
233230
}
234-
}
235231

236-
/**
237-
* Throws an exception based on the incorrect custom attribute specified.
238-
*
239-
* @param string $customAttributeCode
240-
* @param boolean $hasAttributeValue
241-
* @return void
242-
* @throws SerializationException
243-
*/
244-
protected function throwException($customAttributeCode, $hasAttributeValue)
245-
{
246-
if (!$customAttributeCode && !$hasAttributeValue) {
232+
if (!$customAttributeCode && !isset($customAttribute[AttributeValue::VALUE])) {
247233
throw new SerializationException(new Phrase('There is an empty custom attribute specified.'));
248234
} else if (!$customAttributeCode) {
249235
throw new SerializationException(new Phrase('A custom attribute is specified without an attribute code.'));
250-
} else if (!$hasAttributeValue) {
236+
} else if (!isset($customAttribute[AttributeValue::VALUE])) {
251237
throw new SerializationException(
252238
new Phrase('Value is not set for attribute code "' . $customAttributeCode . '"')
253239
);
254240
}
241+
242+
return [$customAttributeCode, $customAttribute[AttributeValue::VALUE]];
255243
}
256244

257245
/**

0 commit comments

Comments
 (0)