From 6337bfd0437d35dac854a5cef19c9ba77192253c Mon Sep 17 00:00:00 2001 From: Tomas Date: Wed, 3 Aug 2022 08:15:11 +0300 Subject: [PATCH] [Serializer] Fix throwing right exception in ArrayDenormalizer with invalid type --- .../Serializer/Normalizer/ArrayDenormalizer.php | 3 ++- .../Component/Serializer/Tests/Fixtures/Php74Full.php | 2 ++ .../Component/Serializer/Tests/SerializerTest.php | 10 +++++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php b/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php index 3c64eead1c8b9..cd90ac68191ae 100644 --- a/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Serializer\Normalizer; +use Symfony\Component\PropertyInfo\Type; use Symfony\Component\Serializer\Exception\BadMethodCallException; use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Exception\NotNormalizableValueException; @@ -40,7 +41,7 @@ public function denormalize($data, string $type, string $format = null, array $c throw new BadMethodCallException('Please set a denormalizer before calling denormalize()!'); } if (!\is_array($data)) { - throw new InvalidArgumentException('Data expected to be an array, '.get_debug_type($data).' given.'); + 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); } if (!str_ends_with($type, '[]')) { throw new InvalidArgumentException('Unsupported class: '.$type); diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/Php74Full.php b/src/Symfony/Component/Serializer/Tests/Fixtures/Php74Full.php index 8b53906c405dc..5aea0fa4af76f 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/Php74Full.php +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/Php74Full.php @@ -31,6 +31,8 @@ final class Php74Full public DummyMessageInterface $dummyMessage; /** @var TestFoo[] $nestedArray */ public TestFoo $nestedObject; + /** @var Php74Full[] */ + public $anotherCollection; } diff --git a/src/Symfony/Component/Serializer/Tests/SerializerTest.php b/src/Symfony/Component/Serializer/Tests/SerializerTest.php index 5d5425f88fa2f..761ea066b5962 100644 --- a/src/Symfony/Component/Serializer/Tests/SerializerTest.php +++ b/src/Symfony/Component/Serializer/Tests/SerializerTest.php @@ -854,7 +854,8 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet }, "nestedObject": { "int": "string" - } + }, + "anotherCollection": null }'; $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]); @@ -1030,6 +1031,13 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet 'useMessageForUser' => true, 'message' => 'The type of the key "int" must be "int" ("string" given).', ], + [ + 'currentType' => 'null', + 'expectedTypes' => ['array'], + 'path' => 'anotherCollection', + 'useMessageForUser' => false, + 'message' => 'Data expected to be "Symfony\Component\Serializer\Tests\Fixtures\Php74Full[]", "null" given.', + ], ]; $this->assertSame($expected, $exceptionsAsArray);