8000 [Serializer] Fix throwing right exception in ArrayDenormalizer with invalid type by norkunas · Pull Request #47169 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Fix throwing right exception in ArrayDenormalizer with invalid type #47169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 3, 2022
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
0