-
Notifications
You must be signed in to change notification settings - Fork 9.4k
Use the new json serializer which throws an error when failing #16154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
magento-engcom-team
merged 5 commits into
magento:2.3-develop
from
storefront-bvba:layout-config-serializer-fix
Jan 14, 2019
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da7f293
added json hex tag serializer and use it.
d7a11d7
Merge remote-tracking branch 'mainline/2.3-develop' into layout-confi…
nmalevanec 46a9ed5
Fix static tests.
nmalevanec 32fd4bd
Fix static test.
nmalevanec bea49bc
Merge remote-tracking branch 'mainline/2.3-develop' into layout-confi…
nmalevanec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
lib/internal/Magento/Framework/Serialize/Serializer/JsonHexTag.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| <?php | ||
| /** | ||
| * Copyright © Magento, Inc. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Magento\Framework\Serialize\Serializer; | ||
|
|
||
| use Magento\Framework\Serialize\SerializerInterface; | ||
|
|
||
| /** | ||
| * Serialize data to JSON with the JSON_HEX_TAG option enabled | ||
| * (All < and > are converted to \u003C and \u003E), | ||
| * unserialize JSON encoded data | ||
| * | ||
| * @api | ||
| * @since 100.2.0 | ||
| */ | ||
| class JsonHexTag extends Json implements SerializerInterface | ||
| { | ||
| /** | ||
| * @inheritDoc | ||
| * @since 100.2.0 | ||
| */ | ||
| public function serialize($data): string | ||
quisse marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| $result = json_encode($data, JSON_HEX_TAG); | ||
quisse marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (false === $result) { | ||
| throw new \InvalidArgumentException('Unable to serialize value.'); | ||
| } | ||
| return $result; | ||
| } | ||
| } | ||
118 changes: 118 additions & 0 deletions
118
lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonHexTagTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| <?php | ||
| /** | ||
| * Copyright © Magento, Inc. All rights reserved. | ||
| * See COPYING.txt for license details. | ||
| */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Magento\Framework\Serialize\Test\Unit\Serializer; | ||
|
|
||
| use Magento\Framework\DataObject; | ||
| use Magento\Framework\Serialize\Serializer\JsonHexTag; | ||
|
|
||
| class JsonHexTagTest extends \PHPUnit\Framework\TestCase | ||
| { | ||
| /** | ||
| * @var \Magento\Framework\Serialize\Serializer\Json | ||
| */ | ||
| private $json; | ||
|
|
||
| protected function setUp() | ||
quisse marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); | ||
| $this->json = $objectManager->getObject(JsonHexTag::class); | ||
| } | ||
|
|
||
| /** | ||
| * @param string|int|float|bool|array|null $value | ||
| * @param string $expected | ||
| * @dataProvider serializeDataProvider | ||
| */ | ||
| public function testSerialize($value, $expected) | ||
| { | ||
| $this->assertEquals( | ||
| $expected, | ||
| $this->json->serialize($value) | ||
| ); | ||
| } | ||
|
|
||
| public function serializeDataProvider() | ||
| { | ||
| $dataObject = new DataObject(['something']); | ||
| return [ | ||
| ['', '""'], | ||
| ['string', '"string"'], | ||
| [null, 'null'], | ||
| [false, 'false'], | ||
| [['a' => 'b', 'd' => 123], '{"a":"b","d":123}'], | ||
| [123, '123'], | ||
| [10.56, '10.56'], | ||
| [$dataObject, '{}'], | ||
| ['< >', '"\u003C \u003E"'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @param string $value | ||
| * @param string|int|float|bool|array|null $expected | ||
| * @dataProvider unserializeDataProvider | ||
| */ | ||
| public function testUnserialize($value, $expected) | ||
| { | ||
| $this->assertEquals( | ||
| $expected, | ||
| $this->json->unserialize($value) | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public function unserializeDataProvider(): array | ||
| { | ||
| return [ | ||
quisse marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ['""', ''], | ||
| ['"string"', 'string'], | ||
| ['null', null], | ||
| ['false', false], | ||
| ['{"a":"b","d":123}', ['a' => 'b', 'd' => 123]], | ||
| ['123', 123], | ||
| ['10.56', 10.56], | ||
| ['{}', []], | ||
| ['"\u003C \u003E"', '< >'], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @expectedException \InvalidArgumentException | ||
| * @expectedExceptionMessage Unable to serialize value. | ||
| */ | ||
| public function testSerializeException() | ||
| { | ||
| $this->json->serialize(STDOUT); | ||
| } | ||
|
|
||
| /** | ||
| * @expectedException \InvalidArgumentException | ||
| * @expectedExceptionMessage Unable to unserialize value. | ||
| * @dataProvider unserializeExceptionDataProvider | ||
| */ | ||
| public function testUnserializeException($value) | ||
| { | ||
| $this->json->unserialize($value); | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public function unserializeExceptionDataProvider(): array | ||
| { | ||
| return [ | ||
| [''], | ||
| [false], | ||
| [null], | ||
| ['{'] | ||
| ]; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.