Skip to content

[Serializer] forward exceptions caught in the AbstractObjectNormalizer #57433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public function __construct()

public function denormalize(DenormalizerInterface $denormalizer, $data, ?string $format = null, array $context = []): void
{
throw new NotNormalizableValueException();
throw new NotNormalizableValueException('Custom exception message');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,7 @@ public function testDenormalizeUntypedFormat()
public function testDenormalizeUntypedFormatNotNormalizable()
{
$this->expectException(NotNormalizableValueException::class);
$this->expectExceptionMessage('Custom exception message');
$serializer = new Serializer([new CustomNormalizer(), new ObjectNormalizer(null, null, null, new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]))]);
$serializer->denormalize(['value' => 'test'], DummyWithNotNormalizable::class, 'xml');
}
Expand Down
Loading