Skip to content

Commit 12c0a5b

Browse files
author
allison guilhem
committed
[Serializer] Receive a PartialDenormalizationException if the property expected in the constructor is missing
1 parent 3f7c872 commit 12c0a5b

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace Symfony\Component\Serializer\Tests\Fixtures;
4+
5+
use Symfony\Component\Serializer\Tests\Fixtures\StringBackedEnumDummy;
6+
7+
class DummyObjectWithEnumConstructor
8+
{
9+
public function __construct(public StringBackedEnumDummy $get)
10+
{
11+
}
12+
}

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

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
3737
use Symfony\Component\Serializer\Normalizer\AbstractObjectNormalizer;
3838
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
39+
use Symfony\Component\Serializer\Normalizer\BackedEnumNormalizer;
3940
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
4041
use Symfony\Component\Serializer\Normalizer\DataUriNormalizer;
4142
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
@@ -63,6 +64,7 @@
6364
use Symfony\Component\Serializer\Tests\Fixtures\Php74Full;
6465
use Symfony\Component\Serializer\Tests\Fixtures\Php80WithPromotedTypedConstructor;
6566
use Symfony\Component\Serializer\Tests\Fixtures\TraversableDummy;
67+
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
6668
use Symfony\Component\Serializer\Tests\Normalizer\TestDenormalizer;
6769
use Symfony\Component\Serializer\Tests\Normalizer\TestNormalizer;
6870

@@ -1165,6 +1167,47 @@ public function testCollectDenormalizationErrorsWithConstructor(?ClassMetadataFa
11651167
$this->assertSame($expected, $exceptionsAsArray);
11661168
}
11671169

1170+
/**
1171+
* @requires PHP 8.1
1172+
*/
1173+
public function testCollectDenormalizationErrorsWithEnumConstructor()
1174+
{
1175+
$serializer = new Serializer(
1176+
[
1177+
new BackedEnumNormalizer(),
1178+
new ObjectNormalizer(),
1179+
],
1180+
['json' => new JsonEncoder()]
1181+
);
1182+
1183+
try {
1184+
$serializer->deserialize('{"invalid": "get"}', DummyObjectWithEnumConstructor::class, 'json', [
1185+
DenormalizerInterface::COLLECT_DENORMALIZATION_ERRORS => true
1186+
]);
1187+
}
1188+
catch (\Throwable $th) {
1189+
$this->assertInstanceOf(PartialDenormalizationException::class, $th);
1190+
}
1191+
1192+
$exceptionsAsArray = array_map(function (NotNormalizableValueException $e): array {
1193+
return [
1194+
'currentType' => $e->getCurrentType(),
1195+
'useMessageForUser' => $e->canUseMessageForUser(),
1196+
'message' => $e->getMessage(),
1197+
];
1198+
}, $th->getErrors());
1199+
1200+
$expected = [
1201+
[
1202+
'currentType' => 'array',
1203+
'useMessageForUser' => true,
1204+
'message' => 'Failed to create object because the object miss the "get" property.',
1205+
],
1206+
];
1207+
1208+
$this->assertSame($expected, $exceptionsAsArray);
1209+
}
1210+
11681211
public function provideCollectDenormalizationErrors()
11691212
{
11701213
return [

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