Skip to content

Commit b666e9f

Browse files
committed
forward exceptions caught in the AbstractObjectNormalizer
1 parent 7abc106 commit b666e9f

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

+22-3
Original file line numberDiff line numberDiff line change
@@ -626,8 +626,11 @@ private function validateAndDenormalizeLegacy(array $types, string $currentClass
626626
private function validateAndDenormalize(Type $type, string $currentClass, string $attribute, mixed $data, ?string $format, array $context): mixed
627627
{
628628
$expectedTypes = [];
629+
$isUnionType = $type->asNonNullable() instanceof UnionType;
630+
$e = null;
629631
$extraAttributesException = null;
630632
$missingConstructorArgumentsException = null;
633+
$isNullable = false;
631634

632635
$types = match (true) {
633636
$type instanceof IntersectionType => throw new LogicException('Unable to handle intersection type.'),
@@ -665,6 +668,18 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
665668
// That's why we have to transform the values, if one of these non-string basic datatypes is expected.
666669
$typeIdentifier = $t->getTypeIdentifier();
667670
if (\is_string($data) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
671+
if ('' === $data) {
672+
if (TypeIdentifier::ARRAY === $typeIdentifier) {
673+
return [];
674+
}
675+
676+
if (TypeIdentifier::STRING === $typeIdentifier) {
677+
return '';
678+
}
679+
680+
$isNullable = $isNullable ?: $type->isNullable();
681+
}
682+
668683
switch ($typeIdentifier) {
669684
case TypeIdentifier::ARRAY:
670685
if ('' === $data) {
@@ -794,17 +809,17 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
794809
return $data;
795810
}
796811
} catch (NotNormalizableValueException|InvalidArgumentException $e) {
797-
if (!$type instanceof UnionType) {
812+
if (!$isUnionType && !$isNullable) {
798813
throw $e;
799814
}
800815
} catch (ExtraAttributesException $e) {
801-
if (!$type instanceof UnionType) {
816+
if (!$isUnionType && !$isNullable) {
802817
throw $e;
803818
}
804819

805820
$extraAttributesException ??= $e;
806821
} catch (MissingConstructorArgumentsException $e) {
807-
if (!$type instanceof UnionType) {
822+
if (!$isUnionType && !$isNullable) {
808823
throw $e;
809824
}
810825

@@ -824,6 +839,10 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
824839
throw $missingConstructorArgumentsException;
825840
}
826841

842+
if (!$isUnionType && $e) {
843+
throw $e;
844+
}
845+
827846
if ($context[self::DISABLE_TYPE_ENFORCEMENT] ?? $this->defaultContext[self::DISABLE_TYPE_ENFORCEMENT] ?? false) {
828847
return $data;
829848
}

0 commit comments

Comments
 (0)