@@ -434,11 +434,16 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
434
434
$ isUnionType = \count ($ types ) > 1 ;
435
435
$ extraAttributesException = null ;
436
436
$ missingConstructorArgumentsException = null ;
437
+ $ hasNonObjectType = false ;
438
+ $ isUnionTypeOrNullable = $ isUnionType ;
439
+
437
440
foreach ($ types as $ type ) {
438
441
if (null === $ data && $ type ->isNullable ()) {
439
442
return null ;
440
443
}
441
444
445
+ $ isUnionTypeOrNullable = $ isUnionTypeOrNullable ?: $ type ->isNullable ();
446
+ $ hasNonObjectType = $ hasNonObjectType ?: Type::BUILTIN_TYPE_OBJECT !== $ type ->getBuiltinType ();
442
447
$ collectionValueType = $ type ->isCollection () ? $ type ->getCollectionValueTypes ()[0 ] ?? null : null ;
443
448
444
449
// Fix a collection that contains the only one element
@@ -564,17 +569,17 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
564
569
return $ data ;
565
570
}
566
571
} catch (NotNormalizableValueException |InvalidArgumentException $ e ) {
567
- if (!$ isUnionType ) {
572
+ if (!$ isUnionTypeOrNullable ) {
568
573
throw $ e ;
569
574
}
570
575
} catch (ExtraAttributesException $ e ) {
571
- if (!$ isUnionType ) {
576
+ if (!$ isUnionTypeOrNullable ) {
572
577
throw $ e ;
573
578
}
574
579
575
580
$ extraAttributesException ??= $ e ;
576
581
} catch (MissingConstructorArgumentsException $ e ) {
577
- if (!$ isUnionType ) {
582
+ if (!$ isUnionTypeOrNullable ) {
578
583
throw $ e ;
579
584
}
580
585
@@ -590,6 +595,12 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
590
595
throw $ missingConstructorArgumentsException ;
591
596
}
592
597
598
+ if ($ data === '' && !$ hasNonObjectType && (XmlEncoder::FORMAT === $ format || CsvEncoder::FORMAT === $ format )) {
599
+ return null ;
600
+ } elseif (!$ isUnionType && isset ($ e )) {
601
+ throw $ e ;
602
+ }
603
+
593
604
if ($ context [self ::DISABLE_TYPE_ENFORCEMENT ] ?? $ this ->defaultContext [self ::DISABLE_TYPE_ENFORCEMENT ] ?? false ) {
594
605
return $ data ;
595
606
}
0 commit comments