Skip to content

Commit dd6ef5b

Browse files
committed
feature symfony#27715 [Serializer] Deprecate CsvEncoder as_collection false default value (ogizanagi)
This PR was merged into the 4.2-dev branch. Discussion ---------- [Serializer] Deprecate CsvEncoder as_collection false default value | Q | A | ------------- | --- | Branch? | master <!-- see below --> | Bug fix? | no | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no <!-- see https://symfony.com/bc --> | Deprecations? | yes <!-- don't forget to update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tests pass? | yes <!-- please add some, will be required by reviewers --> | Fixed tickets | N/A <!-- #-prefixed issue number(s), if any --> | License | MIT | Doc PR | N/A As already expressed in symfony#25369 and related issues, this behavior is quite counter-intuitive. It may be fine for write-API with a single document in the body but I think such CSV APIs are way less common than file-based ones, expecting collections. So I think this behavior should be opt-in explicitly in required cases, always dealing with collections by default. This is still an arbitrary decision, but trying to make it based on use-cases and user's experience with CSV. Note: perhaps we could find a better name for this as the semantic of setting `as_collection` to `false` to get the single row directly would not be really obvious. Also, it could throw an exception when getting multiple rows where only one was expected. Commits ------- bce59c8 [Serializer] Deprecate CsvEncoder as_collection false default value
2 parents c52b2e9 + bce59c8 commit dd6ef5b

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

UPGRADE-4.2.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ SecurityBundle
5353
the token classes is deprecated. To use
5454
custom tokens extend the existing AnonymousToken and RememberMeToken.
5555

56+
Serializer
57+
----------
58+
59+
* Relying on the default value (false) of the "as_collection" option is deprecated since 4.2.
60+
You should set it to false explicitly instead as true will be the default value in 5.0.
61+
5662
DoctrineBridge
5763
--------------
5864

src/Symfony/Component/Serializer/Encoder/CsvEncoder.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@ public function decode($data, $format, array $context = array())
166166
return $result;
167167
}
168168

169+
if (!isset($context['as_collection'])) {
170+
@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);
171+
}
172+
169173
// If there is only one data line in the document, return it (the line), the result is not considered as a collection
170174
return $result[0];
171175
}

src/Symfony/Component/Serializer/Tests/Encoder/CsvEncoderTest.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,11 @@ public function testSupportsDecoding()
282282
$this->assertFalse($this->encoder->supportsDecoding('foo'));
283283
}
284284

285-
public function testDecode()
285+
/**
286+
* @group legacy
287+
* @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.
288+
*/
289+
public function testDecodeLegacy()
286290
{
287291
$expected = array('foo' => 'a', 'bar' => 'b');
288292

@@ -293,6 +297,17 @@ public function testDecode()
293297
, 'csv'));
294298
}
295299

300+
public function testDecodeAsSingle()
301+
{
302+
$expected = array('foo' => 'a', 'bar' => 'b');
303+
304+
$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
305+
foo,bar
306+
a,b
307+
CSV
308+
, 'csv', array(CsvEncoder::AS_COLLECTION_KEY => false)));
309+
}
310+
296311
public function testDecodeCollection()
297312
{
298313
$expected = array(
@@ -311,10 +326,8 @@ public function testDecodeCollection()
311326
, 'csv'));
312327
}
313328

314-
public function testDecodeOnlyOneAsCollection()
329+
public function testDecode()
315330
{
316-
$this->encoder = new CsvEncoder(',', '"', '\\', '.');
317-
318331
$expected = array(
319332
array('foo' => 'a'),
320333
);
@@ -324,7 +337,9 @@ public function testDecodeOnlyOneAsCollection()
324337
a
325338

326339
CSV
327-
, 'csv', array(CsvEncoder::AS_COLLECTION_KEY => true)));
340+
, 'csv', array(
341+
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
342+
)));
328343
}
329344

330345
public function testDecodeToManyRelation()
@@ -365,17 +380,19 @@ public function testDecodeCustomSettings()
365380
{
366381
$this->encoder = new CsvEncoder(';', "'", '|', '-');
367382

368-
$expected = array('a' => 'hell\'o', 'bar' => array('baz' => 'b'));
383+
$expected = array(array('a' => 'hell\'o', 'bar' => array('baz' => 'b')));
369384
$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
370385
a;bar-baz
371386
'hell''o';b;c
372387
CSV
373-
, 'csv'));
388+
, 'csv', array(
389+
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
390+
)));
374391
}
375392

376393
public function testDecodeCustomSettingsPassedInContext()
377394
{
378-
$expected = array('a' => 'hell\'o', 'bar' => array('baz' => 'b'));
395+
$expected = array(array('a' => 'hell\'o', 'bar' => array('baz' => 'b')));
379396
$this->assertEquals($expected, $this->encoder->decode(<<<'CSV'
380397
a;bar-baz
381398
'hell''o';b;c
@@ -385,6 +402,7 @@ public function testDecodeCustomSettingsPassedInContext()
385402
CsvEncoder::ENCLOSURE_KEY => "'",
386403
CsvEncoder::ESCAPE_CHAR_KEY => '|',
387404
CsvEncoder::KEY_SEPARATOR_KEY => '-',
405+
CsvEncoder::AS_COLLECTION_KEY => true, // Can be removed in 5.0
388406
)));
389407
}
390408

0 commit comments

Comments
 (0)