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

api-platform / core / 14008635868

22 Mar 2025 12:39PM UTC coverage: 8.52% (+0.005%) from 8.515%
14008635868

Pull #7042

github

web-flow
Merge fdd88ef56 into 47a6dffbb
Pull Request #7042: Purge parent collections in inheritance cases

4 of 9 new or added lines in 1 file covered. (44.44%)

540 existing lines in 57 files now uncovered.

13394 of 157210 relevant lines covered (8.52%)

22.93 hits per line

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

91.3
/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
    {
37
        $this->defaultContext = array_merge($this->defaultContext, $defaultContext);
2,008✔
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
    {
47
        $data = $this->documentationNormalizer->normalize($object, $format, $context);
47✔
48
        if (!\is_array($data)) {
47✔
49
            throw new UnexpectedValueException('Expected data to be an array');
×
50
        }
51

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

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

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

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

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

UNCOV
111
        return $data;
1✔
112
    }
113

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

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

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

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

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

UNCOV
140
        return implode('/', $refParts);
1✔
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