Created
June 2, 2021 12:55
[ApiPlatform] Remove endpoints from documentation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ... | |
services: | |
# ... | |
App\Swagger\RemoveFromDocsDecorator: | |
decorates: 'api_platform.swagger.normalizer.api_gateway' | |
arguments: ['@.inner'] | |
autoconfigure: false |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
* "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