8000 bug #47169 [Serializer] Fix throwing right exception in ArrayDenormal… · symfony/symfony@12ea6e2 · GitHub
[go: up one dir, main page]

Skip to content

Commit 12ea6e2

Browse files
committed
bug #47169 [Serializer] Fix throwing right exception in ArrayDenormalizer with invalid type (norkunas)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Fix throwing right exception in ArrayDenormalizer with invalid type | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | N/A | License | MIT | Doc PR | N/A Currently it's possible to catch most errors with serializer when using `DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true` but there was no tests trying to deserialize list of objects with an invalid value. So it failed my expectations, because in an api using serializer, consumers could get 500 instead of the proper response, therefore I've submit this as a bugfix. Commits ------- 6337bfd [Serializer] Fix throwing right exception in ArrayDenormalizer with invalid type
2 parents b5c2f4a + 6337bfd commit 12ea6e2

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @ 8000 @
1111

1212
namespace Symfony\Component\Serializer\Normalizer;
1313

14+
use Symfony\Component\PropertyInfo\Type;
1415
use Symfony\Component\Serializer\Exception\BadMethodCallException;
1516
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
1617
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
@@ -40,7 +41,7 @@ public function denormalize($data, string $type, string $format = null, array $c
4041
throw new BadMethodCallException('Please set a denormalizer before calling denormalize()!');
4142
}
4243
if (!\is_array($data)) {
43-
throw new InvalidArgumentException('Data expected to be an array, '.get_debug_type($data).' given.');
44+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Data expected to be "%s", "%s" given.', $type, get_debug_type($data)), $data, [Type::BUILTIN_TYPE_ARRAY], $context['deserialization_path'] ?? null);
4445
}
4546
if (!str_ends_with($type, '[]')) {
4647
throw new InvalidArgumentException('Unsupported class: '.$type);

src/Symfony/Component/Serializer/Tests/Fixtures/Php74Full.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ final class Php74Full
3131
public DummyMessageInterface $dummyMessage;
3232
/** @var TestFoo[] $nestedArray */
3333
public TestFoo $nestedObject;
34+
/** @var Php74Full[] */
35+
public $anotherCollection;
3436
}
3537

3638

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,8 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
854854
},
855855
"nestedObject": {
856856
"int": "string"
857-
}
857+
},
858+
"anotherCollection": null
858859
}';
859860

860861
$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
@@ -1030,6 +1031,13 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
10301031
'useMessageForUser' => true,
10311032
'message' => 'The type of the key "int" must be "int" ("string" given).',
10321033
],
1034+
[
1035+
'currentType' => 'null',
1036+
'expectedTypes' => ['array'],
1037+
'path' => 'anotherCollection',
1038+
'useMessageForUser' => false,
1039+
'message' => 'Data expected to be "Symfony\Component\Serializer\Tests\Fixtures\Php74Full[]", "null" given.',
1040+
],
10331041
];
10341042

10351043
$this->assertSame($expected, $exceptionsAsArray);

0 commit comments

Comments
 (0)
0