Skip to content

Commit 373812a

Browse files
MLDMoritzteohhanhui
andcommitted
Fix removal of illegal characters in schema name for Amazon API Gateway
Co-authored-by: Teoh Han Hui <[email protected]>
1 parent a3f3fe1 commit 373812a

File tree

2 files changed

+476
-145
lines changed

2 files changed

+476
-145
lines changed

src/Swagger/Serializer/ApiGatewayNormalizer.php

Lines changed: 31 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ final class ApiGatewayNormalizer implements NormalizerInterface, CacheableSuppor
3131
public const API_GATEWAY = 'api_gateway';
3232

3333
private $documentationNormalizer;
34-
private $defaultContext = [self::API_GATEWAY => false];
34+
private $defaultContext = [
35+
self::API_GATEWAY => false,
36+
];
3537

3638
public function __construct(NormalizerInterface $documentationNormalizer, $defaultContext = [])
3739
{
@@ -66,19 +68,19 @@ public function normalize($object, $format = null, array $context = [])
6668
if (!preg_match('/^[a-zA-Z0-9._$-]+$/', $parameter['name'])) {
6769
unset($data['paths'][$path][$operation]['parameters'][$key]);
6870
}
69-
if (isset($parameter['schema']['$ref']) && !preg_match('/^#\/definitions\/[A-z]+$/', $parameter['schema']['$ref'])) {
70-
$data['paths'][$path][$operation]['parameters'][$key]['schema']['$ref'] = str_replace(['-', '_'], '', $parameter['schema']['$ref']);
71+
if (isset($parameter['schema']['$ref']) && $this->isLocalRef($parameter['schema']['$ref'])) {
72+
$data['paths'][$path][$operation]['parameters'][$key]['schema']['$ref'] = $this->normalizeRef($parameter['schema']['$ref']);
7173
}
7274
}
7375
$data['paths'][$path][$operation]['parameters'] = array_values($data['paths'][$path][$operation]['parameters']);
7476
}
7577
if (isset($options['responses'])) {
7678
foreach ($options['responses'] as $statusCode => $response) {
77-
if (isset($response['schema']['items']['$ref']) && !preg_match('/^#\/definitions\/[A-z]+$/', $response['schema']['items']['$ref'])) {
78-
$data['paths'][$path][$operation]['responses'][$statusCode]['schema']['items']['$ref'] = str_replace(['-', '_'], '', $response['schema']['items']['$ref']);
79+
if (isset($response['schema']['items']['$ref']) && $this->isLocalRef($response['schema']['items']['$ref'])) {
80+
$data['paths'][$path][$operation]['responses'][$statusCode]['schema']['items']['$ref'] = $this->normalizeRef($response['schema']['items']['$ref']);
7981
}
80-
if (isset($response['schema']['$ref']) && !preg_match('/^#\/definitions\/[A-z]+$/', $response['schema']['$ref'])) {
81-
$data['paths'][$path][$operation]['responses'][$statusCode]['schema']['$ref'] = str_replace(['-', '_'], '', $response['schema']['$ref']);
82+
if (isset($response['schema']['$ref']) && $this->isLocalRef($response['schema']['$ref'])) {
83+
$data['paths'][$path][$operation]['responses'][$statusCode]['schema']['$ref'] = $this->normalizeRef($response['schema']['$ref']);
8284
}
8385
}
8486
}
@@ -93,19 +95,19 @@ public function normalize($object, $format = null, array $context = [])
9395
if (isset($propertyOptions['readOnly'])) {
9496
unset($data['definitions'][$definition]['properties'][$property]['readOnly']);
9597
}
96-
if (isset($propertyOptions['$ref']) && !preg_match('/^#\/definitions\/[A-z]+$/', $propertyOptions['$ref'])) {
97-
$data['definitions'][$definition]['properties'][$property]['$ref'] = str_replace(['-', '_'], '', $propertyOptions['$ref']);
98+
if (isset($propertyOptions['$ref']) && $this->isLocalRef($propertyOptions['$ref'])) {
99+
$data['definitions'][$definition]['properties'][$property]['$ref'] = $this->normalizeRef($propertyOptions['$ref']);
98100
}
99-
if (isset($propertyOptions['items']['$ref']) && !preg_match('/^#\/definitions\/[A-z]+$/', $propertyOptions['items']['$ref'])) {
100-
$data['definitions'][$definition]['properties'][$property]['items']['$ref'] = str_replace(['-', '_'], '', $propertyOptions['items']['$ref']);
101+
if (isset($propertyOptions['items']['$ref']) && $this->isLocalRef($propertyOptions['items']['$ref'])) {
102+
$data['definitions'][$definition]['properties'][$property]['items']['$ref'] = $this->normalizeRef($propertyOptions['items']['$ref']);
101103
}
102104
}
103105
}
104106

105107
// $data['definitions'] is an instance of \ArrayObject
106108
foreach (array_keys($data['definitions']->getArrayCopy()) as $definition) {
107-
if (!preg_match('/^[A-z]+$/', (string) $definition)) {
108-
$data['definitions'][str_replace(['-', '_'], '', (string) $definition)] = $data['definitions'][$definition];
109+
if (!preg_match('/^[0-9A-Za-z]+$/', (string) $definition)) {
110+
$data['definitions'][preg_replace('/[^0-9A-Za-z]/', '', (string) $definition)] = $data['definitions'][$definition];
109111
unset($data['definitions'][$definition]);
110112
}
111113
}
@@ -128,4 +130,20 @@ public function hasCacheableSupportsMethod(): bool
128130
{
129131
return $this->documentationNormalizer instanceof CacheableSupportsMethodInterface && $this->documentationNormalizer->hasCacheableSupportsMethod();
130132
}
133+
134+
private function isLocalRef(string $ref): bool
135+
{
136+
return '#/' === substr($ref, 0, 2);
137+
}
138+
139+
private function normalizeRef(string $ref): string
140+
{
141+
$refParts = explode('/', $ref);
142+
143+
$schemaName = array_pop($refParts);
144+
$schemaName = preg_replace('/[^0-9A-Za-z]/', '', $schemaName);
145+
$refParts[] = $schemaName;
146+
147+
return implode('/', $refParts);
148+
}
131149
}

0 commit comments

Comments
 (0)