Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit adb6c07

Browse filesBrowse files
committed
Fix BC break when not using the ScalarDenormalizer
1 parent e92d934 commit adb6c07
Copy full SHA for adb6c07

File tree

7 files changed

+67
-20
lines changed
Filter options

7 files changed

+67
-20
lines changed

‎UPGRADE-5.2.md

Copy file name to clipboard
+7Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
UPGRADE FROM 5.0 to 5.1
2+
=======================
3+
4+
Serializer
5+
----------
6+
7+
* Deprecated denormalizing scalar values without registering the `ScalarDenormalizer`

‎UPGRADE-6.0.md

Copy file name to clipboardExpand all lines: UPGRADE-6.0.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ Security
115115
* Removed `DefaultLogoutSuccessHandler` in favor of `DefaultLogoutListener`.
116116
* Added a `logout(Request $request, Response $response, TokenInterface $token)` method to the `RememberMeServicesInterface`.
117117

118+
Serializer
119+
----------
120+
121+
* Removed the denormalization of scalar values without normalizer, add the `ScalarDenormalizer` to the `Serializer`
122+
118123
Yaml
119124
----
120125

‎src/Symfony/Component/Serializer/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/CHANGELOG.md
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
5.2.0
5+
-----
6+
7+
* deprecated denormalizing scalar values without registering the `ScalarDenormalizer`
8+
49
5.1.0
510
-----
611

