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 0c80436

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 52839be commit 0c80436
Copy full SHA for 0c80436

File tree

2 files changed

+23
-0
lines changed
Filter options

2 files changed

+23
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
+9Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php
+14Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.