1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
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
|
|
|
namespace ApiPlatform\Core\Swagger; |
13
|
|
|
|
14
|
|
|
use ApiPlatform\Core\Api\IriConverterInterface; |
15
|
|
|
use ApiPlatform\Core\Api\OperationMethodResolverInterface; |
16
|
|
|
use ApiPlatform\Core\Api\ResourceClassResolverInterface; |
17
|
|
|
use ApiPlatform\Core\Documentation\ApiDocumentationBuilderInterface; |
18
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
19
|
|
|
use ApiPlatform\Core\JsonLd\ContextBuilderInterface; |
20
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
21
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
22
|
|
|
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; |
23
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
24
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface; |
25
|
|
|
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
26
|
|
|
use Symfony\Component\PropertyInfo\Type; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Creates a machine readable Swagger API documentation. |
30
|
|
|
* |
31
|
|
|
* @author Amrouche Hamza <[email protected]> |
32
|
|
|
* @author Kévin Dunglas <[email protected]> |
33
|
|
|
*/ |
34
|
|
|
final class ApiDocumentationBuilder implements ApiDocumentationBuilderInterface |
35
|
|
|
{ |
36
|
|
|
const SWAGGER_VERSION = '2.0'; |
37
|
|
|
|
38
|
|
|
private $resourceNameCollectionFactory; |
39
|
|
|
private $resourceMetadataFactory; |
40
|
|
|
private $propertyNameCollectionFactory; |
41
|
|
|
private $propertyMetadataFactory; |
42
|
|
|
private $contextBuilder; |
43
|
|
|
private $resourceClassResolver; |
44
|
|
|
private $operationMethodResolver; |
45
|
|
|
private $title; |
46
|
|
|
private $description; |
47
|
|
|
private $iriConverter; |
48
|
|
|
private $version; |
49
|
|
|
private $mimeTypes = []; |
50
|
|
|
|
51
|
|
|
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, ContextBuilderInterface $contextBuilder, ResourceClassResolverInterface $resourceClassResolver, OperationMethodResolverInterface $operationMethodResolver, IriConverterInterface $iriConverter, array $formats, string $title, string $description, string $version = null) |
52
|
|
|
{ |
53
|
|
|
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory; |
54
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
55
|
|
|
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
56
|
|
|
$this->propertyMetadataFactory = $propertyMetadataFactory; |
57
|
|
|
$this->contextBuilder = $contextBuilder; |
58
|
|
|
$this->resourceClassResolver = $resourceClassResolver; |
59
|
|
|
$this->operationMethodResolver = $operationMethodResolver; |
60
|
|
|
$this->title = $title; |
61
|
|
|
$this->description = $description; |
62
|
|
|
$this->iriConverter = $iriConverter; |
63
|
|
|
$this->version = $version; |
64
|
|
|
|
65
|
|
|
foreach ($formats as $format => $mimeTypes) { |
66
|
|
|
foreach ($mimeTypes as $mimeType) { |
67
|
|
|
$this->mimeTypes[] = $mimeType; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getApiDocumentation() : array |
76
|
|
|
{ |
77
|
|
|
$classes = []; |
78
|
|
|
$operation = []; |
79
|
|
|
$customOperations = []; |
80
|
|
|
$itemOperationsDocs = []; |
81
|
|
|
$definitions = []; |
82
|
|
|
|
83
|
|
|
foreach ($this->resourceNameCollectionFactory->create() as $resourceClass) { |
84
|
|
|
$operation['item'] = []; |
85
|
|
|
$operation['collection'] = []; |
86
|
|
|
$customOperations['item'] = []; |
87
|
|
|
$customOperations['collection'] = []; |
88
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
89
|
|
|
|
90
|
|
|
$shortName = $resourceMetadata->getShortName(); |
91
|
|
|
$prefixedShortName = ($iri = $resourceMetadata->getIri()) ? $iri : '#'.$shortName; |
92
|
|
|
|
93
|
|
|
$class = [ |
94
|
|
|
'name' => $shortName, |
95
|
|
|
'externalDocs' => ['url' => $prefixedShortName], |
96
|
|
|
]; |
97
|
|
|
|
98
|
|
|
if ($description = $resourceMetadata->getDescription()) { |
99
|
|
|
$class = [ |
100
|
|
|
'name' => $shortName, |
101
|
|
|
'description' => $description, |
102
|
|
|
'externalDocs' => ['url' => $prefixedShortName], |
103
|
|
|
]; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$attributes = $resourceMetadata->getAttributes(); |
107
|
|
|
$context = []; |
108
|
|
|
|
109
|
|
|
if (isset($attributes['normalization_context']['groups'])) { |
110
|
|
|
$context['serializer_groups'] = $attributes['normalization_context']['groups']; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
View Code Duplication |
if (isset($attributes['denormalization_context']['groups'])) { |
|
|
|
|
114
|
|
|
$context['serializer_groups'] = isset($context['serializer_groups']) ? array_merge($context['serializer_groups'], $attributes['denormalization_context']['groups']) : $context['serializer_groups']; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
$definitions[$shortName] = [ |
118
|
|
|
'type' => 'object', |
119
|
|
|
'xml' => ['name' => 'response'], |
120
|
|
|
]; |
121
|
|
|
|
122
|
|
|
foreach ($this->propertyNameCollectionFactory->create($resourceClass, $context) as $propertyName) { |
123
|
|
|
$propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $propertyName); |
124
|
|
|
|
125
|
|
|
if ($propertyMetadata->isRequired()) { |
126
|
|
|
$definitions[$shortName]['required'][] = $propertyName; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
$range = $this->getRange($propertyMetadata); |
130
|
|
|
if (empty($range)) { |
131
|
|
|
continue; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($propertyMetadata->getDescription()) { |
|
|
|
|
135
|
|
|
$definitions[$shortName]['properties'][$propertyName]['description'] = $propertyMetadata->getDescription(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if ($range['complex']) { |
139
|
|
|
$definitions[$shortName]['properties'][$propertyName] = ['$ref' => $range['value']]; |
140
|
|
|
} else { |
141
|
|
|
$definitions[$shortName]['properties'][$propertyName] = ['type' => $range['value']]; |
142
|
|
|
|
143
|
|
|
if (isset($range['example'])) { |
144
|
|
|
$definitions[$shortName]['properties'][$propertyName]['example'] = $range['example']; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
View Code Duplication |
if ($operations = $resourceMetadata->getItemOperations()) { |
|
|
|
|
150
|
|
|
foreach ($operations as $operationName => $itemOperation) { |
151
|
|
|
$method = $this->operationMethodResolver->getItemOperationMethod($resourceClass, $operationName); |
152
|
|
|
$swaggerOperation = $this->getSwaggerOperation($resourceClass, $resourceMetadata, $operationName, $itemOperation, $prefixedShortName, false, $definitions, $method); |
153
|
|
|
$operation['item'] = array_merge($operation['item'], $swaggerOperation); |
154
|
|
|
if ($operationName !== strtolower($method)) { |
155
|
|
|
$customOperations['item'][] = $operationName; |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
} |
159
|
|
|
|
160
|
|
View Code Duplication |
if ($operations = $resourceMetadata->getCollectionOperations()) { |
|
|
|
|
161
|
|
|
foreach ($operations as $operationName => $collectionOperation) { |
162
|
|
|
$method = $this->operationMethodResolver->getCollectionOperationMethod($resourceClass, $operationName); |
163
|
|
|
$swaggerOperation = $this->getSwaggerOperation($resourceClass, $resourceMetadata, $operationName, $collectionOperation, $prefixedShortName, true, $definitions, $method); |
164
|
|
|
$operation['collection'] = array_merge($operation['collection'], $swaggerOperation); |
165
|
|
|
if ($operationName !== strtolower($method)) { |
166
|
|
|
$customOperations['collection'][] = $operationName; |
167
|
|
|
} |
168
|
|
|
} |
169
|
|
|
} |
170
|
|
|
try { |
171
|
|
|
$resourceClassIri = $this->iriConverter->getIriFromResourceClass($resourceClass); |
172
|
|
|
$itemOperationsDocs[$resourceClassIri] = $operation['collection']; |
173
|
|
|
|
174
|
|
View Code Duplication |
if (!empty($customOperations['collection'])) { |
|
|
|
|
175
|
|
|
foreach ($customOperations['collection'] as $customOperation) { |
176
|
|
|
$path = $resourceMetadata->getCollectionOperationAttribute($customOperation, 'path'); |
177
|
|
|
if (null !== $path) { |
178
|
|
|
$method = $this->operationMethodResolver->getCollectionOperationMethod($resourceClass, $customOperation); |
179
|
|
|
$customSwaggerOperation = $this->getSwaggerOperation($resourceClass, $resourceMetadata, $customOperation, [$method], $prefixedShortName, true, $definitions, $method); |
180
|
|
|
|
181
|
|
|
$itemOperationsDocs[$path] = $customSwaggerOperation; |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
|
187
|
|
|
$resourceClassIri .= '/{id}'; |
188
|
|
|
|
189
|
|
|
$itemOperationsDocs[$resourceClassIri] = $operation['item']; |
190
|
|
|
|
191
|
|
View Code Duplication |
if (!empty($customOperations['item'])) { |
|
|
|
|
192
|
|
|
foreach ($customOperations['item'] as $customOperation) { |
193
|
|
|
$path = $resourceMetadata->getItemOperationAttribute($customOperation, 'path'); |
194
|
|
|
if (null !== $path) { |
195
|
|
|
$method = $this->operationMethodResolver->getItemOperationMethod($resourceClass, $customOperation); |
196
|
|
|
$customSwaggerOperation = $this->getSwaggerOperation($resourceClass, $resourceMetadata, $customOperation, [$method], $prefixedShortName, true, $definitions, $method); |
197
|
|
|
|
198
|
|
|
$itemOperationsDocs[$path] = $customSwaggerOperation; |
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
} catch (InvalidArgumentException $e) { |
|
|
|
|
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$classes[] = $class; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
$doc['swagger'] = self::SWAGGER_VERSION; |
|
|
|
|
209
|
|
|
if ('' !== $this->title) { |
210
|
|
|
$doc['info']['title'] = $this->title; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
if ('' !== $this->description) { |
214
|
|
|
$doc['info']['description'] = $this->description; |
215
|
|
|
} |
216
|
|
|
$doc['info']['version'] = $this->version ?? '0.0.0'; |
217
|
|
|
$doc['definitions'] = $definitions; |
218
|
|
|
$doc['externalDocs'] = ['description' => 'Find more about API Platform', 'url' => 'https://api-platform.com']; |
219
|
|
|
$doc['tags'] = $classes; |
220
|
|
|
$doc['paths'] = $itemOperationsDocs; |
221
|
|
|
|
222
|
|
|
return $doc; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Gets and populates if applicable a Swagger operation. |
227
|
|
|
*/ |
228
|
|
|
private function getSwaggerOperation(string $resourceClass, ResourceMetadata $resourceMetadata, string $operationName, array $operation, string $prefixedShortName, bool $collection, array $properties, string $method) : array |
|
|
|
|
229
|
|
|
{ |
230
|
|
|
$methodSwagger = strtolower($method); |
231
|
|
|
$swaggerOperation = $operation['swagger_context'] ?? []; |
232
|
|
|
$shortName = $resourceMetadata->getShortName(); |
233
|
|
|
$swaggerOperation[$methodSwagger] = []; |
234
|
|
|
$swaggerOperation[$methodSwagger]['tags'] = [$shortName]; |
235
|
|
|
|
236
|
|
|
switch ($method) { |
237
|
|
|
case 'GET': |
|
|
|
|
238
|
|
|
$swaggerOperation[$methodSwagger]['produces'] = $this->mimeTypes; |
239
|
|
|
|
240
|
|
|
if ($collection) { |
241
|
|
View Code Duplication |
if (!isset($swaggerOperation[$methodSwagger]['title'])) { |
|
|
|
|
242
|
|
|
$swaggerOperation[$methodSwagger]['summary'] = sprintf('Retrieves the collection of %s resources.', $shortName); |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
$swaggerOperation[$methodSwagger]['responses'] = [ |
246
|
|
|
'200' => [ |
247
|
|
|
'description' => 'Successful operation', |
248
|
|
|
'schema' => [ |
249
|
|
|
'type' => 'array', |
250
|
|
|
'items' => ['$ref' => sprintf('#/definitions/%s', $shortName)], |
251
|
|
|
], |
252
|
|
|
], |
253
|
|
|
]; |
254
|
|
|
break; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
View Code Duplication |
if (!isset($swaggerOperation[$methodSwagger]['title'])) { |
|
|
|
|
258
|
|
|
$swaggerOperation[$methodSwagger]['summary'] = sprintf('Retrieves %s resource.', $shortName); |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
$swaggerOperation[$methodSwagger]['parameters'][] = [ |
262
|
|
|
'name' => 'id', |
263
|
|
|
'in' => 'path', |
264
|
|
|
'required' => true, |
265
|
|
|
'type' => 'integer', |
266
|
|
|
]; |
267
|
|
|
|
268
|
|
|
$swaggerOperation[$methodSwagger]['responses'] = [ |
269
|
|
|
'200' => [ |
270
|
|
|
'description' => 'Successful operation', |
271
|
|
|
'schema' => ['$ref' => sprintf('#/definitions/%s', $shortName)], |
272
|
|
|
], |
273
|
|
|
'404' => ['description' => 'Resource not found'], |
274
|
|
|
]; |
275
|
|
|
break; |
276
|
|
|
|
277
|
|
|
case 'POST': |
|
|
|
|
278
|
|
|
$swaggerOperation[$methodSwagger]['consumes'] = $swaggerOperation[$methodSwagger]['produces'] = $this->mimeTypes; |
279
|
|
|
|
280
|
|
View Code Duplication |
if (!isset($swaggerOperation[$methodSwagger]['title'])) { |
|
|
|
|
281
|
|
|
$swaggerOperation[$methodSwagger]['summary'] = sprintf('Creates a %s resource.', $shortName); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
if ($this->resourceClassResolver->isResourceClass($resourceClass)) { |
285
|
|
|
$swaggerOperation[$methodSwagger]['parameters'][] = [ |
286
|
|
|
'in' => 'body', |
287
|
|
|
'name' => 'body', |
288
|
|
|
'description' => sprintf('%s resource to be added', $shortName), |
289
|
|
|
'schema' => [ |
290
|
|
|
'$ref' => sprintf('#/definitions/%s', $shortName), |
291
|
|
|
], |
292
|
|
|
]; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
$swaggerOperation[$methodSwagger]['responses'] = [ |
296
|
|
|
'201' => [ |
297
|
|
|
'description' => 'Successful operation', |
298
|
|
|
'schema' => ['$ref' => sprintf('#/definitions/%s', $shortName)], |
299
|
|
|
], |
300
|
|
|
'400' => ['description' => 'Invalid input'], |
301
|
|
|
'404' => ['description' => 'Resource not found'], |
302
|
|
|
]; |
303
|
|
|
break; |
304
|
|
|
|
305
|
|
|
case 'PUT': |
|
|
|
|
306
|
|
|
$swaggerOperation[$methodSwagger]['consumes'] = $swaggerOperation[$methodSwagger]['produces'] = $this->mimeTypes; |
307
|
|
|
|
308
|
|
View Code Duplication |
if (!isset($swaggerOperation[$methodSwagger]['title'])) { |
|
|
|
|
309
|
|
|
$swaggerOperation[$methodSwagger]['summary'] = sprintf('Replaces the %s resource.', $shortName); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
if ($this->resourceClassResolver->isResourceClass($resourceClass)) { |
313
|
|
|
$swaggerOperation[$methodSwagger]['parameters'] = [ |
314
|
|
|
[ |
315
|
|
|
'name' => 'id', |
316
|
|
|
'in' => 'path', |
317
|
|
|
'required' => true, |
318
|
|
|
'type' => 'integer', |
319
|
|
|
], |
320
|
|
|
[ |
321
|
|
|
'in' => 'body', |
322
|
|
|
'name' => 'body', |
323
|
|
|
'description' => sprintf('%s resource to be added', $shortName), |
324
|
|
|
'schema' => [ |
325
|
|
|
'$ref' => sprintf('#/definitions/%s', $shortName), |
326
|
|
|
], |
327
|
|
|
], |
328
|
|
|
]; |
329
|
|
|
} |
330
|
|
|
|
331
|
|
|
$swaggerOperation[$methodSwagger]['responses'] = [ |
332
|
|
|
'200' => [ |
333
|
|
|
'description' => 'Successful operation', |
334
|
|
|
'schema' => ['$ref' => sprintf('#/definitions/%s', $shortName)], |
335
|
|
|
], |
336
|
|
|
'400' => ['description' => 'Invalid input'], |
337
|
|
|
'404' => ['description' => 'Resource not found'], |
338
|
|
|
]; |
339
|
|
|
break; |
340
|
|
|
|
341
|
|
|
case 'DELETE': |
342
|
|
|
$swaggerOperation[$methodSwagger]['responses'] = [ |
343
|
|
|
'204' => ['description' => 'Deleted'], |
344
|
|
|
'404' => ['description' => 'Resource not found'], |
345
|
|
|
]; |
346
|
|
|
|
347
|
|
|
$swaggerOperation[$methodSwagger]['parameters'] = [[ |
348
|
|
|
'name' => 'id', |
349
|
|
|
'in' => 'path', |
350
|
|
|
'required' => true, |
351
|
|
|
'type' => 'integer', |
352
|
|
|
]]; |
353
|
|
|
break; |
354
|
|
|
} |
355
|
|
|
ksort($swaggerOperation); |
356
|
|
|
|
357
|
|
|
return $swaggerOperation; |
358
|
|
|
} |
359
|
|
|
|
360
|
|
|
/** |
361
|
|
|
* Gets the range of the property. |
362
|
|
|
* |
363
|
|
|
* @param PropertyMetadata $propertyMetadata |
364
|
|
|
* |
365
|
|
|
* @return array|null |
366
|
|
|
*/ |
367
|
|
|
private function getRange(PropertyMetadata $propertyMetadata) : array |
368
|
|
|
{ |
369
|
|
|
$type = $propertyMetadata->getType(); |
370
|
|
|
if (!$type) { |
371
|
|
|
return []; |
372
|
|
|
} |
373
|
|
|
|
374
|
|
|
if ($type->isCollection() && $collectionType = $type->getCollectionValueType()) { |
375
|
|
|
$type = $collectionType; |
376
|
|
|
} |
377
|
|
|
|
378
|
|
|
switch ($type->getBuiltinType()) { |
379
|
|
|
case Type::BUILTIN_TYPE_STRING: |
380
|
|
|
return ['complex' => false, 'value' => 'string']; |
381
|
|
|
|
382
|
|
|
case Type::BUILTIN_TYPE_INT: |
383
|
|
|
return ['complex' => false, 'value' => 'integer']; |
384
|
|
|
|
385
|
|
|
case Type::BUILTIN_TYPE_FLOAT: |
386
|
|
|
return ['complex' => false, 'value' => 'number']; |
387
|
|
|
|
388
|
|
|
case Type::BUILTIN_TYPE_BOOL: |
389
|
|
|
return ['complex' => false, 'value' => 'boolean']; |
390
|
|
|
|
391
|
|
|
case Type::BUILTIN_TYPE_OBJECT: |
392
|
|
|
$className = $type->getClassName(); |
393
|
|
|
if (null === $className) { |
394
|
|
|
return []; |
395
|
|
|
} |
396
|
|
|
|
397
|
|
|
if (is_subclass_of($className, \DateTimeInterface::class)) { |
|
|
|
|
398
|
|
|
return ['complex' => false, 'value' => 'string', 'example' => '1988-01-21T00:00:00+00:00']; |
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
if (!$this->resourceClassResolver->isResourceClass($className)) { |
402
|
|
|
return []; |
403
|
|
|
} |
404
|
|
|
|
405
|
|
|
if ($propertyMetadata->isReadableLink()) { |
406
|
|
|
return ['complex' => true, 'value' => sprintf('#/definitions/%s', $this->resourceMetadataFactory->create($className)->getShortName())]; |
407
|
|
|
} |
408
|
|
|
|
409
|
|
|
return ['complex' => false, 'value' => 'string', 'example' => '/my/iri']; |
410
|
|
|
|
411
|
|
|
default: |
412
|
|
|
return ['complex' => false, 'value' => 'null']; |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.