15
15
16
16
use ApiPlatform \Core \Api \FilterCollection ;
17
17
use ApiPlatform \Core \Api \FilterLocatorTrait ;
18
+ use ApiPlatform \Core \Api \OperationAwareFormatsProviderInterface ;
18
19
use ApiPlatform \Core \Api \OperationMethodResolverInterface ;
19
20
use ApiPlatform \Core \Api \OperationType ;
20
21
use ApiPlatform \Core \Api \ResourceClassResolverInterface ;
@@ -68,11 +69,12 @@ final class DocumentationNormalizer implements NormalizerInterface, CacheableSup
68
69
private $ paginationPageParameterName ;
69
70
private $ clientItemsPerPage ;
70
71
private $ itemsPerPageParameterName ;
72
+ private $ formatsProvider ;
71
73
72
74
/**
73
75
* @param ContainerInterface|FilterCollection|null $filterLocator The new filter locator or the deprecated filter collection
74
76
*/
75
- public function __construct (ResourceMetadataFactoryInterface $ resourceMetadataFactory , PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , ResourceClassResolverInterface $ resourceClassResolver , OperationMethodResolverInterface $ operationMethodResolver , OperationPathResolverInterface $ operationPathResolver , UrlGeneratorInterface $ urlGenerator = null , $ filterLocator = null , NameConverterInterface $ nameConverter = null , $ oauthEnabled = false , $ oauthType = '' , $ oauthFlow = '' , $ oauthTokenUrl = '' , $ oauthAuthorizationUrl = '' , array $ oauthScopes = [], array $ apiKeys = [], SubresourceOperationFactoryInterface $ subresourceOperationFactory = null , $ paginationEnabled = true , $ paginationPageParameterName = 'page ' , $ clientItemsPerPage = false , $ itemsPerPageParameterName = 'itemsPerPage ' )
77
+ public function __construct (ResourceMetadataFactoryInterface $ resourceMetadataFactory , PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , ResourceClassResolverInterface $ resourceClassResolver , OperationMethodResolverInterface $ operationMethodResolver , OperationPathResolverInterface $ operationPathResolver , UrlGeneratorInterface $ urlGenerator = null , $ filterLocator = null , NameConverterInterface $ nameConverter = null , $ oauthEnabled = false , $ oauthType = '' , $ oauthFlow = '' , $ oauthTokenUrl = '' , $ oauthAuthorizationUrl = '' , array $ oauthScopes = [], array $ apiKeys = [], SubresourceOperationFactoryInterface $ subresourceOperationFactory = null , $ paginationEnabled = true , $ paginationPageParameterName = 'page ' , $ clientItemsPerPage = false , $ itemsPerPageParameterName = 'itemsPerPage ' , OperationAwareFormatsProviderInterface $ formatsProvider = null )
76
78
{
77
79
if ($ urlGenerator ) {
78
80
@trigger_error (sprintf ('Passing an instance of %s to %s() is deprecated since version 2.1 and will be removed in 3.0. ' , UrlGeneratorInterface::class, __METHOD__ ), E_USER_DEPRECATED );
@@ -99,6 +101,7 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
99
101
$ this ->apiKeys = $ apiKeys ;
100
102
$ this ->clientItemsPerPage = $ clientItemsPerPage ;
101
103
$ this ->itemsPerPageParameterName = $ itemsPerPageParameterName ;
104
+ $ this ->formatsProvider = $ formatsProvider ;
102
105
}
103
106
104
107
/**
@@ -130,7 +133,11 @@ public function normalize($object, $format = null, array $context = [])
130
133
$ pathOperation = new \ArrayObject ([]);
131
134
$ pathOperation ['tags ' ] = $ subresourceOperation ['shortNames ' ];
132
135
$ pathOperation ['operationId ' ] = $ operationId ;
133
- $ pathOperation ['produces ' ] = $ mimeTypes ;
136
+ if (null !== $ this ->formatsProvider ) {
137
+ $ responseFormats = $ this ->formatsProvider ->getFormatsFromOperation ($ subresourceOperation ['resource_class ' ], $ operationName , OperationType::SUBRESOURCE );
138
+ $ responseMimeTypes = $ this ->extractMimeTypes ($ responseFormats );
139
+ }
140
+ $ pathOperation ['produces ' ] = $ responseMimeTypes ?? $ mimeTypes ;
134
141
$ pathOperation ['summary ' ] = sprintf ('Retrieves %s%s resource%s. ' , $ subresourceOperation ['collection ' ] ? 'the collection of ' : 'a ' , $ subresourceOperation ['shortNames ' ][0 ], $ subresourceOperation ['collection ' ] ? 's ' : '' );
135
142
$ pathOperation ['responses ' ] = [
136
143
'200 ' => $ subresourceOperation ['collection ' ] ? [
@@ -223,17 +230,20 @@ private function getPathOperation(string $operationName, array $operation, strin
223
230
if ($ resourceMetadata ->getTypedOperationAttribute ($ operationType , $ operationName , 'deprecation_reason ' , null , true )) {
224
231
$ pathOperation ['deprecated ' ] = true ;
225
232
}
226
-
233
+ if (null !== $ this ->formatsProvider ) {
234
+ $ responseFormats = $ this ->formatsProvider ->getFormatsFromOperation ($ resourceClass , $ operationName , $ operationType );
235
+ $ responseMimeTypes = $ this ->extractMimeTypes ($ responseFormats );
236
+ }
227
237
switch ($ method ) {
228
238
case 'GET ' :
229
- return $ this ->updateGetOperation ($ pathOperation , $ mimeTypes , $ operationType , $ resourceMetadata , $ resourceClass , $ resourceShortName , $ operationName , $ definitions );
239
+ return $ this ->updateGetOperation ($ pathOperation , $ responseMimeTypes ?? $ mimeTypes , $ operationType , $ resourceMetadata , $ resourceClass , $ resourceShortName , $ operationName , $ definitions );
230
240
case 'POST ' :
231
- return $ this ->updatePostOperation ($ pathOperation , $ mimeTypes , $ operationType , $ resourceMetadata , $ resourceClass , $ resourceShortName , $ operationName , $ definitions );
241
+ return $ this ->updatePostOperation ($ pathOperation , $ responseMimeTypes ?? $ mimeTypes , $ operationType , $ resourceMetadata , $ resourceClass , $ resourceShortName , $ operationName , $ definitions );
232
242
case 'PATCH ' :
233
243
$ pathOperation ['summary ' ] ?? $ pathOperation ['summary ' ] = sprintf ('Updates the %s resource. ' , $ resourceShortName );
234
244
// no break
235
245
case 'PUT ' :
236
- return $ this ->updatePutOperation ($ pathOperation , $ mimeTypes , $ operationType , $ resourceMetadata , $ resourceClass , $ resourceShortName , $ operationName , $ definitions );
246
+ return $ this ->updatePutOperation ($ pathOperation , $ responseMimeTypes ?? $ mimeTypes , $ operationType , $ resourceMetadata , $ resourceClass , $ resourceShortName , $ operationName , $ definitions );
237
247
case 'DELETE ' :
238
248
return $ this ->updateDeleteOperation ($ pathOperation , $ resourceShortName );
239
249
}
@@ -678,4 +688,16 @@ private function getSerializerContext(string $operationType, bool $denormalizati
678
688
679
689
return $ resourceMetadata ->getItemOperationAttribute ($ operationName , $ contextKey , null , true );
680
690
}
691
+
692
+ private function extractMimeTypes (array $ responseFormats ): array
693
+ {
694
+ $ responseMimeTypes = [];
695
+ foreach ($ responseFormats as $ mimeTypes ) {
696
+ foreach ($ mimeTypes as $ mimeType ) {
697
+ $ responseMimeTypes [] = $ mimeType ;
698
+ }
699
+ }
700
+
701
+ return $ responseMimeTypes ;
702
+ }
681
703
}
0 commit comments