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 90f8671

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 b4cc161 commit 90f8671
Copy full SHA for 90f8671

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
@@ -4,4 +4,4 @@ UPGRADE FROM 5.2 to 5.3
44
Serializer
55
----------
66

7-
* Deprecated denormalizing scalar values without registering the `ScalarDenormalizer`
7+
* 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
@@ -222,7 +222,7 @@ Validator
222222
Serializer
223223
----------
224224

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

227227
Yaml
228228
----

‎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
@@ -29,7 +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;
32+
use Symfony\Component\Serializer\Normalizer\BuiltinTypeDenormalizer;
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
@@ -4,19 +4,19 @@
44

55
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
66
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
7-
use Symfony\Component\Serializer\Normalizer\ScalarDenormalizer;
7+
use Symfony\Component\Serializer\Normalizer\BuiltinTypeDenormalizer;
88
use PHPUnit\Framework\TestCase;
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
@@ -39,7 +39,7 @@
3939
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
4040
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
4141
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
42-
use Symfony\Component\Serializer\Normalizer\ScalarDenormalizer;
42+
use Symfony\Component\Serializer\Normalizer\BuiltinTypeDenormalizer;
4343
use Symfony\Component\Serializer\Normalizer\UnwrappingDenormalizer;
4444
use Symfony\Component\Serializer\Serializer;
4545
use Symfony\Component\Serializer\Tests\Fixtures\Annotations\AbstractDummy;
@@ -625,7 +625,7 @@ public function testLegacyDeserializeWrappedScalar()
625625

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

630630
$this->assertSame(42, $serializer->deserialize('42', 'int', 'json'));
631631
$this->assertSame(-42, $serializer->deserialize('-42', 'int', 'json'));
@@ -640,34 +640,34 @@ public function testDeserializeScalar()
640640
public function testDeserializeLegacyScalarType()
641641
{
642642
$this->expectException(NotNormalizableValueException::class);
643-
$serializer = new Serializer([new ScalarDenormalizer()], ['json' => new JsonEncoder()]);
643+
$serializer = new Serializer([new BuiltinTypeDenormalizer()], ['json' => new JsonEncoder()]);
644644
$serializer->deserialize('42', 'integer', 'json');
645645
}
646646

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

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

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

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

672672
$this->assertSame([42], $serializer->deserialize('[42]', 'int[]', 'json'));
673673
$this->assertSame([true, false], $serializer->deserialize('[true,false]', 'bool[]', 'json'));
@@ -678,13 +678,13 @@ public function testDeserializeScalarArray()
678678
public function testDeserializeInconsistentScalarArray()
679679
{
680680
$this->expectException(NotNormalizableValueException::class);
681-
$serializer = new Serializer([new ScalarDenormalizer(), new ArrayDenormalizer()], ['json' => new JsonEncoder()]);
681+
$serializer = new Serializer([new BuiltinTypeDenormalizer(), new ArrayDenormalizer()], ['json' => new JsonEncoder()]);
682682
$serializer->deserialize('["42"]', 'int[]', 'json');
683683
}
684684

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

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

0 commit comments

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