Skip to content

Commit c75329f

Browse files
Update operations: A required operation is no longer necessary. (#2145)
1 parent fa3bd28 commit c75329f

File tree

1 file changed

+2
-66
lines changed

1 file changed

+2
-66
lines changed

core/operations.md

+2-66
Original file line numberDiff line numberDiff line change
@@ -572,74 +572,10 @@ namespace App\Entity;
572572

573573
use ApiPlatform\Metadata\ApiResource;
574574

575-
#[ApiResource]
575+
#[ApiResource(operations: [])]
576576
class Weather
577577
{
578578
// ...
579579
```
580580

581-
This will expose the `Weather` model, but also all the default CRUD routes: `GET`, `PATCH`, `DELETE` and `POST`, which is nonsense in our context.
582-
Since we are required to expose at least one route, let's expose just one:
583-
584-
```php
585-
<?php
586-
// api/src/Entity/Weather.php
587-
namespace App\Entity;
588-
589-
use ApiPlatform\Metadata\ApiResource;
590-
use ApiPlatform\Metadata\Get;
591-
592-
#[ApiResource(operations: [
593-
new Get(controller: SomeRandomController::class)
594-
])]
595-
class Weather
596-
{
597-
// ...
598-
}
599-
```
600-
601-
This way, we expose a route that will do… nothing. Note that the controller does not even need to exist.
602-
603-
It's almost done, we have just one final issue: our fake item operation is visible in the API docs.
604-
To remove it, we will need to [decorate the Swagger documentation](openapi.md#overriding-the-openapi-specification).
605-
Then, remove the route from the decorator:
606-
607-
```php
608-
<?php
609-
// src/OpenApi/OpenApiFactory.php
610-
namespace App\OpenApi;
611-
612-
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
613-
use ApiPlatform\OpenApi\OpenApi;
614-
use ApiPlatform\OpenApi\Model;
615-
616-
final class OpenApiFactory implements OpenApiFactoryInterface
617-
{
618-
private $decorated;
619-
620-
public function __construct(OpenApiFactoryInterface $decorated)
621-
{
622-
$this->decorated = $decorated;
623-
}
624-
625-
public function __invoke(array $context = []): OpenApi
626-
{
627-
$openApi = $this->decorated->__invoke($context);
628-
629-
$paths = $openApi->getPaths()->getPaths();
630-
631-
$filteredPaths = new Model\Paths();
632-
foreach ($paths as $path => $pathItem) {
633-
// If a prefix is configured on API Platform's routes, it must appear here.
634-
if ($path === '/weathers/{id}') {
635-
continue;
636-
}
637-
$filteredPaths->addPath($path, $pathItem);
638-
}
639-
640-
return $openApi->withPaths($filteredPaths);
641-
}
642-
}
643-
```
644-
645-
That's it: your route is gone!
581+
That's it!

0 commit comments

Comments
 (0)