Skip to content

Commit a424932

Browse files
committed
bug symfony#57646 [Serializer] Raise correct exception in ArrayDenormalizer when called without a nested denormalizer (derrabus)
This PR was merged into the 7.0 branch. Discussion ---------- [Serializer] Raise correct exception in `ArrayDenormalizer` when called without a nested denormalizer | Q | A | ------------- | --- | Branch? | 7.0 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | N/A | License | MIT The `$denormalizer` property is not nullable and will be uninitialized by default. Because of that, the check that makes sure that the ArrayDenormalizer is not used before setting a nested denormalizer will produce an obscure error instead of raising the proper exception. Commits ------- ed0d3e5 [Serializer] Raise correct exception in ArrayDenormalizer
2 parents f9f194c + ed0d3e5 commit a424932

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function getSupportedTypes(?string $format): array
4242
*/
4343
public function denormalize(mixed $data, string $type, ?string $format = null, array $context = []): array
4444
{
45-
if (null === $this->denormalizer) {
45+
if (!isset($this->denormalizer)) {
4646
throw new BadMethodCallException('Please set a denormalizer before calling denormalize()!');
4747
}
4848
if (!\is_array($data)) {
@@ -72,7 +72,7 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
7272

7373
public function supportsDenormalization(mixed $data, string $type, ?string $format = null, array $context = []): bool
7474
{
75-
if (null === $this->denormalizer) {
75+
if (!isset($this->denormalizer)) {
7676
throw new BadMethodCallException(sprintf('The nested denormalizer needs to be set to allow "%s()" to be used.', __METHOD__));
7777
}
7878

src/Symfony/Component/Serializer/Tests/Normalizer/ArrayDenormalizerTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,22 @@ public function testSupportsNoArray()
108108
)
109109
);
110110
}
111+
112+
public function testDenormalizeWithoutDenormalizer()
113+
{
114+
$arrayDenormalizer = new ArrayDenormalizer();
115+
116+
$this->expectException(\BadMethodCallException::class);
117+
$arrayDenormalizer->denormalize([], 'string[]');
118+
}
119+
120+
public function testSupportsDenormalizationWithoutDenormalizer()
121+
{
122+
$arrayDenormalizer = new ArrayDenormalizer();
123+
124+
$this->expectException(\BadMethodCallException::class);
125+
$arrayDenormalizer->supportsDenormalization([], 'string[]');
126+
}
111127
}
112128

113129
class ArrayDummy

0 commit comments

Comments
 (0)