@@ -252,9 +252,9 @@ private function handleFields(
252
252
$ data = $ this ->handleSentFields ($ data , $ mappedObjectContext , $ callContext );
253
253
$ data = $ this ->handleMissingFields ($ data , $ mappedObjectContext , $ callContext );
254
254
255
- $ type = $ mappedObjectContext ->getType ();
255
+ $ type = $ mappedObjectContext ->getTypeIfInitialized ();
256
256
257
- if ($ type ->hasInvalidFields ()) {
257
+ if ($ type !== null && $ type ->hasInvalidFields ()) {
258
258
throw InvalidData::create ($ type , Value::none ());
259
259
}
260
260
@@ -272,7 +272,7 @@ private function handleSentFields(
272
272
ProcessorCallContext $ callContext
273
273
): array
274
274
{
275
- $ type = $ mappedObjectContext -> getType () ;
275
+ $ type = null ;
276
276
$ options = $ mappedObjectContext ->getOptions ();
277
277
278
278
$ meta = $ callContext ->getMeta ();
@@ -281,7 +281,7 @@ private function handleSentFields(
281
281
282
282
foreach ($ data as $ fieldName => $ value ) {
283
283
// Skip invalid field
284
- if ($ type ->isFieldInvalid ($ fieldName )) {
284
+ if ($ type !== null && $ type ->isFieldInvalid ($ fieldName )) {
285
285
continue ;
286
286
}
287
287
@@ -305,6 +305,7 @@ private function handleSentFields(
305
305
: '. ' ;
306
306
307
307
// Add error to type
308
+ $ type ??= $ mappedObjectContext ->getType ();
308
309
$ type ->overwriteInvalidField (
309
310
$ fieldName ,
310
311
ValueDoesNotMatch::create (
@@ -346,6 +347,7 @@ private function handleSentFields(
346
347
$ fieldMeta ,
347
348
);
348
349
} catch (ValueDoesNotMatch | InvalidData $ exception ) {
350
+ $ type ??= $ mappedObjectContext ->getType ();
349
351
$ type ->overwriteInvalidField ($ fieldName , $ exception );
350
352
}
351
353
}
@@ -388,7 +390,7 @@ private function handleMissingFields(
388
390
ProcessorCallContext $ callContext
389
391
): array
390
392
{
391
- $ type = $ mappedObjectContext -> getType () ;
393
+ $ type = null ;
392
394
$ options = $ mappedObjectContext ->getOptions ();
393
395
$ initializeObjects = $ mappedObjectContext ->shouldInitializeObjects ();
394
396
@@ -416,10 +418,14 @@ private function handleMissingFields(
416
418
if ($ fillDefaultValues ) {
417
419
$ data [$ missingField ] = $ defaultMeta ->getValue ();
418
420
}
419
- } elseif ($ requiredFields !== RequiredFields::none () && !$ type ->isFieldInvalid ($ missingField )) {
421
+ } elseif (
422
+ $ requiredFields !== RequiredFields::none ()
423
+ && ($ type === null || !$ type ->isFieldInvalid ($ missingField ))
424
+ ) {
420
425
// Field is missing and have no default value, mark as invalid
421
426
$ fieldRuleMeta = $ fieldMeta ->getRule ();
422
427
$ fieldRule = $ this ->ruleManager ->getRule ($ fieldRuleMeta ->getType ());
428
+ $ type ??= $ mappedObjectContext ->getType ();
423
429
$ type ->overwriteInvalidField (
424
430
$ missingField ,
425
431
ValueDoesNotMatch::create (
@@ -469,8 +475,7 @@ private function createFieldContext(
469
475
ReflectionProperty $ property
470
476
): FieldContext
471
477
{
472
- $ parentType = $ mappedObjectContext ->getType ();
473
- $ typeCreator = static fn (): Type => $ parentType ->getField ($ fieldName );
478
+ $ typeCreator = static fn (): Type => $ mappedObjectContext ->getType ()->getField ($ fieldName );
474
479
475
480
return new FieldContext (
476
481
$ this ->metaLoader ,
@@ -543,11 +548,10 @@ private function handleClassCallbacks(
543
548
string $ callbackType
544
549
)
545
550
{
546
- $ type = $ mappedObjectContext ->getType ();
547
-
548
551
try {
549
552
$ data = $ this ->applyCallbacks ($ data , $ mappedObjectContext , $ callContext , $ meta , $ callbackType );
550
553
} catch (ValueDoesNotMatch | InvalidData $ exception ) {
554
+ $ type = $ mappedObjectContext ->getType ();
551
555
$ caughtType = $ exception ->getType ();
552
556
553
557
// User thrown type is not the actual type from MappedObjectContext
@@ -616,7 +620,6 @@ private function fillObject(
616
620
ProcessorCallContext $ callContext
617
621
): void
618
622
{
619
- $ type = $ mappedObjectContext ->getType ();
620
623
$ options = $ mappedObjectContext ->getOptions ();
621
624
$ meta = $ callContext ->getMeta ();
622
625
@@ -639,7 +642,7 @@ private function fillObject(
639
642
// Set skipped properties
640
643
$ skippedFields = $ callContext ->getSkippedFields ();
641
644
if ($ skippedFields !== []) {
642
- $ skippedContext = new SkippedFieldsContext ($ type , $ options );
645
+ $ skippedContext = new SkippedFieldsContext ($ mappedObjectContext );
643
646
$ this ->skippedMap ->setSkippedFieldsContext ($ object , $ skippedContext );
644
647
645
648
foreach ($ skippedFields as $ fieldName => $ skippedFieldContext ) {
@@ -713,11 +716,13 @@ public function processSkippedFields(
713
716
}
714
717
715
718
$ skippedFieldsContext = $ this ->skippedMap ->getSkippedFieldsContext ($ object );
719
+ $ mappedObjectContext = $ skippedFieldsContext ->getMappedObjectContext ();
720
+
721
+ if ($ options !== null ) {
722
+ $ mappedObjectContext = $ mappedObjectContext ->createCloneWithOptions ($ options );
723
+ }
716
724
717
- $ type = $ skippedFieldsContext ->getType ();
718
- $ typeCreator = static fn (): MappedObjectType => $ type ;
719
- $ options ??= $ skippedFieldsContext ->getOptions ();
720
- $ mappedObjectContext = $ this ->createMappedObjectContext ($ options , $ typeCreator , true );
725
+ $ type = null ;
721
726
$ skippedFields = $ skippedFieldsContext ->getSkippedFields ();
722
727
723
728
$ meta = $ this ->metaLoader ->load ($ class );
@@ -757,6 +762,7 @@ public function processSkippedFields(
757
762
$ fieldMeta ,
758
763
);
759
764
} catch (ValueDoesNotMatch | InvalidData $ exception ) {
765
+ $ type ??= $ mappedObjectContext ->getType ();
760
766
$ type ->overwriteInvalidField ($ fieldName , $ exception );
761
767
762
768
continue ;
@@ -768,7 +774,7 @@ public function processSkippedFields(
768
774
}
769
775
770
776
// If any of fields is invalid, throw error
771
- if ($ type ->hasInvalidFields ()) {
777
+ if ($ type !== null && $ type ->hasInvalidFields ()) {
772
778
throw InvalidData::create ($ type , Value::none ());
773
779
}
774
780
0 commit comments