‎src/Symfony/Component/Serializer/Normalizer/ScalarDenormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/ScalarDenormalizer.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
final class ScalarDenormalizer implements DenormalizerInterface, CacheableSupportsMethodInterface
2020
{
21-
private const SCALAR_TYPES = [
21+
public const SCALAR_TYPES = [
2222
'int' => true,
2323
'bool' => true,
2424
'float' => true,

‎src/Symfony/Component/Serializer/Serializer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Serializer.php
+28-1Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
3030
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
3131
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
32+
use Symfony\Component\Serializer\Normalizer\ScalarDenormalizer;
3233

3334
/**
3435
* Serializer serializes and deserializes data.
@@ -60,6 +61,7 @@ class Serializer implements SerializerInterface, ContextAwareNormalizerInterface
6061
private $normalizers = [];
6162
private $denormalizerCache = [];
6263
private $normalizerCache = [];
64+
private $hasScalarDenormalizer = null;
6365

6466
/**
6567
* @param (NormalizerInterface|DenormalizerInterface)[] $normalizers
@@ -182,6 +184,16 @@ public function normalize($data, string $format = null, array $context = [])
182184
*/
183185
public function denormalize($data, string $type, string $format = null, array $context = [])
184186
{
187+
if (isset(ScalarDenormalizer::SCALAR_TYPES[$type]) && !$this->hasScalarDenormalizer()) {
188+
trigger_deprecation('symfony/serializer', '5.2', 'Denormalizing scalar values without registering the "%s" is deprecated.', ScalarDenormalizer::class);
189+
190+
if (!('is_'.$type)($data)) {
191+
throw new NotNormalizableValueException(sprintf('Data expected to be of type "%s" ("%s" given).', $type, get_debug_type($data)));
192+
}
193+
194+
return $data;
195+
}
196+
185197
if (!$this->normalizers) {
186198
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
187199
}
@@ -206,7 +218,7 @@ public function supportsNormalization($data, string $format = null, array $conte
206218
*/
207219
public function supportsDenormalization($data, string $type, string $format = null, array $context = [])
208220
{
209-
return null !== $this->getDenormalizer($data, $type, $format, $context);
221+
return isset(ScalarDenormalizer::SCALAR_TYPES[$type]) || null !== $this->getDenormalizer($data, $type, $format, $context);
210222
}
211223

212224
/**
@@ -284,6 +296,21 @@ private function getDenormalizer($data, string $class, ?string $format, array $c
284296
return null;
285297
}
286298

299+
private function hasScalarDenormalizer(): bool
300+
{
301+
if (null !== $this->hasScalarDenormalizer) {
302+
return $this->hasScalarDenormalizer;
303+
}
304+
305+
foreach ($this->normalizers as $normalizer) {
306+
if ($normalizer instanceof ScalarDenormalizer) {
307+
return $this->hasScalarDenormalizer = true;
308+
}
309+
}
310+
311+
return $this->hasScalarDenormalizer = false;
312+
}
313+
287314
/**
288315
* {@inheritdoc}
289316
*/

‎src/Symfony/Component/Serializer/Tests/SerializerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+20-18Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
1919
use Symfony\Component\Serializer\Encoder\JsonEncoder;
2020
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
21+
use Symfony\Component\Serializer\Exception\LogicException;
2122
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2223
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
2324
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping;
@@ -556,61 +557,62 @@ public function testNormalizeScalarArray()
556557
$this->assertSame('[" spaces ","@Ca$e%"]', $serializer->serialize([' spaces ', '@Ca$e%'], 'json'));
557558
}
558559

559-
public function testDeserializeScalar()
560+
public function testLegacyDeserializeScalar()
560561
{
561-
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
562+
$serializer = new Serializer([], ['json' => new JsonEncoder()]);
562563

563564
$this->assertSame(42, $serializer->deserialize('42', 'int', 'json'));
564565
$this->assertTrue($serializer->deserialize('true', 'bool', 'json'));
565566
$this->assertSame(3.14, $serializer->deserialize('3.14', 'float', 'json'));
566567
$this->assertSame(3.14, $serializer->deserialize('31.4e-1', 'float', 'json'));
567-
$this->assertSame(3.0, $serializer->deserialize('3', 'float', 'json')); // '3' === json_encode(3.0)
568568
$this->assertSame(' spaces ', $serializer->deserialize('" spaces "', 'string', 'json'));
569569
$this->assertSame('@Ca$e%', $serializer->deserialize('"@Ca$e%"', 'string', 'json'));
570+
571+
// Only works with the ScalarDenormalizer
572+
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
573+
574+
$this->assertSame(3.0, $serializer->deserialize('3', 'float', 'json')); // '3' === json_encode(3.0)
570575
}
571576

572577
public function testDeserializeLegacyScalarType()
573578
{
574-
$this->expectException(NotNormalizableValueException::class);
575-
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
579+
$this->expectException(LogicException::class);
580+
$serializer = new Serializer([], ['json' => new JsonEncoder()]);
576581
$serializer->deserialize('42', 'integer', 'json');
577582
}
578583

579584
public function testDeserializeScalarTypeToCustomType()
580585
{
581-
$this->expectException(NotNormalizableValueException::class);
582-
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
586+
$this->expectException(LogicException::class);
587+
$serializer = new Serializer([], ['json' => new JsonEncoder()]);
583588
$serializer->deserialize('"something"', Foo::class, 'json');
584589
}
585590

586-
public function testDeserializeNonscalarTypeToScalar()
591+
public function testLegacyDeserializeNonscalarTypeToScalar()
587592
{
588-
$this->expectException(InvalidArgumentException::class);
589-
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
593+
$this->expectException(NotNormalizableValueException::class);
594+
$serializer = new Serializer([], ['json' => new JsonEncoder()]);
590595
$serializer->deserialize('{"foo":true}', 'string', 'json');
591596
}
592597

593-
public function testDeserializeInconsistentScalarType()
598+
public function testLegacyDeserializeInconsistentScalarType()
594599
{
595600
$this->expectException(NotNormalizableValueException::class);
596-
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
601+
$serializer = new Serializer([], ['json' => new JsonEncoder()]);
597602
$serializer->deserialize('"42"', 'int', 'json');
598603
}
599604

600-
public function testDeserializeScalarArray()
605+
public function testLegacyDeserializeScalarArray()
601606
{
602-
$serializer = new Serializer([
603-
new ScalarDenormalizer(),
604-
new ArrayDenormalizer(),
605-
], ['json' => new JsonEncoder()]);
607+
$serializer = new Serializer([new ArrayDenormalizer()], ['json' => new JsonEncoder()]);
606608

607609
$this->assertSame([42], $serializer->deserialize('[42]', 'int[]', 'json'));
608610
$this->assertSame([true, false], $serializer->deserialize('[true,false]', 'bool[]', 'json'));
609611
$this->assertSame([3.14, 3.24], $serializer->deserialize('[3.14,32.4e-1]', 'float[]', 'json'));
610612
$this->assertSame([' spaces ', '@Ca$e%'], $serializer->deserialize('[" spaces ","@Ca$e%"]', 'string[]', 'json'));
611613
}
612614

613-
public function testDeserializeInconsistentScalarArray()
615+
public function testLegacyDeserializeInconsistentScalarArray()
614616
{
615617
$this->expectException(NotNormalizableValueException::class);
616618
$serializer = new Serializer([new ArrayDenormalizer()], ['json' => new JsonEncoder()]);

‎src/Symfony/Component/Serializer/composer.json

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/composer.json
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"symfony/cache": "^4.4|^5.0",
2828
"symfony/config": "^4.4|^5.0",
2929
"symfony/dependency-injection": "^4.4|^5.0",
30+
"symfony/deprecation-contracts": "^2.1",
3031
"symfony/error-handler": "^4.4|^5.0",
3132
"symfony/http-foundation": "^4.4|^5.0",
3233
"symfony/mime": "^4.4|^5.0",

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.