11
11
12
12
namespace ApiPlatform \Core \Swagger \Serializer ;
13
13
14
+ use ApiPlatform \Core \Api \FilterCollection ;
14
15
use ApiPlatform \Core \Api \OperationMethodResolverInterface ;
15
16
use ApiPlatform \Core \Api \ResourceClassResolverInterface ;
16
17
use ApiPlatform \Core \Api \UrlGeneratorInterface ;
@@ -43,8 +44,9 @@ final class DocumentationNormalizer implements NormalizerInterface
43
44
private $ operationMethodResolver ;
44
45
private $ operationPathResolver ;
45
46
private $ urlGenerator ;
47
+ private $ filterCollection ;
46
48
47
- public function __construct (ResourceMetadataFactoryInterface $ resourceMetadataFactory , PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , ResourceClassResolverInterface $ resourceClassResolver , OperationMethodResolverInterface $ operationMethodResolver , OperationPathResolverInterface $ operationPathResolver , UrlGeneratorInterface $ urlGenerator )
49
+ public function __construct (ResourceMetadataFactoryInterface $ resourceMetadataFactory , PropertyNameCollectionFactoryInterface $ propertyNameCollectionFactory , PropertyMetadataFactoryInterface $ propertyMetadataFactory , ResourceClassResolverInterface $ resourceClassResolver , OperationMethodResolverInterface $ operationMethodResolver , OperationPathResolverInterface $ operationPathResolver , UrlGeneratorInterface $ urlGenerator, FilterCollection $ filterCollection = null )
48
50
{
49
51
$ this ->resourceMetadataFactory = $ resourceMetadataFactory ;
50
52
$ this ->propertyNameCollectionFactory = $ propertyNameCollectionFactory ;
@@ -53,6 +55,7 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
53
55
$ this ->operationMethodResolver = $ operationMethodResolver ;
54
56
$ this ->operationPathResolver = $ operationPathResolver ;
55
57
$ this ->urlGenerator = $ urlGenerator ;
58
+ $ this ->filterCollection = $ filterCollection ;
56
59
}
57
60
58
61
/**
@@ -196,6 +199,10 @@ private function updateGetOperation(\ArrayObject $pathOperation, array $mimeType
196
199
],
197
200
];
198
201
202
+ if (!isset ($ pathOperation ['parameters ' ]) && count ($ parameters = $ this ->getFiltersParameters ($ resourceClass , $ operationName , $ resourceMetadata )) > 0 ) {
203
+ $ pathOperation ['parameters ' ] = $ parameters ;
204
+ }
205
+
199
206
return $ pathOperation ;
200
207
}
201
208
@@ -421,7 +428,6 @@ private function getPropertySchema(PropertyMetadata $propertyMetadata) : \ArrayO
421
428
* Gets the Swagger's type corresponding to the given PHP's type.
422
429
*
423
430
* @param string $type
424
- *
425
431
* @param bool $isCollection
426
432
* @param string $className
427
433
* @param bool $readableLink
@@ -503,22 +509,45 @@ private function computeDoc(Documentation $documentation, \ArrayObject $definiti
503
509
return $ doc ;
504
510
}
505
511
512
+ /**
513
+ * Gets Swagger parameters corresponding to enabled filters.
514
+ *
515
+ * @param string $resourceClass
516
+ * @param string $operationName
517
+ * @param ResourceMetadata $resourceMetadata
518
+ *
519
+ * @return array
520
+ */
506
521
private function getFiltersParameters (string $ resourceClass , string $ operationName , ResourceMetadata $ resourceMetadata ) : array
507
522
{
508
- $ parameters = new \ArrayObject ();
509
- foreach ($ resourceMetadata ->getCollectionOperationAttribute ($ operationName , 'filters ' , [], true ) as $ filter ) {
510
- foreach ($ filter ->getDescription ($ resourceClass ) as $ variable => $ data ) {
523
+ if (null === $ this ->filterCollection ) {
524
+ return [];
525
+ }
526
+
527
+ $ parameters = [];
528
+ $ resourceFilters = $ resourceMetadata ->getCollectionOperationAttribute ($ operationName , 'filters ' , [], true );
529
+ foreach ($ this ->filterCollection as $ filterName => $ filter ) {
530
+ if (!in_array ($ filterName , $ resourceFilters )) {
531
+ continue ;
532
+ }
533
+
534
+ foreach ($ filter ->getDescription ($ resourceClass ) as $ name => $ data ) {
511
535
$ parameter = [
512
- 'name ' => $ variable ,
536
+ 'name ' => $ name ,
513
537
'in ' => 'query ' ,
514
538
'required ' => $ data ['required ' ],
515
- 'type ' => $ this ->getType ($ data ['type ' ])
516
539
];
540
+ $ parameter += $ this ->getType ($ data ['type ' ], false );
541
+
542
+ if (isset ($ data ['swagger ' ])) {
543
+ $ parameter = $ data ['swagger ' ] + $ parameter ;
544
+ }
517
545
518
- $ data ['swagger ' ] ?? $ parameter = $ data ['swagger ' ] + $ parameter ;
519
546
$ parameters [] = $ parameter ;
520
547
}
521
548
}
549
+
550
+ return $ parameters ;
522
551
}
523
552
524
553
/**
0 commit comments