From bce59c8427eb8de88720b12d431e8e7eb72ce7c5 Mon Sep 17 00:00:00 2001 From: Maxime Steinhausser Date: Mon, 25 Jun 2018 20:33:58 +0200 Subject: [PATCH] [Serializer] Deprecate CsvEncoder as_collection false default value --- UPGRADE-4.2.md | 6 ++++ .../Serializer/Encoder/CsvEncoder.php | 4 +++ .../Tests/Encoder/CsvEncoderTest.php | 34 ++++++++++++++----- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/UPGRADE-4.2.md b/UPGRADE-4.2.md index 86968c09e0..5d09aeff80 100644 --- a/UPGRADE-4.2.md +++ b/UPGRADE-4.2.md @@ -53,6 +53,12 @@ SecurityBundle the token classes is deprecated. To use custom tokens extend the existing AnonymousToken and RememberMeToken. +Serializer +---------- + + * Relying on the default value (false) of the "as_collection" option is deprecated since 4.2. + You should set it to false explicitly instead as true will be the default value in 5.0. + DoctrineBridge -------------- diff --git a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php index 6d31fb3600..b73b55cc10 100644 --- a/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php +++ b/src/Symfony/Component/Serializer/Encoder/CsvEncoder.php @@ -166,6 +166,10 @@ class CsvEncoder implements EncoderInterface, DecoderInterface return $result; } + if (!isset($context['as_collection'])) { + @trigger_error('Relying on the default value (false) of the "as_collection" option is deprecated since 4.2. You should set it to false explicitly instead as true will be the default value in 5.0.', E_USER_DEPRECATED); + } + // If there is only one data line in the document, return it (the line), the result is not considered as a collection return $result[0]; } diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php index 4a0bc8169b..9a71034496 100644 --- a/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php @@ -282,7 +282,11 @@ CSV $this->assertFalse($this->encoder->supportsDecoding('foo')); } - public function testDecode() + /** + * @group legacy + * @expectedDeprecation Relying on the default value (false) of the "as_collection" option is deprecated since 4.2. You should set it to false explicitly instead as true will be the default value in 5.0. + */ + public function testDecodeLegacy() { $expected = array('foo' => 'a', 'bar' => 'b'); @@ -293,6 +297,17 @@ CSV , 'csv')); } + public function testDecodeAsSingle() + { + $expected = array('foo' => 'a', 'bar' => 'b'); + + $this->assertEquals($expected, $this->encoder->decode(<<<'CSV' +foo,bar +a,b +CSV + , 'csv', array(CsvEncoder::AS_COLLECTION_KEY => false))); + } + public function testDecodeCollection() { $expected = array( @@ -311,10 +326,8 @@ CSV , 'csv')); } - public function testDecodeOnlyOneAsCollection() + public function testDecode() { - $this->encoder = new CsvEncoder(',', '"', '\\', '.'); - $expected = array( array('foo' => 'a'), ); @@ -324,7 +337,9 @@ foo a CSV - , 'csv', array(CsvEncoder::AS_COLLECTION_KEY => true))); + , 'csv', array( + CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0 + ))); } public function testDecodeToManyRelation() @@ -365,17 +380,19 @@ CSV { $this->encoder = new CsvEncoder(';', "'", '|', '-'); - $expected = array('a' => 'hell\'o', 'bar' => array('baz' => 'b')); + $expected = array(array('a' => 'hell\'o', 'bar' => array('baz' => 'b'))); $this->assertEquals($expected, $this->encoder->decode(<<<'CSV' a;bar-baz 'hell''o';b;c CSV - , 'csv')); + , 'csv', array( + CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0 + ))); } public function testDecodeCustomSettingsPassedInContext() { - $expected = array('a' => 'hell\'o', 'bar' => array('baz' => 'b')); + $expected = array(array('a' => 'hell\'o', 'bar' => array('baz' => 'b'))); $this->assertEquals($expected, $this->encoder->decode(<<<'CSV' a;bar-baz 'hell''o';b;c @@ -385,6 +402,7 @@ CSV CsvEncoder::ENCLOSURE_KEY => "'", CsvEncoder::ESCAPE_CHAR_KEY => '|', CsvEncoder::KEY_SEPARATOR_KEY => '-', + CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0 ))); }