Skip to content

Commit a61c583

Browse files
committed
forward exceptions caught in the AbstractObjectNormalizer
1 parent 7d12e1a commit a61c583

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

Normalizer/AbstractObjectNormalizer.php

+22-8
Original file line numberDiff line numberDiff line change
@@ -640,8 +640,11 @@ private function validateAndDenormalizeLegacy(array $types, string $currentClass
640640
private function validateAndDenormalize(Type $type, string $currentClass, string $attribute, mixed $data, ?string $format, array $context): mixed
641641
{
642642
$expectedTypes = [];
643+
$isUnionType = $type->asNonNullable() instanceof UnionType;
644+
$e = null;
643645
$extraAttributesException = null;
644646
$missingConstructorArgumentsException = null;
647+
$isNullable = false;
645648

646649
$types = match (true) {
647650
$type instanceof IntersectionType => throw new LogicException('Unable to handle intersection type.'),
@@ -679,12 +682,19 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
679682
// That's why we have to transform the values, if one of these non-string basic datatypes is expected.
680683
$typeIdentifier = $t->getTypeIdentifier();
681684
if (\is_string($data) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
685+
if ('' === $data) {
686+
if (TypeIdentifier::ARRAY === $typeIdentifier) {
687+
return [];
688+
}
689+
690+
if (TypeIdentifier::STRING === $typeIdentifier) {
691+
return '';
692+
}
693+
694+
$isNullable = $isNullable ?: $type->isNullable();
695+
}
696+
682697
switch ($typeIdentifier) {
683-
case TypeIdentifier::ARRAY:
684-
if ('' === $data) {
685-
return [];
686-
}
687-
break;
688698
case TypeIdentifier::BOOL:
689699
// according to https://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"
690700
if ('false' === $data || '0' === $data) {
@@ -808,17 +818,17 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
808818
return $data;
809819
}
810820
} catch (NotNormalizableValueException|InvalidArgumentException $e) {
811-
if (!$type instanceof UnionType) {
821+
if (!$isUnionType && !$isNullable) {
812822
throw $e;
813823
}
814824
} catch (ExtraAttributesException $e) {
815-
if (!$type instanceof UnionType) {
825+
if (!$isUnionType && !$isNullable) {
816826
throw $e;
817827
}
818828

819829
$extraAttributesException ??= $e;
820830
} catch (MissingConstructorArgumentsException $e) {
821-
if (!$type instanceof UnionType) {
831+
if (!$isUnionType && !$isNullable) {
822832
throw $e;
823833
}
824834

@@ -838,6 +848,10 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
838848
throw $missingConstructorArgumentsException;
839849
}
840850

851+
if (!$isUnionType && $e) {
852+
throw $e;
853+
}
854+
841855
if ($context[self::DISABLE_TYPE_ENFORCEMENT] ?? $this->defaultContext[self::DISABLE_TYPE_ENFORCEMENT] ?? false) {
842856
return $data;
843857
}

0 commit comments

Comments
 (0)