1818use ApiPlatform \Metadata \Exception \ItemNotFoundException ;
1919use ApiPlatform \Metadata \HttpOperation ;
2020use ApiPlatform \Metadata \IriConverterInterface ;
21+ use ApiPlatform \Metadata \Operation \Factory \OperationMetadataFactoryInterface ;
2122use ApiPlatform \Metadata \Property \Factory \PropertyMetadataFactoryInterface ;
2223use ApiPlatform \Metadata \Property \Factory \PropertyNameCollectionFactoryInterface ;
2324use ApiPlatform \Metadata \Resource \Factory \ResourceMetadataCollectionFactoryInterface ;
@@ -70,7 +71,7 @@ final class ItemNormalizer extends AbstractItemNormalizer
7071 '@vocab ' ,
7172 ];
7273
73- public function __construct (ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory , PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , IriConverterInterface $ iriConverter , ResourceClassResolverInterface $ resourceClassResolver , private readonly ContextBuilderInterface $ contextBuilder , ?PropertyAccessorInterface $ propertyAccessor = null , ?NameConverterInterface $ nameConverter = null , ?ClassMetadataFactoryInterface $ classMetadataFactory = null , array $ defaultContext = [], ?ResourceAccessCheckerInterface $ resourceAccessChecker = null , protected ?TagCollectorInterface $ tagCollector = null )
74+ public function __construct (ResourceMetadataCollectionFactoryInterface $ resourceMetadataCollectionFactory , PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , IriConverterInterface $ iriConverter , ResourceClassResolverInterface $ resourceClassResolver , private readonly ContextBuilderInterface $ contextBuilder , ?PropertyAccessorInterface $ propertyAccessor = null , ?NameConverterInterface $ nameConverter = null , ?ClassMetadataFactoryInterface $ classMetadataFactory = null , array $ defaultContext = [], ?ResourceAccessCheckerInterface $ resourceAccessChecker = null , protected ?TagCollectorInterface $ tagCollector = null , private ? OperationMetadataFactoryInterface $ operationMetadataFactory = null )
7475 {
7576 parent ::__construct ($ propertyNameCollectionFactory , $ propertyMetadataFactory , $ iriConverter , $ resourceClassResolver , $ propertyAccessor , $ nameConverter , $ classMetadataFactory , $ defaultContext , $ resourceMetadataCollectionFactory , $ resourceAccessChecker , $ tagCollector );
7677 }
@@ -115,7 +116,9 @@ public function normalize(mixed $object, ?string $format = null, array $context
115116 $ context ['output ' ]['iri ' ] = null ;
116117 }
117118
118- if ($ this ->resourceClassResolver ->isResourceClass ($ resourceClass )) {
119+ if (isset ($ context ['item_uri_template ' ]) && $ this ->operationMetadataFactory ) {
120+ $ context ['output ' ]['operation ' ] = $ this ->operationMetadataFactory ->create ($ context ['item_uri_template ' ]);
121+ } elseif ($ this ->resourceClassResolver ->isResourceClass ($ resourceClass )) {
119122 $ context ['output ' ]['operation ' ] = $ this ->resourceMetadataCollectionFactory ->create ($ resourceClass )->getOperation ();
120123 }
121124
@@ -141,6 +144,11 @@ public function normalize(mixed $object, ?string $format = null, array $context
141144 }
142145
143146 $ operation = $ context ['operation ' ] ?? null ;
147+
148+ if ($ this ->operationMetadataFactory && isset ($ context ['item_uri_template ' ]) && !$ operation ) {
149+ $ operation = $ this ->operationMetadataFactory ->create ($ context ['item_uri_template ' ]);
150+ }
151+
144152 if ($ isResourceClass && !$ operation ) {
145153 $ operation = $ this ->resourceMetadataCollectionFactory ->create ($ resourceClass )->getOperation ();
146154 }
0 commit comments