@@ -31,7 +31,9 @@ final class ApiGatewayNormalizer implements NormalizerInterface, CacheableSuppor
31
31
public const API_GATEWAY = 'api_gateway ' ;
32
32
33
33
private $ documentationNormalizer ;
34
- private $ defaultContext = [self ::API_GATEWAY => false ];
34
+ private $ defaultContext = [
35
+ self ::API_GATEWAY => false ,
36
+ ];
35
37
36
38
public function __construct (NormalizerInterface $ documentationNormalizer , $ defaultContext = [])
37
39
{
@@ -66,19 +68,19 @@ public function normalize($object, $format = null, array $context = [])
66
68
if (!preg_match ('/^[a-zA-Z0-9._$-]+$/ ' , $ parameter ['name ' ])) {
67
69
unset($ data ['paths ' ][$ path ][$ operation ]['parameters ' ][$ key ]);
68
70
}
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 ' ]);
71
73
}
72
74
}
73
75
$ data ['paths ' ][$ path ][$ operation ]['parameters ' ] = array_values ($ data ['paths ' ][$ path ][$ operation ]['parameters ' ]);
74
76
}
75
77
if (isset ($ options ['responses ' ])) {
76
78
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 ' ]);
79
81
}
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 ' ]);
82
84
}
83
85
}
84
86
}
@@ -93,19 +95,19 @@ public function normalize($object, $format = null, array $context = [])
93
95
if (isset ($ propertyOptions ['readOnly ' ])) {
94
96
unset($ data ['definitions ' ][$ definition ]['properties ' ][$ property ]['readOnly ' ]);
95
97
}
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 ' ]);
98
100
}
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 ' ]);
101
103
}
102
104
}
103
105
}
104
106
105
107
// $data['definitions'] is an instance of \ArrayObject
106
108
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 ];
109
111
unset($ data ['definitions ' ][$ definition ]);
110
112
}
111
113
}
@@ -128,4 +130,20 @@ public function hasCacheableSupportsMethod(): bool
128
130
{
129
131
return $ this ->documentationNormalizer instanceof CacheableSupportsMethodInterface && $ this ->documentationNormalizer ->hasCacheableSupportsMethod ();
130
132
}
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
+ }
131
149
}
0 commit comments