Skip to content

Instantly share code, notes, and snippets.

@ERuban
Created June 2, 2021 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ERuban/b0b1264e3166e16ab173b9640584aed2 to your computer and use it in GitHub Desktop.
Save ERuban/b0b1264e3166e16ab173b9640584aed2 to your computer and use it in GitHub Desktop.
[ApiPlatform] Remove endpoints from documentation
<?php
declare(strict_types=1);
namespace App\Swagger;
use ApiPlatform\Core\Documentation\Documentation;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
final class RemoveFromDocsDecorator implements NormalizerInterface
{
/**
* @var string
*/
private const IGNORE_API_RESOURCE_KEY = 'remove_from_docs';
private NormalizerInterface $decorated;
private ResourceMetadataFactoryInterface $resMetadataFactory;
public function __construct(NormalizerInterface $decorated, ResourceMetadataFactoryInterface $resMetadataFactory)
{
$this->decorated = $decorated;
$this->resMetadataFactory = $resMetadataFactory;
}
/**
* @param \ApiPlatform\Core\Documentation\Documentation $object
* @param mixed[]|null $context
*
* @return mixed[]
*
* @throws \Symfony\Component\Serializer\Exception\ExceptionInterface
*/
public function normalize($object, ?string $format = null, ?array $context = null): array
{
/** @var mixed[] $documentation */
$documentation = $this->decorated->normalize($this->filterApiResources($object), $format, $context ?? []);
return $documentation;
}
public function supportsNormalization($data, ?string $format = null): bool
{
return $this->decorated->supportsNormalization($data, $format);
}
private function filterApiResources(Documentation $documentation): Documentation
{
$classesToNormalize = [];
foreach ($documentation->getResourceNameCollection() as $resourceClass) {
$resourceMetadata = $this->resMetadataFactory->create($resourceClass);
if (
$this->isApiResourceShouldBeIgnored($resourceMetadata->getCollectionOperations()) ||
$this->isApiResourceShouldBeIgnored($resourceMetadata->getItemOperations())
) {
continue;
}
$classesToNormalize[] = $resourceClass;
}
$resourceCollection = new ResourceNameCollection($classesToNormalize);
return new Documentation(
$resourceCollection,
$documentation->getTitle(),
$documentation->getDescription(),
$documentation->getVersion()
);
}
/**
* @param mixed[] $resourceOperations
*/
private function isApiResourceShouldBeIgnored(array $resourceOperations): bool
{
foreach ($resourceOperations as $key => $value) {
if ($key === self::IGNORE_API_RESOURCE_KEY && $value) {
return true;
}
if (\is_array($value) && $this->isApiResourceShouldBeIgnored($value)) {
return true;
}
}
return false;
}
}
# ...
services:
# ...
App\Swagger\RemoveFromDocsDecorator:
decorates: 'api_platform.swagger.normalizer.api_gateway'
arguments: ['@.inner']
autoconfigure: false
* "post"={
* ...
* "openapi_context"={"remove_from_docs"=true}
* }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment