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

Open in your IDE?
  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 $documentationNormalizer;
  28.     private $defaultContext = [
  29.         self::API_GATEWAY => false,
  30.     ];
  31.     public function __construct(NormalizerInterface $documentationNormalizer$defaultContext = [])
  32.     {
  33.         $this->documentationNormalizer $documentationNormalizer;
  34.         $this->defaultContext array_merge($this->defaultContext$defaultContext);
  35.     }
  36.     /**
  37.      * {@inheritdoc}
  38.      *
  39.      * @throws UnexpectedValueException
  40.      *
  41.      * @return array|string|int|float|bool|\ArrayObject|null
  42.      */
  43.     public function normalize($object$format null, array $context = [])
  44.     {
  45.         $data $this->documentationNormalizer->normalize($object$format$context);
  46.         if (!\is_array($data)) {
  47.             throw new UnexpectedValueException('Expected data to be an array');
  48.         }
  49.         if (!($context[self::API_GATEWAY] ?? $this->defaultContext[self::API_GATEWAY])) {
  50.             return $data;
  51.         }
  52.         if (empty($data['basePath'])) {
  53.             $data['basePath'] = '/';
  54.         }
  55.         foreach ($data['paths'] as $path => $operations) {
  56.             foreach ($operations as $operation => $options) {
  57.                 if (isset($options['parameters'])) {
  58.                     foreach ($options['parameters'] as $key => $parameter) {
  59.                         if (!preg_match('/^[a-zA-Z0-9._$-]+$/'$parameter['name'])) {
  60.                             unset($data['paths'][$path][$operation]['parameters'][$key]);
  61.                         }
  62.                         if (isset($parameter['schema']['$ref']) && $this->isLocalRef($parameter['schema']['$ref'])) {
  63.                             $data['paths'][$path][$operation]['parameters'][$key]['schema']['$ref'] = $this->normalizeRef($parameter['schema']['$ref']);
  64.                         }
  65.                     }
  66.                     $data['paths'][$path][$operation]['parameters'] = array_values($data['paths'][$path][$operation]['parameters']);
  67.                 }
  68.                 if (isset($options['responses'])) {
  69.                     foreach ($options['responses'] as $statusCode => $response) {
  70.                         if (isset($response['schema']['items']['$ref']) && $this->isLocalRef($response['schema']['items']['$ref'])) {
  71.                             $data['paths'][$path][$operation]['responses'][$statusCode]['schema']['items']['$ref'] = $this->normalizeRef($response['schema']['items']['$ref']);
  72.                         }
  73.                         if (isset($response['schema']['$ref']) && $this->isLocalRef($response['schema']['$ref'])) {
  74.                             $data['paths'][$path][$operation]['responses'][$statusCode]['schema']['$ref'] = $this->normalizeRef($response['schema']['$ref']);
  75.                         }
  76.                     }
  77.                 }
  78.             }
  79.         }
  80.         foreach ($data['definitions'] as $definition => $options) {
  81.             if (!isset($options['properties'])) {
  82.                 continue;
  83.             }
  84.             foreach ($options['properties'] as $property => $propertyOptions) {
  85.                 if (isset($propertyOptions['readOnly'])) {
  86.                     unset($data['definitions'][$definition]['properties'][$property]['readOnly']);
  87.                 }
  88.                 if (isset($propertyOptions['$ref']) && $this->isLocalRef($propertyOptions['$ref'])) {
  89.                     $data['definitions'][$definition]['properties'][$property]['$ref'] = $this->normalizeRef($propertyOptions['$ref']);
  90.                 }
  91.                 if (isset($propertyOptions['items']['$ref']) && $this->isLocalRef($propertyOptions['items']['$ref'])) {
  92.                     $data['definitions'][$definition]['properties'][$property]['items']['$ref'] = $this->normalizeRef($propertyOptions['items']['$ref']);
  93.                 }
  94.             }
  95.         }
  96.         // $data['definitions'] is an instance of \ArrayObject
  97.         foreach (array_keys($data['definitions']->getArrayCopy()) as $definition) {
  98.             if (!preg_match('/^[0-9A-Za-z]+$/', (string) $definition)) {
  99.                 $data['definitions'][preg_replace('/[^0-9A-Za-z]/''', (string) $definition)] = $data['definitions'][$definition];
  100.                 unset($data['definitions'][$definition]);
  101.             }
  102.         }
  103.         return $data;
  104.     }
  105.     /**
  106.      * {@inheritdoc}
  107.      */
  108.     public function supportsNormalization($data$format null, array $context = []): bool
  109.     {
  110.         return $this->documentationNormalizer->supportsNormalization($data$format);
  111.     }
  112.     /**
  113.      * {@inheritdoc}
  114.      */
  115.     public function hasCacheableSupportsMethod(): bool
  116.     {
  117.         return $this->documentationNormalizer instanceof CacheableSupportsMethodInterface && $this->documentationNormalizer->hasCacheableSupportsMethod();
  118.     }
  119.     private function isLocalRef(string $ref): bool
  120.     {
  121.         return '#/' === substr($ref02);
  122.     }
  123.     private function normalizeRef(string $ref): string
  124.     {
  125.         $refParts explode('/'$ref);
  126.         $schemaName array_pop($refParts);
  127.         $schemaName preg_replace('/[^0-9A-Za-z]/'''$schemaName);
  128.         $refParts[] = $schemaName;
  129.         return implode('/'$refParts);
  130.     }
  131. }
  132. class_alias(ApiGatewayNormalizer::class, \ApiPlatform\Core\Swagger\Serializer\ApiGatewayNormalizer::class);