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

[Serializer] Throw NotNormalizableException instead of InvalidArgumentException in ArrayDenormalizer #46652

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

Closed
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
5 changes: 5 additions & 0 deletions 5 src/Symfony/Component/Serializer/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

6.2
---

* Throw `NotNormalizableValueException` instead of `InvalidArgumentException` in case of invalid data type in `ArrayDenormalizer`

6.1
---

Expand Down
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 All @@ -36,13 +37,14 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
if (null === $this->denormalizer) {
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.');
}
if (!str_ends_with($type, '[]')) {
throw new InvalidArgumentException('Unsupported class: '.$type);
}

if (!\is_array($data)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why inverting the order with the check on the type? Calling is_array() is cheaper than calling str_ends_with(), so it should be done first (especially because this method is ok the hot path and can be called hundreds of times).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@NorthBlue333 Can you answer this question please?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, IHMO, there is no need to change the order of checks there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, sorry did not have much time for those PRs. I will take a look during the end of the week.

throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Data expected to be an array, "%s" given.', get_debug_type($data)), $data, [Type::BUILTIN_TYPE_ARRAY], $context['deserialization_path'] ?? '');
}

$type = substr($type, 0, -2);

$builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Exception\RuntimeException;
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;

/**
* @author Jordi Boggiano <j.boggiano@seld.be>
Expand All @@ -36,13 +37,14 @@ interface DenormalizerInterface
*
* @return mixed
*
* @throws BadMethodCallException Occurs when the normalizer is not called in an expected context
* @throws InvalidArgumentException Occurs when the arguments are not coherent or not supported
* @throws UnexpectedValueException Occurs when the item cannot be hydrated with the given data
* @throws ExtraAttributesException Occurs when the item doesn't have attribute to receive given data
* @throws LogicException Occurs when the normalizer is not supposed to denormalize
* @throws RuntimeException Occurs if the class cannot be instantiated
* @throws ExceptionInterface Occurs for all the other cases of errors
* @throws BadMethodCallException Occurs when the normalizer is not called in an expected context
* @throws InvalidArgumentException Occurs when the arguments are not coherent or not supported
* @throws UnexpectedValueException Occurs when the item cannot be hydrated with the given data
* @throws NotNormalizableValueException Occurs when the item cannot be hydrated with the given data and the error can be collected
* @throws ExtraAttributesException Occurs when the item doesn't have attribute to receive given data
* @throws LogicException Occurs when the normalizer is not supposed to denormalize
* @throws RuntimeException Occurs if the class cannot be instantiated
* @throws ExceptionInterface Occurs for all the other cases of errors
*/
public function denormalize(mixed $data, string $type, string $format = null, array $context = []);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ final class Php74Full
public array $array;
/** @var Php74Full[] */
public array $collection;
/** @var Php74Full[] */
public array $collection2;
public Php74FullWithConstructor $php74FullWithConstructor;
public DummyMessageInterface $dummyMessage;
/** @var TestFoo[] $nestedArray */
Expand Down
14 changes: 12 additions & 2 deletions 14 src/Symfony/Component/Serializer/Tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,8 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
"splFileInfo": null,
"uuid": null,
"array": null,
"collection": [
"collection": null,
"collection2": [
{
"string": "string"
},
Expand Down Expand Up @@ -945,12 +946,21 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
'useMessageForUser' => false,
'message' => 'The type of the "array" attribute for class "Symfony\\Component\\Serializer\\Tests\\Fixtures\\Php74Full" must be one of "array" ("null" given).',
],
[
'currentType' => 'null',
'expectedTypes' => [
'array',
],
'path' => 'collection',
'useMessageForUser' => false,
'message' => 'Data expected to be an array, "null" given.',
],
[
'currentType' => 'null',
'expectedTypes' => [
'string',
],
'path' => 'collection[1].string',
'path' => 'collection2[1].string',
'useMessageForUser' => false,
'message' => 'The type of the "string" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\Php74Full" must be one of "string" ("null" given).',
],
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.