From 85f36cd1b6c9163bc8e620207bc540b27fb6d3be Mon Sep 17 00:00:00 2001 From: tkotosz Date: Wed, 7 Feb 2018 16:58:00 +0100 Subject: [PATCH 1/2] add failing test for json-encoded attribute backend --- .../Attribute/Backend/JsonEncodedTest.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Backend/JsonEncodedTest.php b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Backend/JsonEncodedTest.php index d94d25e7fd180..3388cbd3052a8 100644 --- a/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Backend/JsonEncodedTest.php +++ b/app/code/Magento/Eav/Test/Unit/Model/Entity/Attribute/Backend/JsonEncodedTest.php @@ -82,6 +82,25 @@ public function testBeforeSave() $this->assertEquals(json_encode([1, 2, 3]), $product->getData('json_encoded')); } + /** + * Test before save handler with already encoded attribute value + */ + public function testBeforeSaveWithAlreadyEncodedValue() + { + $product = new \Magento\Framework\DataObject( + [ + 'json_encoded' => [1, 2, 3] + ] + ); + + // save twice + $this->model->beforeSave($product); + $this->model->beforeSave($product); + + // check it is encoded only once + $this->assertEquals(json_encode([1, 2, 3]), $product->getData('json_encoded')); + } + /** * Test after load handler */ From 3d10764bbfd0d091e4dd3fb33f165655809f360a Mon Sep 17 00:00:00 2001 From: tkotosz Date: Wed, 7 Feb 2018 17:03:40 +0100 Subject: [PATCH 2/2] fix issue when json encoded attribute backend serialize the value multiple times --- .../Magento/Eav/Model/Entity/Attribute/Backend/JsonEncoded.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Eav/Model/Entity/Attribute/Backend/JsonEncoded.php b/app/code/Magento/Eav/Model/Entity/Attribute/Backend/JsonEncoded.php index 5ea8e97e7004f..31e26cf125abd 100644 --- a/app/code/Magento/Eav/Model/Entity/Attribute/Backend/JsonEncoded.php +++ b/app/code/Magento/Eav/Model/Entity/Attribute/Backend/JsonEncoded.php @@ -41,7 +41,7 @@ public function beforeSave($object) { // parent::beforeSave() is not called intentionally $attrCode = $this->getAttribute()->getAttributeCode(); - if ($object->hasData($attrCode)) { + if ($object->hasData($attrCode) && !is_string($object->getData($attrCode))) { $object->setData($attrCode, $this->jsonSerializer->serialize($object->getData($attrCode))); } return $this;