Skip to content

Commit 58c4522

Browse files
committed
bug symfony#52817 [Serializer] Do not instantiate object if it is not instantiable (maxbaldanza)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] Do not instantiate object if it is not instantiable | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | | License | MIT 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 Commits ------- 298b15f Do not instantiate object if it is not instantiable
2 parents ddf3bcb + 298b15f commit 58c4522

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)