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 17, 2024
1 parent 7abc106 commit b666e9f
Showing 1 changed file with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -626,8 +626,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 @@ -665,6 +668,18 @@ 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) {
Expand Down Expand Up @@ -794,17 +809,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 @@ -824,6 +839,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 b666e9f

Please sign in to comment.