@@ -883,6 +883,7 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
883
883
$ propertyMetadata = $ this ->propertyMetadataFactory ->create ($ context ['resource_class ' ], $ attribute , $ this ->getFactoryOptions ($ context ));
884
884
$ types = $ propertyMetadata ->getBuiltinTypes () ?? [];
885
885
$ isMultipleTypes = \count ($ types ) > 1 ;
886
+ $ denormalizationException = null ;
886
887
887
888
foreach ($ types as $ type ) {
888
889
if (null === $ value && ($ type ->isNullable () || ($ context [static ::DISABLE_TYPE_ENFORCEMENT ] ?? false ))) {
@@ -908,7 +909,18 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
908
909
$ context ['resource_class ' ] = $ resourceClass ;
909
910
unset($ context ['uri_variables ' ]);
910
911
911
- return $ this ->denormalizeCollection ($ attribute , $ propertyMetadata , $ type , $ resourceClass , $ value , $ format , $ context );
912
+ try {
913
+ return $ this ->denormalizeCollection ($ attribute , $ propertyMetadata , $ type , $ resourceClass , $ value , $ format , $ context );
914
+ } catch (NotNormalizableValueException $ e ) {
915
+ // union/intersect types: try the next type, if not valid, an exception will be thrown at the end
916
+ if ($ isMultipleTypes ) {
917
+ $ denormalizationException ??= $ e ;
918
+
919
+ continue ;
920
+ }
921
+
922
+ throw $ e ;
923
+ }
912
924
}
913
925
914
926
if (
@@ -918,7 +930,18 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
918
930
$ resourceClass = $ this ->resourceClassResolver ->getResourceClass (null , $ className );
919
931
$ childContext = $ this ->createChildContext ($ this ->createOperationContext ($ context , $ resourceClass ), $ attribute , $ format );
920
932
921
- return $ this ->denormalizeRelation ($ attribute , $ propertyMetadata , $ resourceClass , $ value , $ format , $ childContext );
933
+ try {
934
+ return $ this ->denormalizeRelation ($ attribute , $ propertyMetadata , $ resourceClass , $ value , $ format , $ childContext );
935
+ } catch (NotNormalizableValueException $ e ) {
936
+ // union/intersect types: try the next type, if not valid, an exception will be thrown at the end
937
+ if ($ isMultipleTypes ) {
938
+ $ denormalizationException ??= $ e ;
939
+
940
+ continue ;
941
+ }
942
+
943
+ throw $ e ;
944
+ }
922
945
}
923
946
924
947
if (
@@ -933,7 +956,18 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
933
956
934
957
unset($ context ['resource_class ' ], $ context ['uri_variables ' ]);
935
958
936
- return $ this ->serializer ->denormalize ($ value , $ className .'[] ' , $ format , $ context );
959
+ try {
960
+ return $ this ->serializer ->denormalize ($ value , $ className .'[] ' , $ format , $ context );
961
+ } catch (NotNormalizableValueException $ e ) {
962
+ // union/intersect types: try the next type, if not valid, an exception will be thrown at the end
963
+ if ($ isMultipleTypes ) {
964
+ $ denormalizationException ??= $ e ;
965
+
966
+ continue ;
967
+ }
968
+
969
+ throw $ e ;
970
+ }
937
971
}
938
972
939
973
if (null !== $ className = $ type ->getClassName ()) {
@@ -943,7 +977,18 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
943
977
944
978
unset($ context ['resource_class ' ], $ context ['uri_variables ' ]);
945
979
946
- return $ this ->serializer ->denormalize ($ value , $ className , $ format , $ context );
980
+ try {
981
+ return $ this ->serializer ->denormalize ($ value , $ className , $ format , $ context );
982
+ } catch (NotNormalizableValueException $ e ) {
983
+ // union/intersect types: try the next type, if not valid, an exception will be thrown at the end
984
+ if ($ isMultipleTypes ) {
985
+ $ denormalizationException ??= $ e ;
986
+
987
+ continue ;
988
+ }
989
+
990
+ throw $ e ;
991
+ }
947
992
}
948
993
949
994
/* From @see AbstractObjectNormalizer::validateAndDenormalize() */
@@ -1019,6 +1064,10 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
1019
1064
}
1020
1065
}
1021
1066
1067
+ if ($ denormalizationException ) {
1068
+ throw $ denormalizationException ;
1069
+ }
1070
+
1022
1071
return $ value ;
1023
1072
}
1024
1073
0 commit comments