Skip to content

Commit

Permalink
forward exceptions caught in the AbstractObjectNormalizer
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed Jun 27, 2024
1 parent 7d12e1a commit a61c583
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions Normalizer/AbstractObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -640,8 +640,11 @@ private function validateAndDenormalizeLegacy(array $types, string $currentClass
private function validateAndDenormalize(Type $type, string $currentClass, string $attribute, mixed $data, ?string $format, array $context): mixed
{
$expectedTypes = [];
$isUnionType = $type->asNonNullable() instanceof UnionType;
$e = null;
$extraAttributesException = null;
$missingConstructorArgumentsException = null;
$isNullable = false;

$types = match (true) {
$type instanceof IntersectionType => throw new LogicException('Unable to handle intersection type.'),
Expand Down Expand Up @@ -679,12 +682,19 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
// That's why we have to transform the values, if one of these non-string basic datatypes is expected.
$typeIdentifier = $t->getTypeIdentifier();
if (\is_string($data) && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
if ('' === $data) {
if (TypeIdentifier::ARRAY === $typeIdentifier) {
return [];
}

if (TypeIdentifier::STRING === $typeIdentifier) {
return '';
}

$isNullable = $isNullable ?: $type->isNullable();
}

switch ($typeIdentifier) {
case TypeIdentifier::ARRAY:
if ('' === $data) {
return [];
}
break;
case TypeIdentifier::BOOL:
// according to https://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"
if ('false' === $data || '0' === $data) {
Expand Down Expand Up @@ -808,17 +818,17 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
return $data;
}
} catch (NotNormalizableValueException|InvalidArgumentException $e) {
if (!$type instanceof UnionType) {
if (!$isUnionType && !$isNullable) {
throw $e;
}
} catch (ExtraAttributesException $e) {
if (!$type instanceof UnionType) {
if (!$isUnionType && !$isNullable) {
throw $e;
}

$extraAttributesException ??= $e;
} catch (MissingConstructorArgumentsException $e) {
if (!$type instanceof UnionType) {
if (!$isUnionType && !$isNullable) {
throw $e;
}

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

if (!$isUnionType && $e) {
throw $e;
}

if ($context[self::DISABLE_TYPE_ENFORCEMENT] ?? $this->defaultContext[self::DISABLE_TYPE_ENFORCEMENT] ?? false) {
return $data;
}
Expand Down

0 comments on commit a61c583

Please sign in to comment.