Skip to content

Commit 3c8be1c

Browse files
author
Oleksii Korshenko
committed
MAGETWO-87588: Fix json encoded attribute backend type to not encode attribute value multiple times magento#13551
- Merge Pull Request magento#13551 from tkotosz/magento2:patch-5 - Merged commits: 1. 85f36cd 2. 3d10764
2 parents 03bb798 + 3d10764 commit 3c8be1c

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 1 addition & 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)) {
44+
if ($object->hasData($attrCode) && !is_string($object->getData($attrCode))) {
4545
$object->setData($attrCode, $this->jsonSerializer->serialize($object->getData($attrCode)));
4646
}
4747
return $this;

app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Backend/JsonEncodedTest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,25 @@ public function testBeforeSave()
8282
$this->assertEquals(json_encode([1, 2, 3]), $product->getData('json_encoded'));
8383
}
8484

85+
/**
86+
* Test before save handler with already encoded attribute value
87+
*/
88+
public function testBeforeSaveWithAlreadyEncodedValue()
89+
{
90+
$product = new \Magento\Framework\DataObject(
91+
[
92+
'json_encoded' => [1, 2, 3]
93+
]
94+
);
95+
96+
// save twice
97+
$this->model->beforeSave($product);
98+
$this->model->beforeSave($product);
99+
100+
// check it is encoded only once
101+
$this->assertEquals(json_encode([1, 2, 3]), $product->getData('json_encoded'));
102+
}
103+
85104
/**
86105
* Test after load handler
87106
*/

0 commit comments

Comments
 (0)