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 b7ae71d

Browse filesBrowse files
committed
handle builtin types and not just scalars
Looking towards the possible refactoring of the AbstractObjectNormalizer it will be more convenient to have a denormalizer capable of handling builtin types instead of just scalar ones. Expect for `null`, `iterable`, `array` and `object` types: - `null` could be handled here with little work but I'm not sure it's a good idea - `iterable` does not provide enough information to validte the items so it might be better to not handle it so that the user gave a "better" type - `array` and `object` it's simplier to not support them so that we don't have to deal with a complex handling of priority within the normalizers
1 parent 633a605 commit b7ae71d
Copy full SHA for b7ae71d

File tree

7 files changed

+44
-31
lines changed
Filter options

7 files changed

+44
-31
lines changed

‎UPGRADE-5.3.md

Copy file name to clipboardExpand all lines: UPGRADE-5.3.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ Security
2626
Serializer
2727
----------
2828

29-
* Deprecated denormalizing scalar values without registering the `ScalarDenormalizer`
29+
* Deprecated denormalizing scalar values without registering the `BuiltinTypeDenormalizer`

‎UPGRADE-6.0.md

Copy file name to clipboardExpand all lines: UPGRADE-6.0.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ Validator
229229
Serializer
230230
----------
231231

232-
* Removed the denormalization of scalar values without normalizer, add the `ScalarDenormalizer` to the `Serializer`
232+
* Removed the denormalization of scalar values without normalizer, add the `BuiltinTypeDenormalizer` to the `Serializer`
233233

234234
Yaml
235235
----

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/CHANGELOG.md
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ CHANGELOG
44
5.3.0
55
-----
66

7-
* [DEPRECATION] denormalizing scalar values without registering the `ScalarDenormalizer`
7+
* [DEPRECATION] denormalizing scalar values without registering the `BuiltinTypeDenormalizer`
88

99
5.2.0
1010
-----

‎src/Symfony/Component/Serializer/Normalizer/ScalarDenormalizer.php renamed to ‎src/Symfony/Component/Serializer/Normalizer/BuiltinTypeDenormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/BuiltinTypeDenormalizer.php
+11-5Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,33 @@
1717
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1818
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1919

