8000 [Serializer] Fix throwing right exception in ArrayDenormalizer with i… · symfony/symfony@6337bfd · GitHub
[go: up one dir, main page]

Skip to content

Commit 6337bfd

Browse files
committed
[Serializer] Fix throwing right exception in ArrayDenormalizer with invalid type
1 parent 6d859d4 commit 6337bfd

File tree

3 files changed

+13
-2
lines changed
  • Tests
  • 3 files changed

    +13
    -2
    lines changed

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

    Lines changed: 2 additions & 1 deletion
    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;
    @@ -40,7 +41,7 @@ public function denormalize($data, string $type, string $format = null, array $c
    4041
    throw new BadMethodCallException('Please set a denormalizer before calling denormalize()!');
    4142
    }
    4243
    if (!\is_array($data)) {
    43-
    throw new InvalidArgumentException('Data expected to be an array, '.get_debug_type($data).' given.');
    44+
    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);
    4445
    }
    4546
    if (!str_ends_with($type, '[]')) {
    4647
    throw new InvalidArgumentException('Unsupported class: '.$type);

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

    Lines changed: 2 additions & 0 deletions
    Original file line numberDiff line numberDiff line change
    @@ -31,6 +31,8 @@ final class Php74Full
    3131
    public DummyMessageInterface $dummyMessage;
    3232
    /** @var TestFoo[] $nestedArray */
    3333
    public TestFoo $nestedObject;
    34+
    /** @var Php74Full[] */
    35+
    public $anotherCollection;
    3436
    }
    3537

    3638

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

    Lines changed: 9 additions & 1 deletion
    Original file line numberDiff line numberDiff line change
    @@ -854,7 +854,8 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
    854854
    },
    855855
    "nestedObject": {
    856856
    "int": "string"
    857-
    }
    857+
    },
    858+
    "anotherCollection": null
    858859
    }';
    859860

    860861
    $extractor = new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]);
    @@ -1030,6 +1031,13 @@ public function testCollectDenormalizationErrors(?ClassMetadataFactory $classMet
    10301031
    'useMessageForUser' => true,
    10311032
    'message' => 'The type of the key "int" must be "int" ("string" given).',
    10321033
    ],
    1034+
    [
    1035+
    'currentType' => 'null',
    1036+
    'expectedTypes' => ['array'],
    1037+
    'path' => 'anotherCollection',
    1038+
    'useMessageForUser' => false,
    1039+
    'message' => 'Data expected to be "Symfony\Component\Serializer\Tests\Fixtures\Php74Full[]", "null" given.',
    1040+
    ],
    10331041
    ];
    10341042

    10351043
    $this->assertSame($expected, $exceptionsAsArray);

    0 commit comments

    Comments
     (0)
    0