Skip to content

Commit 1634ac0

Browse files
Merge branch '6.0' into 6.1
* 6.0: [HttpKernel] Fix a PHP 8.1 deprecation notice in HttpCache Add an invariable word in french [Serializer] Fix denormalization union types with constructor
2 parents 5ca08a0 + d01114d commit 1634ac0

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

Normalizer/AbstractObjectNormalizer.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Serializer\Encoder\XmlEncoder;
2222
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
2323
use Symfony\Component\Serializer\Exception\LogicException;
24+
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
2425
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
2526
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
2627
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
@@ -439,13 +440,16 @@ abstract protected function setAttributeValue(object $object, string $attribute,
439440
* @param Type[] $types
440441
*
441442
* @throws NotNormalizableValueException
443+
* @throws ExtraAttributesException
444+
* @throws MissingConstructorArgumentsException
442445
* @throws LogicException
443446
*/
444447
private function validateAndDenormalize(array $types, string $currentClass, string $attribute, mixed $data, ?string $format, array $context): mixed
445448
{
446449
$expectedTypes = [];
447450
$isUnionType = \count($types) > 1;
448451
$extraAttributesException = null;
452+
$missingConstructorArgumentException = null;
449453
foreach ($types as $type) {
450454
if (null === $data && $type->isNullable()) {
451455
return null;
@@ -582,13 +586,23 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
582586
}
583587

584588
$extraAttributesException ??= $e;
589+
} catch (MissingConstructorArgumentsException $e) {
590+
if (!$isUnionType) {
591+
throw $e;
592+
}
593+
594+
$missingConstructorArgumentException ??= $e;
585595
}
586596
}
587597

588598
if ($extraAttributesException) {
589599
throw $extraAttributesException;
590600
}
591601

602+
if ($missingConstructorArgumentException) {
603+
throw $missingConstructorArgumentException;
604+
}
605+
592606
if ($context[self::DISABLE_TYPE_ENFORCEMENT] ?? $this->defaultContext[self::DISABLE_TYPE_ENFORCEMENT] ?? false) {
593607
return $data;
594608
}

Tests/SerializerTest.php

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -764,20 +764,26 @@ public function testUnionTypeDeserializableWithoutAllowedExtraAttributes()
764764
['json' => new JsonEncoder()]
765765
);
766766

767-
$actual = $serializer->deserialize('{ "v": { "a": 0 }}', DummyUnionWithAAndB::class, 'json', [
767+
$actual = $serializer->deserialize('{ "v": { "a": 0 }}', DummyUnionWithAAndCAndB::class, 'json', [
768768
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
769769
]);
770770

771-
$this->assertEquals(new DummyUnionWithAAndB(new DummyATypeForUnion()), $actual);
771+
$this->assertEquals(new DummyUnionWithAAndCAndB(new DummyATypeForUnion()), $actual);
772772

773-
$actual = $serializer->deserialize('{ "v": { "b": 1 }}', DummyUnionWithAAndB::class, 'json', [
773+
$actual = $serializer->deserialize('{ "v": { "b": 1 }}', DummyUnionWithAAndCAndB::class, 'json', [
774774
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
775775
]);
776776

777-
$this->assertEquals(new DummyUnionWithAAndB(new DummyBTypeForUnion()), $actual);
777+
$this->assertEquals(new DummyUnionWithAAndCAndB(new DummyBTypeForUnion()), $actual);
778+
779+
$actual = $serializer->deserialize('{ "v": { "c": 3 }}', DummyUnionWithAAndCAndB::class, 'json', [
780+
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
781+
]);
782+
783+
$this->assertEquals(new DummyUnionWithAAndCAndB(new DummyCTypeForUnion(3)), $actual);
778784

779785
$this->expectException(ExtraAttributesException::class);
780-
$serializer->deserialize('{ "v": { "b": 1, "c": "i am not allowed" }}', DummyUnionWithAAndB::class, 'json', [
786+
$serializer->deserialize('{ "v": { "b": 1, "d": "i am not allowed" }}', DummyUnionWithAAndCAndB::class, 'json', [
781787
AbstractNormalizer::ALLOW_EXTRA_ATTRIBUTES => false,
782788
]);
783789
}
@@ -1256,13 +1262,23 @@ class DummyBTypeForUnion
12561262
public $b = 1;
12571263
}
12581264

1259-
class DummyUnionWithAAndB
1265+
class DummyCTypeForUnion
1266+
{
1267+
public $c = 2;
1268+
1269+
public function __construct($c)
1270+
{
1271+
$this->c = $c;
1272+
}
1273+
}
1274+
1275+
class DummyUnionWithAAndCAndB
12601276
{
1261-
/** @var DummyATypeForUnion|DummyBTypeForUnion */
1277+
/** @var DummyATypeForUnion|DummyCTypeForUnion|DummyBTypeForUnion */
12621278
public $v;
12631279

12641280
/**
1265-
* @param DummyATypeForUnion|DummyBTypeForUnion $v
1281+
* @param DummyATypeForUnion|DummyCTypeForUnion|DummyBTypeForUnion $v
12661282
*/
12671283
public function __construct($v)
12681284
{

0 commit comments

Comments
 (0)