20-
final class ScalarDenormalizer implements DenormalizerInterface, CacheableSupportsMethodInterface
20+
final class BuiltinTypeDenormalizer implements DenormalizerInterface, CacheableSupportsMethodInterface
2121
{
2222
private const TYPE_INT = 'int';
2323
private const TYPE_FLOAT = 'float';
2424
private const TYPE_STRING = 'string';
2525
private const TYPE_BOOL = 'bool';
26+
private const TYPE_RESOURCE = 'resource';
27+
private const TYPE_CALLABLE = 'callable';
2628

27-
public const SCALAR_TYPES = [
29+
private const SUPPORTED_TYPES = [
2830
self::TYPE_INT => true,
2931
self::TYPE_BOOL => true,
3032
self::TYPE_FLOAT => true,
3133
self::TYPE_STRING => true,
34+
self::TYPE_RESOURCE => true,
35+
self::TYPE_CALLABLE => true,
3236
];
3337

3438
/**
3539
* {@inheritdoc}
3640
*/
3741
public function denormalize($data, string $type, string $format = null, array $context = [])
3842
{
39-
if (!isset(self::SCALAR_TYPES[get_debug_type($data)])) {
40-
throw new InvalidArgumentException(sprintf('Data expected to be of one of the types in "%s" ("%s" given).', implode(', ', array_keys(self::SCALAR_TYPES)), get_debug_type($data)));
43+
$dataType = get_debug_type($data);
44+
45+
if (!(isset(self::SUPPORTED_TYPES[$dataType]) || 0 === strpos($dataType, self::TYPE_RESOURCE) || \is_callable($data))) {
46+
throw new InvalidArgumentException(sprintf('Data expected to be of one of the types in "%s" ("%s" given).', implode(', ', array_keys(self::SUPPORTED_TYPES)), get_debug_type($data)));
4147
}
4248

4349
// In XML and CSV all basic datatypes are represented as strings, it is e.g. not possible to determine,
@@ -101,7 +107,7 @@ public function denormalize($data, string $type, string $format = null, array $c
101107
*/
102108
public function supportsDenormalization($data, string $type, string $format = null)
103109
{
104-
return isset(self::SCALAR_TYPES[$type]);
110+
return isset(self::SUPPORTED_TYPES[$type]);
105111
}
106112

107113
public function hasCacheableSupportsMethod(): bool

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Serializer.php
+11-4Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,14 @@
2222
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
2323
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2424
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
25+
use Symfony\Component\Serializer\Normalizer\BuiltinTypeDenormalizer;
2526
use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
2627
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
2728
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
2829
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
2930
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
3031
use Symfony\Component\Serializer\Normalizer\NormalizerAwareInterface;
3132
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
32-
use Symfony\Component\Serializer\Normalizer\ScalarDenormalizer;
3333

3434
/**
3535
* Serializer serializes and deserializes data.
@@ -48,6 +48,13 @@
4848
*/
4949
class Serializer implements SerializerInterface, ContextAwareNormalizerInterface, ContextAwareDenormalizerInterface, ContextAwareEncoderInterface, ContextAwareDecoderInterface
5050
{
51+
private const SCALAR_TYPES = [
52+
'int' => true,
53+
'bool' => true,
54+
'float' => true,
55+
'string' => true,
56+
];
57+
5158
/**
5259
* @var Encoder\ChainEncoder
5360
*/
@@ -186,8 +193,8 @@ public function denormalize($data, string $type, string $format = null, array $c
186193
$normalizer = $this->getDenormalizer($data, $type, $format, $context);
187194

188195
// Check for a denormalizer first, e.g. the data is wrapped
189-
if (!$normalizer && isset(ScalarDenormalizer::SCALAR_TYPES[$type])) {
190-
trigger_deprecation('symfony/serializer', '5.2', 'Denormalizing scalar values without registering the "%s" is deprecated.', ScalarDenormalizer::class);
196+
if (!$normalizer && isset(self::SCALAR_TYPES[$type])) {
197+
trigger_deprecation('symfony/serializer', '5.2', 'Denormalizing scalar values without registering the "%s" is deprecated.', BuiltinTypeDenormalizer::class);
191198

192199
if (!('is_'.$type)($data)) {
193200
throw new NotNormalizableValueException(sprintf('Data expected to be of type "%s" ("%s" given).', $type, get_debug_type($data)));
@@ -220,7 +227,7 @@ public function supportsNormalization($data, string $format = null, array $conte
220227
*/
221228
public function supportsDenormalization($data, string $type, string $format = null, array $context = [])
222229
{
223-
return isset(ScalarDenormalizer::SCALAR_TYPES[$type]) || null !== $this->getDenormalizer($data, $type, $format, $context);
230+
return isset(self::SCALAR_TYPES[$type]) || null !== $this->getDenormalizer($data, $type, $format, $context);
224231
}
225232

226233
/**

‎src/Symfony/Component/Serializer/Tests/Normalizer/ScalarDenormalizerTest.php renamed to ‎src/Symfony/Component/Serializer/Tests/Normalizer/BuiltinTypeDenormalizerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/BuiltinTypeDenormalizerTest.php
+10-10Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,18 @@
55
use PHPUnit\Framework\TestCase;
66
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
77
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
8-
use Symfony\Component\Serializer\Normalizer\ScalarDenormalizer;
8+
use Symfony\Component\Serializer\Normalizer\BuiltinTypeDenormalizer;
99

10-
class ScalarDenormalizerTest extends TestCase
10+
class BuiltinTypeDenormalizerTest extends TestCase
1111
{
1212
/**
13-
* @var ScalarDenormalizer
13+
* @var BuiltinTypeDenormalizer
1414
*/
1515
private $denormalizer;
1616

1717
protected function setUp(): void
1818
{
19-
$this->denormalizer = new ScalarDenormalizer();
19+
$this->denormalizer = new BuiltinTypeDenormalizer();
2020
}
2121

2222
/**
@@ -29,7 +29,7 @@ public function testSupportsDenormalization(string $supportedType): void
2929

3030
public function provideSupportedTypes(): iterable
3131
{
32-
return [['int'], ['float'], ['string'], ['bool']];
32+
return [['int'], ['float'], ['string'], ['bool'], ['resource'], ['callable']];
3333
}
3434

3535
/**
@@ -42,7 +42,7 @@ public function testUnsupportsDenormalization(string $unsupportedType): void
4242

4343
public function provideUnsupportedTypes(): iterable
4444
{
45-
return [['null'], ['array'], ['iterable'], ['object'], ['resource'], ['callable'], ['int[]']];
45+
return [['null'], ['array'], ['iterable'], ['object'], ['int[]']];
4646
}
4747

4848
/**
@@ -57,8 +57,6 @@ public function testDenormalizeInvalidDataThrowsException($invalidData): void
5757
public function provideInvalidData(): iterable
5858
{
5959
return [
60-
'callable' => [[$this, 'provideInvalidData']],
61-
'resource' => [fopen(__FILE__, 'r')],
6260
'array' => [[1, 2]],
6361
'object' => [new \stdClass()],
6462
'null' => [null],
@@ -90,11 +88,11 @@ public function provideNotNormalizableData(): iterable
9088
/**
9189
* @dataProvider provideNormalizableData
9290
*/
93-
public function testDenormalize($expectedResult, $data, string $type, string $format): void
91+
public function testDenormalize($expectedResult, $data, string $type, string $format = null): void
9492
{
9593
$result = $this->denormalizer->denormalize($data, $type, $format);
9694

97-
if (is_nan($expectedResult)) {
95+
if (\is_float($expectedResult) && is_nan($expectedResult)) {
9896
$this->assertNan($result);
9997
} else {
10098
$this->assertSame($expectedResult, $result);
@@ -118,6 +116,8 @@ public function provideNormalizableData(): iterable
118116
"boolean: '1' (xml/csv)" => [true, '1', 'bool', 'xml'],
119117
"boolean: 'false' (xml/csv)" => [false, 'false', 'bool', 'xml'],
120118
"boolean: '0' (xml/csv)" => [false, '0', 'bool', 'xml'],
119+
'callable' => [[$this, 'provideInvalidData'], [$this, 'provideInvalidData'], 'callable', null],
120+
'resource' => [$r = fopen(__FILE__, 'r'), $r, 'resource', null],
121121
];
122122
}
123123
}

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+9-9Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
3030
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
3131
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
32+
use Symfony\Component\Serializer\Normalizer\BuiltinTypeDenormalizer;
3233
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
3334
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
3435
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
@@ -37,7 +38,6 @@
3738
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
3839
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
3940
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
40-
use Symfony\Component\Serializer\Normalizer\ScalarDenormalizer;
4141
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
4242
use Symfony\Component\Serializer\Serializer;
4343
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy;
@@ -623,7 +623,7 @@ public function testLegacyDeserializeWrappedScalar()
623623

624624
public function testDeserializeScalar()
625625
{
626-
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
626+
$serializer = new Serializer([new BuiltinTypeDenormalizer()], ['json' => new JsonEncoder()]);
627627

628628
$this->assertSame(42, $serializer->deserialize('42', 'int', 'json'));
629629
$this->assertSame(-42, $serializer->deserialize('-42', 'int', 'json'));
@@ -638,34 +638,34 @@ public function testDeserializeScalar()
638638
public function testDeserializeLegacyScalarType()
639639
{
640640
$this->expectException(NotNormalizableValueException::class);
641-
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
641+
$serializer = new Serializer([new BuiltinTypeDenormalizer()], ['json' => new JsonEncoder()]);
642642
$serializer->deserialize('42', 'integer', 'json');
643643
}
644644

645645
public function testDeserializeScalarTypeToCustomType()
646646
{
647647
$this->expectException(NotNormalizableValueException::class);
648-
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
648+
$serializer = new Serializer([new BuiltinTypeDenormalizer()], ['json' => new JsonEncoder()]);
649649
$serializer->deserialize('"something"', Foo::class, 'json');
650650
}
651651

652652
public function testDeserializeNonscalarTypeToScalar()
653653
{
654654
$this->expectException(InvalidArgumentException::class);
655-
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
655+
$serializer = new Serializer([new BuiltinTypeDenormalizer()], ['json' => new JsonEncoder()]);
656656
$serializer->deserialize('{"foo":true}', 'string', 'json');
657657
}
658658

659659
public function testDeserializeInconsistentScalarType()
660660
{
661661
$this->expectException(NotNormalizableValueException::class);
662-
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
662+
$serializer = new Serializer([new BuiltinTypeDenormalizer()], ['json' => new JsonEncoder()]);
663663
$serializer->deserialize('"42"', 'int', 'json');
664664
}
665665

666666
public function testDeserializeScalarArray()
667667
{
668-
$serializer = new Serializer([new ScalarDenormalizer(), new ArrayDenormalizer()], ['json' => new JsonEncoder()]);
668+
$serializer = new Serializer([new BuiltinTypeDenormalizer(), new ArrayDenormalizer()], ['json' => new JsonEncoder()]);
669669

670670
$this->assertSame([42], $serializer->deserialize('[42]', 'int[]', 'json'));
671671
$this->assertSame([true, false], $serializer->deserialize('[true,false]', 'bool[]', 'json'));
@@ -676,13 +676,13 @@ public function testDeserializeScalarArray()
676676
public function testDeserializeInconsistentScalarArray()
677677
{
678678
$this->expectException(NotNormalizableValueException::class);
679-
$serializer = new Serializer([new ScalarDenormalizer(), new ArrayDenormalizer()], ['json' => new JsonEncoder()]);
679+
$serializer = new Serializer([new BuiltinTypeDenormalizer(), new ArrayDenormalizer()], ['json' => new JsonEncoder()]);
680680
$serializer->deserialize('["42"]', 'int[]', 'json');
681681
}
682682

683683
public function testDeserializeWrappedScalar()
684684
{
685-
$serializer = new Serializer([new UnwrappingDenormalizer(), new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
685+
$serializer = new Serializer([new UnwrappingDenormalizer(), new BuiltinTypeDenormalizer()], ['json' => new JsonEncoder()]);
686686

687687
$this->assertSame(42, $serializer->deserialize('{"wrapper": 42}', 'int', 'json', [UnwrappingDenormalizer::UNWRAP_PATH => '[wrapper]']));
688688
}

0 commit comments

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