Skip to content

Commit 0c80436

Browse files
committed
Do not instantiate object if it is not instantiable
If you pass an object that can't be instantiable such as enum to deserialize then you get the following error `Error: Cannot instantiate enum` as the object is tried to be created without checking if it's instantiable
1 parent 52839be commit 0c80436

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,15 @@ protected function instantiateObject(array &$data, string $class, array &$contex
458458

459459
unset($context['has_constructor']);
460460

461+
if (!$reflectionClass->isInstantiable()) {
462+
throw NotNormalizableValueException::createForUnexpectedDataType(
463+
sprintf('Failed to create object because the class "%s" is not instantiable.', $class),
464+
$data,
465+
['unknown'],
466+
$context['deserialization_path'] ?? null
467+
);
468+
}
469+
461470
return new $class();
462471
}
463472

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1717
use Symfony\Component\Serializer\Encoder\JsonEncoder;
18+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1819
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
1920
use Symfony\Component\Serializer\Mapping\ClassMetadata;
2021
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
@@ -32,6 +33,7 @@
3233
use Symfony\Component\Serializer\Tests\Fixtures\NullableOptionalConstructorArgumentDummy;
3334
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
3435
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
36+
use Symfony\Component\Serializer\Tests\Fixtures\UnitEnumDummy;
3537
use Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorTypedArgsDummy;
3638

3739
/**
@@ -279,4 +281,16 @@ public function testIgnore()
279281

280282
$this->assertSame([], $normalizer->normalize($dummy));
281283
}
284+
285+
/**
286+
* @requires PHP 8.1
287+
*/
288+
public function testDenormalizeWhenObjectNotInstantiable()
289+
{
290+
$this->expectException(NotNormalizableValueException::class);
291+
292+
$normalizer = new ObjectNormalizer();
293+
294+
$normalizer->denormalize('{}', UnitEnumDummy::class);
295+
}
282296
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy