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 4fb5b4c

Browse filesBrowse files
committed
Fix BC break when not using the ScalarDenormalizer
1 parent e69918b commit 4fb5b4c
Copy full SHA for 4fb5b4c

File tree

Expand file treeCollapse file tree

7 files changed

+44
-20
lines changed
Filter options
Expand file treeCollapse file tree

7 files changed

+44
-20
lines changed

‎UPGRADE-5.2.md

Copy file name to clipboardExpand all lines: UPGRADE-5.2.md
+4Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,7 @@ Security
173173
`AbstractRememberMeServices::$firewallName`, the old property will be removed
174174
in 6.0.
175175

176+
Serializer
177+
----------
178+
179+
* 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
@@ -219,6 +219,11 @@ Validator
219219
->addDefaultDoctrineAnnotationReader();
220220
```
221221

222+
Serializer
223+
----------
224+
225+
* Removed the denormalization of scalar values without normalizer, add the `ScalarDenormalizer` to the `Serializer`
226+
222227
Yaml
223228
----
224229

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* added `FormErrorNormalizer`
1010
* added `MimeMessageNormalizer`
1111
* serializer mapping can be configured using php attributes
12+
* deprecated denormalizing scalar values without registering the `ScalarDenormalizer`
1213

1314
5.1.0
1415
-----

‎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
+12-1Lines changed: 12 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.
@@ -182,6 +183,16 @@ public function normalize($data, string $format = null, array $context = [])
182183
*/
183184
public function denormalize($data, string $type, string $format = null, array $context = [])
184185
{
186+
if (isset(ScalarDenormalizer::SCALAR_TYPES[$type]) && null === $this->getDenormalizer($data, $type, $format, $context)) {
187+
trigger_deprecation('symfony/serializer', '5.2', 'Denormalizing scalar values without registering the "%s" is deprecated.', ScalarDenormalizer::class);
188+
189+
if (!('is_'.$type)($data)) {
190+
throw new NotNormalizableValueException(sprintf('Data expected to be of type "%s" ("%s" given).', $type, get_debug_type($data)));
191+
}
192+
193+
return $data;
194+
}
195+
185196
if (!$this->normalizers) {
186197
throw new LogicException('You must register at least one normalizer to be able to denormalize objects.');
187198
}
@@ -206,7 +217,7 @@ public function supportsNormalization($data, string $format = null, array $conte
206217
*/
207218
public function supportsDenormalization($data, string $type, string $format = null, array $context = [])
208219
{
209-
return null !== $this->getDenormalizer($data, $type, $format, $context);
220+
return isset(ScalarDenormalizer::SCALAR_TYPES[$type]) || null !== $this->getDenormalizer($data, $type, $format, $context);
210221
}
211222

212223
/**

‎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/filesystem": "^4.4|^5.0",
3233
"symfony/form": "^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.