|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the OpenapiBundle package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Niels Nijens <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Nijens\OpenapiBundle\Routing; |
|
15
|
|
|
|
|
16
|
|
|
use Nijens\OpenapiBundle\Controller\CatchAllController; |
|
17
|
|
|
use Nijens\OpenapiBundle\Json\JsonPointer; |
|
18
|
|
|
use Nijens\OpenapiBundle\Json\SchemaLoaderInterface; |
|
19
|
|
|
use stdClass; |
|
20
|
|
|
use Symfony\Component\Config\FileLocatorInterface; |
|
21
|
|
|
use Symfony\Component\Config\Loader\FileLoader; |
|
22
|
|
|
use Symfony\Component\HttpFoundation\Request; |
|
23
|
|
|
use Symfony\Component\Routing\Route; |
|
24
|
|
|
use Symfony\Component\Routing\RouteCollection; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Loads the paths from an OpenAPI specification as routes. |
|
28
|
|
|
* |
|
29
|
|
|
* @author Niels Nijens <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
class RouteLoader extends FileLoader |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
public const TYPE = 'openapi'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var SchemaLoaderInterface |
|
40
|
|
|
*/ |
|
41
|
|
|
private $schemaLoader; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var bool |
|
45
|
|
|
*/ |
|
46
|
|
|
private $useOperationIdAsRouteName; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Constructs a new RouteLoader instance. |
|
50
|
|
|
*/ |
|
51
|
|
|
public function __construct( |
|
52
|
|
|
FileLocatorInterface $locator, |
|
53
|
|
|
SchemaLoaderInterface $schemaLoader, |
|
54
|
|
|
bool $useOperationIdAsRouteName = false |
|
55
|
|
|
) { |
|
56
|
|
|
parent::__construct($locator); |
|
57
|
|
|
|
|
58
|
|
|
$this->schemaLoader = $schemaLoader; |
|
59
|
|
|
$this->useOperationIdAsRouteName = $useOperationIdAsRouteName; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* {@inheritdoc} |
|
64
|
|
|
*/ |
|
65
|
|
|
public function supports($resource, $type = null): bool |
|
66
|
|
|
{ |
|
67
|
|
|
return self::TYPE === $type; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* {@inheritdoc} |
|
72
|
|
|
*/ |
|
73
|
|
|
public function load($resource, $type = null): RouteCollection |
|
74
|
|
|
{ |
|
75
|
|
|
$file = $this->getLocator()->locate($resource, null, true); |
|
76
|
|
|
|
|
77
|
|
|
$schema = $this->schemaLoader->load($file); |
|
|
|
|
|
|
78
|
|
|
|
|
79
|
|
|
$jsonPointer = new JsonPointer($schema); |
|
80
|
|
|
|
|
81
|
|
|
$routeCollection = new RouteCollection(); |
|
82
|
|
|
$routeCollection->addResource($this->schemaLoader->getFileResource($file)); |
|
|
|
|
|
|
83
|
|
|
|
|
84
|
|
|
$paths = get_object_vars($jsonPointer->get('/paths')); |
|
85
|
|
|
foreach ($paths as $path => $pathItem) { |
|
86
|
|
|
$this->parsePathItem($jsonPointer, $file, $routeCollection, $path, $pathItem); |
|
|
|
|
|
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$this->addDefaultRoutes($routeCollection, $file); |
|
|
|
|
|
|
90
|
|
|
|
|
91
|
|
|
return $routeCollection; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Parses a path item of the OpenAPI specification for a route. |
|
96
|
|
|
*/ |
|
97
|
|
|
private function parsePathItem( |
|
98
|
|
|
JsonPointer $jsonPointer, |
|
99
|
|
|
string $resource, |
|
100
|
|
|
RouteCollection $collection, |
|
101
|
|
|
string $path, |
|
102
|
|
|
stdClass $pathItem |
|
103
|
|
|
): void { |
|
104
|
|
|
$operations = get_object_vars($pathItem); |
|
105
|
|
|
foreach ($operations as $requestMethod => $operation) { |
|
106
|
|
|
if ($this->isValidRequestMethod($requestMethod) === false) { |
|
107
|
|
|
return; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
$this->parseOperation($jsonPointer, $resource, $collection, $path, $requestMethod, $operation, $pathItem); |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Parses an operation of the OpenAPI specification for a route. |
|
116
|
|
|
*/ |
|
117
|
|
|
private function parseOperation( |
|
118
|
|
|
JsonPointer $jsonPointer, |
|
119
|
|
|
string $resource, |
|
120
|
|
|
RouteCollection $collection, |
|
121
|
|
|
string $path, |
|
122
|
|
|
string $requestMethod, |
|
123
|
|
|
stdClass $operation, |
|
124
|
|
|
stdClass $pathItem |
|
125
|
|
|
): void { |
|
126
|
|
|
$defaults = []; |
|
127
|
|
|
$openapiRouteContext = [ |
|
128
|
|
|
RouteContext::RESOURCE => $resource, |
|
129
|
|
|
]; |
|
130
|
|
|
|
|
131
|
|
|
$this->parseOpenapiBundleSpecificationExtension($operation, $defaults, $openapiRouteContext); |
|
132
|
|
|
$this->addRouteContextForValidation( |
|
133
|
|
|
$jsonPointer, |
|
134
|
|
|
$path, |
|
135
|
|
|
$requestMethod, |
|
136
|
|
|
$operation, |
|
137
|
|
|
$pathItem, |
|
138
|
|
|
$openapiRouteContext |
|
139
|
|
|
); |
|
140
|
|
|
|
|
141
|
|
|
$defaults[RouteContext::REQUEST_ATTRIBUTE] = $openapiRouteContext; |
|
142
|
|
|
|
|
143
|
|
|
$route = new Route($path, $defaults, []); |
|
144
|
|
|
$route->setMethods($requestMethod); |
|
145
|
|
|
|
|
146
|
|
|
$routeName = null; |
|
147
|
|
|
if ($this->useOperationIdAsRouteName && isset($operation->operationId)) { |
|
148
|
|
|
$routeName = $operation->operationId; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
$collection->add( |
|
152
|
|
|
$routeName ?? $this->createRouteName($path, $requestMethod), |
|
153
|
|
|
$route |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
private function parseOpenapiBundleSpecificationExtension(stdClass $operation, array &$defaults, array &$openapiRouteContext): void |
|
158
|
|
|
{ |
|
159
|
|
|
if (isset($operation->{'x-openapi-bundle'}->controller)) { |
|
160
|
|
|
$defaults['_controller'] = $operation->{'x-openapi-bundle'}->controller; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
if (isset($defaults['_controller']) === false && isset($operation->{'x-symfony-controller'})) { |
|
164
|
|
|
$defaults['_controller'] = $operation->{'x-symfony-controller'}; |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if (isset($operation->{'x-openapi-bundle'}->deserializationObject)) { |
|
168
|
|
|
$openapiRouteContext[RouteContext::DESERIALIZATION_OBJECT] = $operation->{'x-openapi-bundle'}->deserializationObject; |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
if (isset($operation->{'x-openapi-bundle'}->deserializationObjectArgumentName)) { |
|
172
|
|
|
$openapiRouteContext[RouteContext::DESERIALIZATION_OBJECT_ARGUMENT_NAME] = $operation->{'x-openapi-bundle'}->deserializationObjectArgumentName; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
if (isset($operation->{'x-openapi-bundle'}->additionalRouteAttributes)) { |
|
176
|
|
|
$additionalRouteAttributes = get_object_vars($operation->{'x-openapi-bundle'}->additionalRouteAttributes); |
|
177
|
|
|
foreach ($additionalRouteAttributes as $key => $value) { |
|
178
|
|
|
$defaults[$key] = $value; |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
private function addRouteContextForValidation( |
|
184
|
|
|
JsonPointer $jsonPointer, |
|
185
|
|
|
string $path, |
|
186
|
|
|
string $requestMethod, |
|
187
|
|
|
stdClass $operation, |
|
188
|
|
|
stdClass $pathItem, |
|
189
|
|
|
array &$openapiRouteContext |
|
190
|
|
|
): void { |
|
191
|
|
|
$openapiRouteContext[RouteContext::REQUEST_BODY_REQUIRED] = false; |
|
192
|
|
|
if (isset($operation->requestBody->required)) { |
|
193
|
|
|
$openapiRouteContext[RouteContext::REQUEST_BODY_REQUIRED] = $operation->requestBody->required; |
|
194
|
|
|
} |
|
195
|
|
|
|
|
196
|
|
|
$openapiRouteContext[RouteContext::REQUEST_ALLOWED_CONTENT_TYPES] = []; |
|
197
|
|
|
if (isset($operation->requestBody->content)) { |
|
198
|
|
|
$openapiRouteContext[RouteContext::REQUEST_ALLOWED_CONTENT_TYPES] = array_keys( |
|
199
|
|
|
get_object_vars($operation->requestBody->content) |
|
200
|
|
|
); |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
$openapiRouteContext[RouteContext::REQUEST_VALIDATE_QUERY_PARAMETERS] = []; |
|
204
|
|
|
$parameters = array_merge( |
|
205
|
|
|
$pathItem->parameters ?? [], |
|
206
|
|
|
$operation->parameters ?? [] |
|
207
|
|
|
); |
|
208
|
|
|
foreach ($parameters as $parameter) { |
|
209
|
|
|
if ($parameter->in !== 'query') { |
|
210
|
|
|
continue; |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
|
|
$openapiRouteContext[RouteContext::REQUEST_VALIDATE_QUERY_PARAMETERS][$parameter->name] = json_encode($parameter); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
if (isset($operation->requestBody->content->{'application/json'}->schema)) { |
|
217
|
|
|
$openapiRouteContext[RouteContext::REQUEST_BODY_SCHEMA] = json_encode($operation->requestBody->content->{'application/json'}->schema); |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if (isset($operation->requestBody->content->{'application/json'})) { |
|
221
|
|
|
$openapiRouteContext[RouteContext::JSON_REQUEST_VALIDATION_POINTER] = sprintf( |
|
222
|
|
|
'/paths/%s/%s/requestBody/content/%s/schema', |
|
223
|
|
|
$jsonPointer->escape($path), |
|
224
|
|
|
$requestMethod, |
|
225
|
|
|
$jsonPointer->escape('application/json') |
|
226
|
|
|
); |
|
227
|
|
|
} |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
/** |
|
231
|
|
|
* Returns true when the provided request method is a valid request method in the OpenAPI specification. |
|
232
|
|
|
*/ |
|
233
|
|
|
private function isValidRequestMethod(string $requestMethod): bool |
|
234
|
|
|
{ |
|
235
|
|
|
return in_array( |
|
236
|
|
|
strtoupper($requestMethod), |
|
237
|
|
|
[ |
|
238
|
|
|
Request::METHOD_GET, |
|
239
|
|
|
Request::METHOD_PUT, |
|
240
|
|
|
Request::METHOD_POST, |
|
241
|
|
|
Request::METHOD_DELETE, |
|
242
|
|
|
Request::METHOD_OPTIONS, |
|
243
|
|
|
Request::METHOD_HEAD, |
|
244
|
|
|
Request::METHOD_PATCH, |
|
245
|
|
|
Request::METHOD_TRACE, |
|
246
|
|
|
] |
|
247
|
|
|
); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
/** |
|
251
|
|
|
* Creates a route name based on the path and request method. |
|
252
|
|
|
*/ |
|
253
|
|
|
private function createRouteName(string $path, string $requestMethod): string |
|
254
|
|
|
{ |
|
255
|
|
|
return sprintf('%s_%s', |
|
256
|
|
|
trim(preg_replace('/[^a-zA-Z0-9]+/', '_', $path), '_'), |
|
257
|
|
|
$requestMethod |
|
258
|
|
|
); |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* Adds a catch-all route to handle responses for non-existing routes. |
|
263
|
|
|
*/ |
|
264
|
|
|
private function addDefaultRoutes(RouteCollection $collection, string $resource): void |
|
265
|
|
|
{ |
|
266
|
|
|
$catchAllRoute = new Route( |
|
267
|
|
|
'/{catchall}', |
|
268
|
|
|
[ |
|
269
|
|
|
'_controller' => CatchAllController::CONTROLLER_REFERENCE, |
|
270
|
|
|
RouteContext::REQUEST_ATTRIBUTE => [RouteContext::RESOURCE => $resource], |
|
271
|
|
|
], |
|
272
|
|
|
['catchall' => '.*'] |
|
273
|
|
|
); |
|
274
|
|
|
|
|
275
|
|
|
$collection->add('catch_all', $catchAllRoute); |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
|