|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the API Platform project. |
| 5 | + * |
| 6 | + * (c) Kévin Dunglas <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +declare(strict_types=1); |
| 13 | + |
| 14 | +namespace ApiPlatform\Core\Swagger\Serializer; |
| 15 | + |
| 16 | +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 17 | + |
| 18 | +/** |
| 19 | + * Removes features unsupported by Amazon API Gateway. |
| 20 | + * |
| 21 | + * @see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html |
| 22 | + * |
| 23 | + * @internal |
| 24 | + * |
| 25 | + * @author Vincent Chalamon <[email protected]> |
| 26 | + */ |
| 27 | +final class ApiGatewayNormalizer implements NormalizerInterface |
| 28 | +{ |
| 29 | + private $documentationNormalizer; |
| 30 | + |
| 31 | + public function __construct(NormalizerInterface $documentationNormalizer) |
| 32 | + { |
| 33 | + $this->documentationNormalizer = $documentationNormalizer; |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * {@inheritdoc} |
| 38 | + */ |
| 39 | + public function normalize($object, $format = null, array $context = []) |
| 40 | + { |
| 41 | + $data = $this->documentationNormalizer->normalize($object, $format, $context); |
| 42 | + if (empty($data['basePath'])) { |
| 43 | + $data['basePath'] = '/'; |
| 44 | + } |
| 45 | + |
| 46 | + if (!($context['api_gateway'] ?? false)) { |
| 47 | + return $data; |
| 48 | + } |
| 49 | + |
| 50 | + foreach ($data['paths'] as $path => $operations) { |
| 51 | + foreach ($operations as $operation => $options) { |
| 52 | + if (isset($options['parameters'])) { |
| 53 | + foreach ($options['parameters'] as $key => $parameter) { |
| 54 | + if (!preg_match('/^[a-zA-Z0-9._$-]+$/', $parameter['name'])) { |
| 55 | + unset($data['paths'][$path][$operation]['parameters'][$key]); |
| 56 | + } |
| 57 | + if (isset($parameter['schema']['$ref']) && !preg_match('/^#\/definitions\/[A-z]+$/', $parameter['schema']['$ref'])) { |
| 58 | + $data['paths'][$path][$operation]['parameters'][$key]['schema']['$ref'] = str_replace(['-', '_'], '', $parameter['schema']['$ref']); |
| 59 | + } |
| 60 | + } |
| 61 | + $data['paths'][$path][$operation]['parameters'] = array_values($data['paths'][$path][$operation]['parameters']); |
| 62 | + } |
| 63 | + if (isset($options['responses'])) { |
| 64 | + foreach ($options['responses'] as $statusCode => $response) { |
| 65 | + if (isset($response['schema']['items']['$ref']) && !preg_match('/^#\/definitions\/[A-z]+$/', $response['schema']['items']['$ref'])) { |
| 66 | + $data['paths'][$path][$operation]['responses'][$statusCode]['schema']['items']['$ref'] = str_replace(['-', '_'], '', $response['schema']['items']['$ref']); |
| 67 | + } |
| 68 | + if (isset($response['schema']['$ref']) && !preg_match('/^#\/definitions\/[A-z]+$/', $response['schema']['$ref'])) { |
| 69 | + $data['paths'][$path][$operation]['responses'][$statusCode]['schema']['$ref'] = str_replace(['-', '_'], '', $response['schema']['$ref']); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + foreach ($data['definitions'] 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['definitions'][$definition]['properties'][$property]['readOnly']); |
| 83 | + } |
| 84 | + if (isset($propertyOptions['$ref']) && !preg_match('/^#\/definitions\/[A-z]+$/', $propertyOptions['$ref'])) { |
| 85 | + $data['definitions'][$definition]['properties'][$property]['$ref'] = str_replace(['-', '_'], '', $propertyOptions['$ref']); |
| 86 | + } |
| 87 | + if (isset($propertyOptions['items']['$ref']) && !preg_match('/^#\/definitions\/[A-z]+$/', $propertyOptions['items']['$ref'])) { |
| 88 | + $data['definitions'][$definition]['properties'][$property]['items']['$ref'] = str_replace(['-', '_'], '', $propertyOptions['items']['$ref']); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // $data['definitions'] is an instance of \ArrayObject |
| 94 | + foreach (array_keys($data['definitions']->getArrayCopy()) as $definition) { |
| 95 | + if (!preg_match('/^[A-z]+$/', $definition)) { |
| 96 | + $data['definitions'][str_replace(['-', '_'], '', $definition)] = $data['definitions'][$definition]; |
| 97 | + unset($data['definitions'][$definition]); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + return $data; |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * {@inheritdoc} |
| 106 | + */ |
| 107 | + public function supportsNormalization($data, $format = null) |
| 108 | + { |
| 109 | + return $this->documentationNormalizer->supportsNormalization($data, $format); |
| 110 | + } |
| 111 | +} |
0 commit comments