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 }
@@ -118,7 +119,9 @@ public function normalize(mixed $object, ?string $format = null, array $context
118119 $ context ['output ' ]['iri ' ] = null ;
119120 }
120121
121- if ($ this ->resourceClassResolver ->isResourceClass ($ resourceClass )) {
122+ if (isset ($ context ['item_uri_template ' ]) && $ this ->operationMetadataFactory ) {
123+ $ context ['output ' ]['operation ' ] = $ this ->operationMetadataFactory ->create ($ context ['item_uri_template ' ]);
124+ } elseif ($ this ->resourceClassResolver ->isResourceClass ($ resourceClass )) {
122125 $ context ['output ' ]['operation ' ] = $ this ->resourceMetadataCollectionFactory ->create ($ resourceClass )->getOperation ();
123126 }
124127
@@ -144,6 +147,11 @@ public function normalize(mixed $object, ?string $format = null, array $context
144147 }
145148
146149 $ operation = $ context ['operation ' ] ?? null ;
150+
151+ if ($ this ->operationMetadataFactory && isset ($ context ['item_uri_template ' ]) && !$ operation ) {
152+ $ operation = $ this ->operationMetadataFactory ->create ($ context ['item_uri_template ' ]);
153+ }
154+
147155 if ($ isResourceClass && !$ operation ) {
148156 $ operation = $ this ->resourceMetadataCollectionFactory ->create ($ resourceClass )->getOperation ();
149157 }
0 commit comments