Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ final class Php74Full
public DummyMessageInterface $dummyMessage;
/** @var TestFoo[] $nestedArray */
public TestFoo $nestedObject;
/** @var Php74Full[] */
public $anotherCollection;
}


Expand Down
10 changes: 9 additions & 1 deletion src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,8 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
},
"nestedObject": {
"int": "string"
}
},
"anotherCollection": null
}';

$extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
Expand Down Expand Up @@ -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);
Expand Down