Skip to content

Commit e6561ac

Browse files
committed
magento/magento2#87588: Fix json encoded attribute backend type to not encode attribute value multiple times magento#13551
1 parent 3c8be1c commit e6561ac

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

app/code/Magento/Eav/Model/Entity/Attribute/Backend/JsonEncoded.php

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public function beforeSave($object)
4141
{
4242
// parent::beforeSave() is not called intentionally
4343
$attrCode = $this->getAttribute()->getAttributeCode();
44-
if ($object->hasData($attrCode) && !is_string($object->getData($attrCode))) {
44+
if ($object->hasData($attrCode) && !$this->isJsonEncoded((string)$object->getData($attrCode))) {
4545
$object->setData($attrCode, $this->jsonSerializer->serialize($object->getData($attrCode)));
4646
}
4747
return $this;
@@ -61,4 +61,22 @@ public function afterLoad($object)
6161
$object->setData($attrCode, $this->jsonSerializer->unserialize($object->getData($attrCode) ?: '{}'));
6262
return $this;
6363
}
64+
65+
/**
66+
* Returns true if given param is a valid json value else false.
67+
*
68+
* @param string $value
69+
* @return bool
70+
*/
71+
private function isJsonEncoded(string $value): bool
72+
{
73+
$result = true;
74+
try {
75+
$this->jsonSerializer->unserialize($value);
76+
} catch (\InvalidArgumentException $e) {
77+
$result = false;
78+
}
79+
80+
return $result;
81+
}
6482
}

0 commit comments

Comments
 (0)