Skip to content

Commit

Permalink
Fix webapi product custom attribute preprocessor execution
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavosolomon committed Apr 18, 2024
1 parent e1babcf commit 71cef67
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,20 @@ interface PreprocessorInterface
/**
* Check if this attribute data should be processed
*
* @param string $key
* @param mixed $attribute
* @param string $attributeCode
* @param mixed $attribute
*
* @return bool
*/
public function shouldBeProcessed(string $key, $attribute): bool;
public function shouldBeProcessed(string $attributeCode, $attribute): bool;

/**
* Process attribute object according to type rules
*
* @param string $key
* @param mixed $attribute
* @param string $attributeCode
* @param mixed $attribute
*/
public function process(string $key, &$attribute);
public function process(string $attributeCode, &$attribute);

/**
* Get list of affected attributes for the current preprocessor
Expand Down
27 changes: 14 additions & 13 deletions lib/internal/Magento/Framework/Webapi/ServiceInputProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,12 @@ protected function convertCustomAttributeValue($customAttributesValueArray, $dat
$dataObjectClassName = ltrim($dataObjectClassName, '\\');

foreach ($customAttributesValueArray as $key => $customAttribute) {
$this->runCustomAttributePreprocessors($key, $customAttribute);
if (!is_array($customAttribute)) {
$customAttribute = [AttributeValue::ATTRIBUTE_CODE => $key, AttributeValue::VALUE => $customAttribute];
}
list($customAttributeCode, $customAttributeValue) = $this->processCustomAttribute($customAttribute);
$customAttributeCode = $this->processCustomAttributeCode($customAttribute);
$this->runCustomAttributePreprocessors($customAttributeCode, $customAttribute);
$customAttributeValue = $customAttribute[AttributeValue::VALUE];
$entityType = $this->serviceTypeToEntityTypeMap->getEntityType($dataObjectClassName);
if ($entityType) {
$type = $this->customAttributeTypeLocator->getType(
Expand Down Expand Up @@ -406,8 +407,8 @@ private function getAttributesPreprocessorsMap(): array
{
if (!$this->attributesPreprocessorsMap) {
foreach ($this->customAttributePreprocessors as $attributePreprocessor) {
foreach ($attributePreprocessor->getAffectedAttributes() as $attributeKey) {
$this->attributesPreprocessorsMap[$attributeKey][] = $attributePreprocessor;
foreach ($attributePreprocessor->getAffectedAttributes() as $attributeCode) {
$this->attributesPreprocessorsMap[$attributeCode][] = $attributePreprocessor;
}
}
}
Expand All @@ -418,17 +419,17 @@ private function getAttributesPreprocessorsMap(): array
/**
* Prepare attribute value by loaded attribute preprocessors
*
* @param mixed $key
* @param mixed $attributeCode
* @param mixed $customAttribute
*/
private function runCustomAttributePreprocessors($key, &$customAttribute)
private function runCustomAttributePreprocessors($attributeCode, &$customAttribute)
{
$preprocessorsMap = $this->getAttributesPreprocessorsMap();
if ($key && is_array($customAttribute) && array_key_exists($key, $preprocessorsMap)) {
$preprocessorsList = $preprocessorsMap[$key];
if ($attributeCode && is_array($customAttribute) && array_key_exists($attributeCode, $preprocessorsMap)) {
$preprocessorsList = $preprocessorsMap[$attributeCode];
foreach ($preprocessorsList as $attributePreprocessor) {
if ($attributePreprocessor->shouldBeProcessed($key, $customAttribute)) {
$attributePreprocessor->process($key, $customAttribute);
if ($attributePreprocessor->shouldBeProcessed($attributeCode, $customAttribute)) {
$attributePreprocessor->process($attributeCode, $customAttribute);
}
}
}
Expand All @@ -438,10 +439,10 @@ private function runCustomAttributePreprocessors($key, &$customAttribute)
* Derive the custom attribute code and value.
*
* @param string[] $customAttribute
* @return string[]
* @return string
* @throws SerializationException
*/
private function processCustomAttribute($customAttribute)
private function processCustomAttributeCode($customAttribute)
{
$camelCaseAttributeCodeKey = lcfirst(
SimpleDataObjectConverter::snakeCaseToUpperCamelCase(AttributeValue::ATTRIBUTE_CODE)
Expand Down Expand Up @@ -474,7 +475,7 @@ private function processCustomAttribute($customAttribute)
);
}

return [$customAttributeCode, $customAttribute[AttributeValue::VALUE]];
return $customAttributeCode;
}

/**
Expand Down

0 comments on commit 71cef67

Please sign in to comment.