8000 Fix denormalizing empty string into object|null parameter · symfony/symfony@37ba207 · GitHub
[go: up one dir, main page]

Skip to content

Commit 37ba207

Browse files
committed
Fix denormalizing empty string into object|null parameter
1 parent eee7e8f commit 37ba207

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -434,11 +434,16 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
434434
$isUnionType = \count($types) > 1;
435435
$extraAttributesException = null;
436436
$missingConstructorArgumentsException = null;
437+
$hasNonObjectType = false;
438+
$isUnionTypeOrNullable = $isUnionType;
439+
437440
foreach ($types as $type) {
438441
if (null === $data && $type->isNullable()) {
439442
return null;
440443
}
441444

445+
$isUnionTypeOrNullable = $isUnionTypeOrNullable ?: $type->isNullable();
446+
$hasNonObjectType = $hasNonObjectType ?: Type::BUILTIN_TYPE_OBJECT !== $type->getBuiltinType();
442447
$collectionValueType = $type->isCollection() ? $type->getCollectionValueTypes()[0] ?? null : null;
443448

444449
// Fix a collection that contains the only one element
@@ -564,17 +569,17 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
564569
return $data;
565570
}
566571
} catch (NotNormalizableValueException|InvalidArgumentException $e) {
567-
if (!$isUnionType) {
572+
if (!$isUnionTypeOrNullable) {
568573
throw $e;
569574
}
570575
} catch (ExtraAttributesException $e) {
571-
if (!$isUnionType) {
576+
if (!$isUnionTypeOrNullable) {
572577
throw $e;
573578
}
574579

575580
$extraAttributesException ??= $e;
576581
} catch (MissingConstructorArgumentsException $e) {
577-
if (!$isUnionType) {
582+
if (!$isUnionTypeOrNullable) {
578583
throw $e;
579584
}
580585

@@ -590,6 +595,12 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
590595
throw $missingConstructorArgumentsException;
591596
}
592597

598+
if ($data === '' && !$hasNonObjectType && (XmlEncoder::FORMAT === $format || CsvEncoder::FORMAT === $format)) {
599+
return null;
600+
} elseif (!$isUnionType && isset($e)) {
601+
throw $e;
602+
}
603+
593604
if ($context[self::DISABLE_TYPE_ENFORCEMENT] ?? $this->defaultContext[self::DISABLE_TYPE_ENFORCEMENT] ?? false) {
594605
return $data;
595606
}

0 commit comments

Comments
 (0)
0