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