@@ -640,8 +640,11 @@ private function validateAndDenormalizeLegacy(array $types, string $currentClass
640
640
private function validateAndDenormalize (Type $ type , string $ currentClass , string $ attribute , mixed $ data , ?string $ format , array $ context ): mixed
641
641
{
642
642
$ expectedTypes = [];
643
+ $ isUnionType = $ type ->asNonNullable () instanceof UnionType;
644
+ $ e = null ;
643
645
$ extraAttributesException = null ;
644
646
$ missingConstructorArgumentsException = null ;
647
+ $ isNullable = false ;
645
648
646
649
$ types = match (true ) {
647
650
$ type instanceof IntersectionType => throw new LogicException ('Unable to handle intersection type. ' ),
@@ -679,12 +682,19 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
679
682
// That's why we have to transform the values, if one of these non-string basic datatypes is expected.
680
683
$ typeIdentifier = $ t ->getTypeIdentifier ();
681
684
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
+
682
697
switch ($ typeIdentifier ) {
683
- case TypeIdentifier::ARRAY :
684
- if ('' === $ data ) {
685
- return [];
686
- }
687
- break ;
688
698
case TypeIdentifier::BOOL :
689
699
// according to https://www.w3.org/TR/xmlschema-2/#boolean, valid representations are "false", "true", "0" and "1"
690
700
if ('false ' === $ data || '0 ' === $ data ) {
@@ -808,17 +818,17 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
808
818
return $ data ;
809
819
}
810
820
} catch (NotNormalizableValueException |InvalidArgumentException $ e ) {
811
- if (!$ type instanceof UnionType ) {
821
+ if (!$ isUnionType && ! $ isNullable ) {
812
822
throw $ e ;
813
823
}
814
824
} catch (ExtraAttributesException $ e ) {
815
- if (!$ type instanceof UnionType ) {
825
+ if (!$ isUnionType && ! $ isNullable ) {
816
826
throw $ e ;
817
827
}
818
828
819
829
$ extraAttributesException ??= $ e ;
820
830
} catch (MissingConstructorArgumentsException $ e ) {
821
- if (!$ type instanceof UnionType ) {
831
+ if (!$ isUnionType && ! $ isNullable ) {
822
832
throw $ e ;
823
833
}
824
834
@@ -838,6 +848,10 @@ private function validateAndDenormalize(Type $type, string $currentClass, string
838
848
throw $ missingConstructorArgumentsException ;
839
849
}
840
850
851
+ if (!$ isUnionType && $ e ) {
852
+ throw $ e ;
853
+ }
854
+
841
855
if ($ context [self ::DISABLE_TYPE_ENFORCEMENT ] ?? $ this ->defaultContext [self ::DISABLE_TYPE_ENFORCEMENT ] ?? false ) {
842
856
return $ data ;
843
857
}
0 commit comments