• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

0 of 2 new or added lines in 1 file covered. (0.0%)

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

15.22
/src/OpenApi/Serializer/ApiGatewayNormalizer.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\OpenApi\Serializer;
15

16
use Symfony\Component\Serializer\Exception\UnexpectedValueException;
17
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
18

19
/**
20
 * Removes features unsupported by Amazon API Gateway.
21
 *
22
 * @see https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-known-issues.html
23
 *
24
 * @internal
25
 *
26
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
27
 */
28
final class ApiGatewayNormalizer implements NormalizerInterface
29
{
30
    public const API_GATEWAY = 'api_gateway';
31
    private array $defaultContext = [
32
        self::API_GATEWAY => false,
33
    ];
34

35
    public function __construct(private readonly NormalizerInterface $documentationNormalizer, $defaultContext = [])
36
    {
UNCOV
37
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
950✔
38
    }
39

40
    /**
41
     * {@inheritdoc}
42
     *
43
     * @throws UnexpectedValueException
44
     */
45
    public function normalize(mixed $object, ?string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
46
    {
UNCOV
47
        $data = $this->documentationNormalizer->normalize($object, $format, $context);
22✔
UNCOV
48
        if (!\is_array($data)) {
22✔
49
            throw new UnexpectedValueException('Expected data to be an array');
×
50
        }
51

UNCOV
52
        if (!($context[self::API_GATEWAY] ?? $this->defaultContext[self::API_GATEWAY])) {
22✔
UNCOV
53
            return $data;
22✔
54
        }
55

56
        if (empty($data['basePath'])) {
×
57
            $data['basePath'] = '/';
×
58
        }
59

60
        foreach ($data['paths'] as $path => $operations) {
×
61
            foreach ($operations as $operation => $options) {
×
62
                if (isset($options['parameters'])) {
×
63
                    foreach ($options['parameters'] as $key => $parameter) {
×
64
                        if (!preg_match('/^[a-zA-Z0-9._$-]+$/', (string) $parameter['name'])) {
×
65
                            unset($data['paths'][$path][$operation]['parameters'][$key]);
×
66
                        }
67
                        if (isset($parameter['schema']['$ref']) && $this->isLocalRef($parameter['schema']['$ref'])) {
×
68
                            $data['paths'][$path][$operation]['parameters'][$key]['schema']['$ref'] = $this->normalizeRef($parameter['schema']['$ref']);
×
69
                        }
70
                    }
71
                    $data['paths'][$path][$operation]['parameters'] = array_values($data['paths'][$path][$operation]['parameters']);
×
72
                }
73
                if (isset($options['responses'])) {
×
74
                    foreach ($options['responses'] as $statusCode => $response) {
×
75
                        if (isset($response['schema']['items']['$ref']) && $this->isLocalRef($response['schema']['items']['$ref'])) {
×
76
                            $data['paths'][$path][$operation]['responses'][$statusCode]['schema']['items']['$ref'] = $this->normalizeRef($response['schema']['items']['$ref']);
×
77
                        }
78
                        if (isset($response['schema']['$ref']) && $this->isLocalRef($response['schema']['$ref'])) {
×
79
                            $data['paths'][$path][$operation]['responses'][$statusCode]['schema']['$ref'] = $this->normalizeRef($response['schema']['$ref']);
×
80
                        }
81
                    }
82
                }
83
            }
84
        }
85

86
        foreach ($data['components']['schemas'] as $definition => $options) {
×
87
            if (!isset($options['properties'])) {
×
88
                continue;
×
89
            }
90
            foreach ($options['properties'] as $property => $propertyOptions) {
×
91
                if (isset($propertyOptions['readOnly'])) {
×
92
                    unset($data['components']['schemas'][$definition]['properties'][$property]['readOnly']);
×
93
                }
94
                if (isset($propertyOptions['$ref']) && $this->isLocalRef($propertyOptions['$ref'])) {
×
95
                    $data['components']['schemas'][$definition]['properties'][$property]['$ref'] = $this->normalizeRef($propertyOptions['$ref']);
×
96
                }
97
                if (isset($propertyOptions['items']['$ref']) && $this->isLocalRef($propertyOptions['items']['$ref'])) {
×
98
                    $data['components']['schemas'][$definition]['properties'][$property]['items']['$ref'] = $this->normalizeRef($propertyOptions['items']['$ref']);
×
99
                }
100
            }
101
        }
102

103
        // $data['definitions'] is an instance of \ArrayObject
104
        foreach (array_keys($data['components']['schemas']) as $definition) {
×
105
            if (!preg_match('/^[0-9A-Za-z]+$/', (string) $definition)) {
×
106
                $data['components']['schemas'][preg_replace('/[^0-9A-Za-z]/', '', (string) $definition)] = $data['components']['schemas'][$definition];
×
107
                unset($data['components']['schemas'][$definition]);
×
108
            }
109
        }
110

111
        return $data;
×
112
    }
113

114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function supportsNormalization(mixed $data, ?string $format = null, array $context = []): bool
118
    {
UNCOV
119
        return $this->documentationNormalizer->supportsNormalization($data, $format);
22✔
120
    }
121

122
    public function getSupportedTypes($format): array
123
    {
UNCOV
124
        return $this->documentationNormalizer->getSupportedTypes($format);
912✔
125
    }
126

127
    private function isLocalRef(string $ref): bool
128
    {
129
        return str_starts_with($ref, '#/');
×
130
    }
131

132
    private function normalizeRef(string $ref): string
133
    {
134
        $refParts = explode('/', $ref);
×
135

136
        $schemaName = array_pop($refParts);
×
137
        $schemaName = preg_replace('/[^0-9A-Za-z]/', '', $schemaName);
×
138
        $refParts[] = $schemaName;
×
139

140
        return implode('/', $refParts);
×
141
    }
142
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc