20
20
use ApiPlatform \Exception \ItemNotFoundException ;
21
21
use ApiPlatform \Metadata \ApiProperty ;
22
22
use ApiPlatform \Metadata \CollectionOperationInterface ;
23
+ use ApiPlatform \Metadata \Exception \OperationNotFoundException ;
23
24
use ApiPlatform \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
24
25
use ApiPlatform \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
25
26
use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
@@ -512,12 +513,7 @@ protected function denormalizeCollection(string $attribute, ApiProperty $propert
512
513
513
514
$ collectionKeyType = $ type ->getCollectionKeyTypes ()[0 ] ?? null ;
514
515
$ collectionKeyBuiltinType = $ collectionKeyType ?->getBuiltinType();
515
- $ childContext = $ this ->createChildContext (['resource_class ' => $ className ] + $ context , $ attribute , $ format );
516
- unset($ childContext ['uri_variables ' ]);
517
- if ($ this ->resourceMetadataCollectionFactory ) {
518
- $ childContext ['operation ' ] = $ this ->resourceMetadataCollectionFactory ->create ($ className )->getOperation ();
519
- }
520
-
516
+ $ childContext = $ this ->createChildContext ($ this ->createOperationContext ($ context , $ className ), $ attribute , $ format );
521
517
$ values = [];
522
518
foreach ($ value as $ index => $ obj ) {
523
519
if (null !== $ collectionKeyBuiltinType && !\call_user_func ('is_ ' .$ collectionKeyBuiltinType , $ index )) {
@@ -637,8 +633,7 @@ protected function getAttributeValue(object $object, string $attribute, string $
637
633
}
638
634
639
635
$ resourceClass = $ this ->resourceClassResolver ->getResourceClass ($ attributeValue , $ className );
640
- $ childContext = $ this ->createChildContext ($ context , $ attribute , $ format );
641
- unset($ childContext ['iri ' ], $ childContext ['uri_variables ' ], $ childContext ['resource_class ' ], $ childContext ['operation ' ]);
636
+ $ childContext = $ this ->createChildContext ($ this ->createOperationContext ($ context , $ resourceClass ), $ attribute , $ format );
642
637
643
638
return $ this ->normalizeCollectionOfRelations ($ propertyMetadata , $ attributeValue , $ resourceClass , $ format , $ childContext );
644
639
}
@@ -653,12 +648,7 @@ protected function getAttributeValue(object $object, string $attribute, string $
653
648
}
654
649
655
650
$ resourceClass = $ this ->resourceClassResolver ->getResourceClass ($ attributeValue , $ className );
656
- $ childContext = $ this ->createChildContext ($ context , $ attribute , $ format );
657
- $ childContext ['resource_class ' ] = $ resourceClass ;
658
- if ($ this ->resourceMetadataCollectionFactory ) {
659
- $ childContext ['operation ' ] = $ this ->resourceMetadataCollectionFactory ->create ($ resourceClass )->getOperation ();
660
- }
661
- unset($ childContext ['iri ' ], $ childContext ['uri_variables ' ]);
651
+ $ childContext = $ this ->createChildContext ($ this ->createOperationContext ($ context , $ resourceClass ), $ attribute , $ format );
662
652
663
653
return $ this ->normalizeRelation ($ propertyMetadata , $ attributeValue , $ resourceClass , $ format , $ childContext );
664
654
}
@@ -670,17 +660,16 @@ protected function getAttributeValue(object $object, string $attribute, string $
670
660
unset($ context ['resource_class ' ]);
671
661
unset($ context ['force_resource_class ' ]);
672
662
663
+ // Anonymous resources
673
664
if ($ type && $ type ->getClassName ()) {
674
665
$ childContext = $ this ->createChildContext ($ context , $ attribute , $ format );
675
- unset($ childContext ['iri ' ], $ childContext ['uri_variables ' ]);
676
666
$ childContext ['output ' ]['gen_id ' ] = $ propertyMetadata ->getGenId () ?? true ;
677
667
678
668
return $ this ->serializer ->normalize ($ attributeValue , $ format , $ childContext );
679
669
}
680
670
681
671
if ($ type && 'array ' === $ type ->getBuiltinType ()) {
682
672
$ childContext = $ this ->createChildContext ($ context , $ attribute , $ format );
683
- unset($ childContext ['iri ' ], $ childContext ['uri_variables ' ]);
684
673
685
674
return $ this ->serializer ->normalize ($ attributeValue , $ format , $ childContext );
686
675
}
@@ -804,12 +793,7 @@ private function createAndValidateAttributeValue(string $attribute, mixed $value
804
793
&& $ this ->resourceClassResolver ->isResourceClass ($ className )
805
794
) {
806
795
$ resourceClass = $ this ->resourceClassResolver ->getResourceClass (null , $ className );
807
- $ childContext = $ this ->createChildContext ($ context , $ attribute , $ format );
808
- $ childContext ['resource_class ' ] = $ resourceClass ;
809
- unset($ childContext ['uri_variables ' ]);
810
- if ($ this ->resourceMetadataCollectionFactory ) {
811
- $ childContext ['operation ' ] = $ this ->resourceMetadataCollectionFactory ->create ($ resourceClass )->getOperation ();
812
- }
796
+ $ childContext = $ this ->createChildContext ($ this ->createOperationContext ($ context , $ resourceClass ), $ attribute , $ format );
813
797
814
798
return $ this ->denormalizeRelation ($ attribute , $ propertyMetadata , $ resourceClass , $ value , $ format , $ childContext );
815
799
}
@@ -899,4 +883,29 @@ private function setValue(object $object, string $attributeName, mixed $value):
899
883
// Properties not found are ignored
900
884
}
901
885
}
886
+
887
+ private function createOperationContext (array $ context , string $ resourceClass = null ): array
888
+ {
889
+ if (isset ($ context ['operation ' ]) && !isset ($ context ['root_operation ' ])) {
890
+ $ context ['root_operation ' ] = $ context ['operation ' ];
891
+ $ context ['root_operation_name ' ] = $ context ['operation_name ' ];
892
+ }
893
+
894
+ unset($ context ['iri ' ], $ context ['uri_variables ' ]);
895
+ if (!$ resourceClass ) {
896
+ return $ context ;
897
+ }
898
+
899
+ unset($ context ['operation ' ], $ context ['operation_name ' ]);
900
+ $ context ['resource_class ' ] = $ resourceClass ;
901
+ if ($ this ->resourceMetadataCollectionFactory ) {
902
+ try {
903
+ $ context ['operation ' ] = $ this ->resourceMetadataCollectionFactory ->create ($ resourceClass )->getOperation ();
904
+ $ context ['operation_name ' ] = $ context ['operation ' ]->getName ();
905
+ } catch (OperationNotFoundException ) {
906
+ }
907
+ }
908
+
909
+ return $ context ;
910
+ }
902
911
}
0 commit comments