Skip to content

Commit 9cd2293

Browse files
authored
Update docs with OpenApiFactoryInterface. (#1342)
1 parent 50aade5 commit 9cd2293

File tree

2 files changed

+36
-23
lines changed

2 files changed

+36
-23
lines changed

core/openapi.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,16 @@ use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
6969
use ApiPlatform\Core\OpenApi\OpenApi;
7070
use ApiPlatform\Core\OpenApi\Model;
7171

72-
class OpenApiFactory implements OpenApiFactoryInterface {
72+
class OpenApiFactory implements OpenApiFactoryInterface
73+
{
7374
private $decorated;
7475

75-
public function __construct(OpenApiFactoryInterface $decorated)
76+
public function __construct(OpenApiFactoryInterface $decorated)
7677
{
7778
$this->decorated = $decorated;
7879
}
7980

80-
public function __invoke(array $context = []): OpenApi
81+
public function __invoke(array $context = []): OpenApi
8182
{
8283
$openApi = $this->decorated->__invoke($context);
8384
$pathItem = $openApi->getPaths()->getPath('/api/grumpy_pizzas/{id}');
@@ -163,7 +164,7 @@ class Product // The class name will be used to name exposed resources
163164
* )
164165
*/
165166
public $timestamp;
166-
167+
167168
// ...
168169
}
169170
```
@@ -539,7 +540,7 @@ The [info object](https://swagger.io/specification/#info-object) provides metada
539540

540541
```yaml
541542
api_platform:
542-
543+
543544
# The title of the API.
544545
title: 'API title'
545546
@@ -548,7 +549,7 @@ api_platform:
548549
549550
# The version of the API.
550551
version: '0.0.0'
551-
552+
552553
openapi:
553554
# The contact information for the exposed API.
554555
contact:
@@ -565,5 +566,5 @@ api_platform:
565566
# The license name used for the API.
566567
name:
567568
# URL to the license used for the API. MUST be in the format of a URL.
568-
url:
569+
url:
569570
```

core/operations.md

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -422,7 +422,7 @@ class CreateBookPublication
422422
public function __construct(
423423
private BookPublishingHandler $bookPublishingHandler
424424
) {}
425-
425+
426426
#[Route(
427427
path: '/books/{id}/publication',
428428
name: 'book_post_publication',
@@ -620,34 +620,46 @@ class Weather
620620
This way, we expose a route that will do… nothing. Note that the controller does not even need to exist.
621621

622622
It's almost done, we have just one final issue: our fake item operation is visible in the API docs.
623-
To remove it, we will need to [decorate the Swagger documentation](swagger.md#overriding-the-openapi-specification).
623+
To remove it, we will need to [decorate the Swagger documentation](openapi.md#overriding-the-openapi-specification).
624624
Then, remove the route from the decorator:
625625

626626
```php
627627
<?php
628-
// src/Swagger/SwaggerDecorator.php
628+
// src/OpenApi/OpenApiFactory.php
629629

630-
namespace App\Swagger;
630+
namespace App\OpenApi;
631631

632-
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
632+
use ApiPlatform\Core\OpenApi\Factory\OpenApiFactoryInterface;
633+
use ApiPlatform\Core\OpenApi\OpenApi;
634+
use ApiPlatform\Core\OpenApi\Model;
633635

634-
final class SwaggerDecorator implements NormalizerInterface
636+
final class OpenApiFactory implements OpenApiFactoryInterface
635637
{
636-
public function __construct(
637-
private NormalizerInterface $decorated
638-
) {}
638+
private $decorated;
639+
640+
public function __construct(OpenApiFactoryInterface $decorated)
641+
{
642+
$this->decorated = $decorated;
643+
}
639644

640-
public function normalize($object, string $format = null, array $context = [])
645+
public function __invoke(array $context = []): OpenApi
641646
{
642-
$docs = $this->decorated->normalize($object, $format, $context);
647+
$openApi = $this->decorated->__invoke($context);
643648

644-
// If a prefix is configured on API Platform's routes, it must appear here.
645-
unset($docs['paths']['/weathers/{id}']);
649+
$paths = $openApi->getPaths()->getPaths();
646650

647-
return $docs;
648-
}
651+
$filteredPaths = new Model\Paths();
652+
foreach ($paths as $path => $pathItem) {
653+
// If a prefix is configured on API Platform's routes, it must appear here.
654+
if ($path === '/weathers/{id}') {
655+
continue;
656+
}
657+
$filteredPaths->addPath($path, $pathItem);
658+
}
649659

650-
// ...
660+
return $openApi->withPaths($filteredPaths);
661+
}
662+
}
651663
```
652664

653665
That's it: your route is gone!

0 commit comments

Comments
 (0)