Skip to content

Commit 8350e87

Browse files
bug symfony#57433 [Serializer] forward exceptions caught in the AbstractObjectNormalizer (HypeMC, xabbuh)
This PR was merged into the 7.1 branch. Discussion ---------- [Serializer] forward exceptions caught in the `AbstractObjectNormalizer` | Q | A | ------------- | --- | Branch? | 7.1 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | Fix symfony#57427 | License | MIT Commits ------- 6a16b59 forward exceptions caught in the AbstractObjectNormalizer 71d67d4 [Serializer] Check if exception message in test is correct
2 parents 14babeb + 6a16b59 commit 8350e87

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

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

Lines changed: 22 additions & 8 deletions
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
}

src/Symfony/Component/Serializer/Tests/Fixtures/NotNormalizableDummy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ public function __construct()
2626

2727
public function denormalize(DenormalizerInterface $denormalizer, $data, ?string $format = null, array $context = []): void
2828
{
29-
throw new NotNormalizableValueException();
29+
throw new NotNormalizableValueException('Custom exception message');
3030
}
3131
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractObjectNormalizerTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,7 @@ public function testDenormalizeUntypedFormat()
946946
public function testDenormalizeUntypedFormatNotNormalizable()
947947
{
948948
$this->expectException(NotNormalizableValueException::class);
949+
$this->expectExceptionMessage('Custom exception message');
949950
$serializer = new Serializer([new CustomNormalizer(), new ObjectNormalizer(null, null, null, new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]))]);
950951
$serializer->denormalize(['value' => 'test'], DummyWithNotNormalizable::class, 'xml');
951952
}

0 commit comments

Comments
 (0)