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

Commit d65a686

Browse filesBrowse files
committed
[Serializer] Throw NotNormalizableException instead of InvalidArgumentException in ArrayDenormalizer
1 parent bf255fa commit d65a686
Copy full SHA for d65a686

File tree

5 files changed

+29
-12
lines changed
Filter options

5 files changed

+29
-12
lines changed

‎src/Symfony/Component/Serializer/CHANGELOG.md

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/CHANGELOG.md
+1Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ CHANGELOG
1414
* Deprecate supporting denormalization for `AbstractUid` in `UidNormalizer`, use one of `AbstractUid` child class instead
1515
* Deprecate denormalizing to an abstract class in `UidNormalizer`
1616
* Add support for `can*()` methods to `ObjectNormalizer`
17+
* Throw `NotNormalizableValueException` instead of `InvalidArgumentException` in case of invalid data type in `ArrayDenormalizer`
1718

1819
6.0
1920
---

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/ArrayDenormalizer.php
+5-3Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
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;
@@ -36,13 +37,14 @@ public function denormalize(mixed $data, string $type, string $format = null, ar
3637
if (null === $this->denormalizer) {
3738
throw new BadMethodCallException('Please set a denormalizer before calling denormalize()!');
3839
}
39-
if (!\is_array($data)) {
40-
throw new InvalidArgumentException('Data expected to be an array, '.get_debug_type($data).' given.');
41-
}
4240
if (!str_ends_with($type, '[]')) {
4341
throw new InvalidArgumentException('Unsupported class: '.$type);
4442
}
4543

44+
if (!\is_array($data)) {
45+
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('Data expected to be an array, "%s" given.', get_debug_type($data)), $data, [Type::BUILTIN_TYPE_ARRAY], $context['deserialization_path'] ?? '');
46+
}
47+
4648
$type = substr($type, 0, -2);
4749

4850
$builtinType = isset($context['key_type']) ? $context['key_type']->getBuiltinType() : null;

‎src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/DenormalizerInterface.php
+9-7Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Serializer\Exception\LogicException;
1919
use Symfony\Component\Serializer\Exception\RuntimeException;
2020
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
21+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2122

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

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Fixtures/Php74Full.php
+2Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ final class Php74Full
2727
public array $array;
2828
/** @var Php74Full[] */
2929
public array $collection;
30+
/** @var Php74Full[] */
31+
public array $collection2;
3032
public Php74FullWithConstructor $php74FullWithConstructor;
3133
public DummyMessageInterface $dummyMessage;
3234
/** @var TestFoo[] $nestedArray */

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+12-2Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -802,7 +802,8 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
802802
"splFileInfo": null,
803803
"uuid": null,
804804
"array": null,
805-
"collection": [
805+
"collection": null,
806+
"collection2": [
806807
{
807808
"string": "string"
808809
},
@@ -945,12 +946,21 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
945946
'useMessageForUser' => false,
946947
'message' => 'The type of the "array" attribute for class "Symfony\\Component\\Serializer\\Tests\\Fixtures\\Php74Full" must be one of "array" ("null" given).',
947948
],
949+
[
950+
'currentType' => 'null',
951+
'expectedTypes' => [
952+
'array',
953+
],
954+
'path' => 'collection',
955+
'useMessageForUser' => false,
956+
'message' => 'Data expected to be an array, "null" given.',
957+
],
948958
[
949959
'currentType' => 'null',
950960
'expectedTypes' => [
951961
'string',
952962
],
953-
'path' => 'collection[1].string',
963+
'path' => 'collection2[1].string',
954964
'useMessageForUser' => false,
955965
'message' => 'The type of the "string" attribute for class "Symfony\Component\Serializer\Tests\Fixtures\Php74Full" must be one of "string" ("null" given).',
956966
],

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.