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 723dc33

Browse filesBrowse 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 ce95b87 commit 723dc33
Copy full SHA for 723dc33

File tree

2 files changed

+26
-0
lines changed
Filter options

2 files changed

+26
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,14 @@ 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+
[]
466+
);
467+
}
468+
461469
return new $class();
462470
}
463471

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+18Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
use Symfony\Component\Serializer\Tests\Fixtures\Php74Full;
7070
use Symfony\Component\Serializer\Tests\Fixtures\Php80WithPromotedTypedConstructor;
7171
use Symfony\Component\Serializer\Tests\Fixtures\TraversableDummy;
72+
use Symfony\Component\Serializer\Tests\Fixtures\UnitEnumDummy;
7273
use Symfony\Component\Serializer\Tests\Fixtures\WithTypedConstructor;
7374
use Symfony\Component\Serializer\Tests\Normalizer\TestDenormalizer;
7475
use Symfony\Component\Serializer\Tests\Normalizer\TestNormalizer;
@@ -716,6 +717,23 @@ public function testDeserializeInconsistentScalarType()
716717
$serializer->deserialize('"42"', 'int', 'json');
717718
}
718719

720+
/**
721+
* @requires PHP 8.1
722+
*/
723+
public function testDeserializeWhenObjectNotInstantiable()
724+
{
725+
$this->expectException(NotNormalizableValueException::class);
726+
727+
$serializer = new Serializer(
728+
[
729+
new BackedEnumNormalizer(),
730+
new ObjectNormalizer(),
731+
],
732+
['json' => new JsonEncoder()]
733+
);
734+
$serializer->deserialize('{}', UnitEnumDummy::class, 'json');
735+
}
736+
719737
public function testDeserializeScalarArray()
720738
{
721739
$serializer = new Serializer([new ArrayDenormalizer()], ['json' => new JsonEncoder()]);

0 commit comments

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