vendor/api-platform/core/src/OpenApi/Serializer/ApiGatewayNormalizer.php line 46

  1. <?php
  2. /*
  3.  * This file is part of the API Platform project.
  4.  *
  5.  * (c) Kévin Dunglas <dunglas@gmail.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\OpenApi\Serializer;
  12. use Symfony\Component\Serializer\Exception\UnexpectedValueException;
  13. use Symfony\Component\Serializer\Normalizer\CacheableSupportsMethodInterface;
  14. use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
  15. /**
  16.  * Removes features unsupported by Amazon API Gateway.
  17.  *
  18.  * @see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html
  19.  *
  20.  * @internal
  21.  *
  22.  * @author Vincent Chalamon <vincentchalamon@gmail.com>
  23.  */
  24. final class ApiGatewayNormalizer implements NormalizerInterfaceCacheableSupportsMethodInterface
  25. {
  26.     public const API_GATEWAY 'api_gateway';
  27.     private array $defaultContext = [
  28.         self::API_GATEWAY => false,
  29.     ];
  30.     public function __construct(private readonly NormalizerInterface $documentationNormalizer$defaultContext = [])
  31.     {
  32.         $this->defaultContext array_merge($this->defaultContext$defaultContext);
  33.     }
  34.     /**
  35.      * {@inheritdoc}
  36.      *
  37.      * @throws UnexpectedValueException
  38.      */
  39.     public function normalize(mixed $objectstring $format null, array $context = []): array|string|int|float|bool|\ArrayObject|null
  40.     {
  41.         $data $this->documentationNormalizer->normalize($object$format$context);
  42.         if (!\is_array($data)) {
  43.             throw new UnexpectedValueException('Expected data to be an array');
  44.         }
  45.         if (!($context[self::API_GATEWAY] ?? $this->defaultContext[self::API_GATEWAY])) {
  46.             return $data;
  47.         }
  48.         if (empty($data['basePath'])) {
  49.             $data['basePath'] = '/';
  50.         }
  51.         foreach ($data['paths'] as $path => $operations) {
  52.             foreach ($operations as $operation => $options) {
  53.                 if (isset($options['parameters'])) {
  54.                     foreach ($options['parameters'] as $key => $parameter) {
  55.                         if (!preg_match('/^[a-zA-Z0-9._$-]+$/', (string) $parameter['name'])) {
  56.                             unset($data['paths'][$path][$operation]['parameters'][$key]);
  57.                         }
  58.                         if (isset($parameter['schema']['$ref']) && $this->isLocalRef($parameter['schema']['$ref'])) {
  59.                             $data['paths'][$path][$operation]['parameters'][$key]['schema']['$ref'] = $this->normalizeRef($parameter['schema']['$ref']);
  60.                         }
  61.                     }
  62.                     $data['paths'][$path][$operation]['parameters'] = array_values($data['paths'][$path][$operation]['parameters']);
  63.                 }
  64.                 if (isset($options['responses'])) {
  65.                     foreach ($options['responses'] as $statusCode => $response) {
  66.                         if (isset($response['schema']['items']['$ref']) && $this->isLocalRef($response['schema']['items']['$ref'])) {
  67.                             $data['paths'][$path][$operation]['responses'][$statusCode]['schema']['items']['$ref'] = $this->normalizeRef($response['schema']['items']['$ref']);
  68.                         }
  69.                         if (isset($response['schema']['$ref']) && $this->isLocalRef($response['schema']['$ref'])) {
  70.                             $data['paths'][$path][$operation]['responses'][$statusCode]['schema']['$ref'] = $this->normalizeRef($response['schema']['$ref']);
  71.                         }
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.         foreach ($data['components']['schemas'] as $definition => $options) {
  77.             if (!isset($options['properties'])) {
  78.                 continue;
  79.             }
  80.             foreach ($options['properties'] as $property => $propertyOptions) {
  81.                 if (isset($propertyOptions['readOnly'])) {
  82.                     unset($data['components']['schemas'][$definition]['properties'][$property]['readOnly']);
  83.                 }
  84.                 if (isset($propertyOptions['$ref']) && $this->isLocalRef($propertyOptions['$ref'])) {
  85.                     $data['components']['schemas'][$definition]['properties'][$property]['$ref'] = $this->normalizeRef($propertyOptions['$ref']);
  86.                 }
  87.                 if (isset($propertyOptions['items']['$ref']) && $this->isLocalRef($propertyOptions['items']['$ref'])) {
  88.                     $data['components']['schemas'][$definition]['properties'][$property]['items']['$ref'] = $this->normalizeRef($propertyOptions['items']['$ref']);
  89.                 }
  90.             }
  91.         }
  92.         // $data['definitions'] is an instance of \ArrayObject
  93.         foreach (array_keys($data['components']['schemas']) as $definition) {
  94.             if (!preg_match('/^[0-9A-Za-z]+$/', (string) $definition)) {
  95.                 $data['components']['schemas'][preg_replace('/[^0-9A-Za-z]/''', (string) $definition)] = $data['components']['schemas'][$definition];
  96.                 unset($data['components']['schemas'][$definition]);
  97.             }
  98.         }
  99.         return $data;
  100.     }
  101.     /**
  102.      * {@inheritdoc}
  103.      */
  104.     public function supportsNormalization(mixed $datastring $format null, array $context = []): bool
  105.     {
  106.         return $this->documentationNormalizer->supportsNormalization($data$format);
  107.     }
  108.     /**
  109.      * {@inheritdoc}
  110.      */
  111.     public function hasCacheableSupportsMethod(): bool
  112.     {
  113.         return $this->documentationNormalizer instanceof CacheableSupportsMethodInterface && $this->documentationNormalizer->hasCacheableSupportsMethod();
  114.     }
  115.     private function isLocalRef(string $ref): bool
  116.     {
  117.         return str_starts_with($ref'#/');
  118.     }
  119.     private function normalizeRef(string $ref): string
  120.     {
  121.         $refParts explode('/'$ref);
  122.         $schemaName array_pop($refParts);
  123.         $schemaName preg_replace('/[^0-9A-Za-z]/'''$schemaName);
  124.         $refParts[] = $schemaName;
  125.         return implode('/'$refParts);
  126.     }
  127. }