@@ -82,10 +82,19 @@ public function cast(mixed $value, Context $context): mixed
82
82
*/
83
83
private function castOverDiscriminator (DiscriminatorMapMetadata $ map , array $ value , Context $ context ): mixed
84
84
{
85
+ // Default mapping type
86
+ $ default = $ map ->getDefaultType ()
87
+ ?->getType();
88
+
85
89
$ field = $ map ->getField ();
86
90
87
91
// In case of discriminator field is missing
88
92
if (!\array_key_exists ($ field , $ value )) {
93
+ // In case of default type is present
94
+ if ($ default !== null ) {
95
+ return $ default ->cast ($ value , $ context );
96
+ }
97
+
89
98
throw MissingRequiredObjectFieldException::createFromContext (
90
99
field: $ field ,
91
100
expected: $ map ->getTypeStatement (),
@@ -98,6 +107,11 @@ private function castOverDiscriminator(DiscriminatorMapMetadata $map, array $val
98
107
99
108
// In case of discriminator field is not a string
100
109
if (!\is_string ($ element )) {
110
+ // In case of default type is present
111
+ if ($ default !== null ) {
112
+ return $ default ->cast ($ value , $ context );
113
+ }
114
+
101
115
throw InvalidObjectValueException::createFromContext (
102
116
element: $ element ,
103
117
field: $ field ,
@@ -111,6 +125,11 @@ private function castOverDiscriminator(DiscriminatorMapMetadata $map, array $val
111
125
112
126
// In case of discriminator value is not found
113
127
if ($ mapping === null ) {
128
+ // In case of default type is present
129
+ if ($ default !== null ) {
130
+ return $ default ->cast ($ value , $ context );
131
+ }
132
+
114
133
throw InvalidObjectValueException::createFromContext (
115
134
element: $ element ,
116
135
field: $ field ,
0 commit comments