1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace ApiPlatform\Core\Swagger; |
6
|
|
|
|
7
|
|
|
use ApiPlatform\Core\Exception\ResourceClassNotFoundException; |
8
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface; |
9
|
|
|
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface; |
10
|
|
|
use ApiPlatform\Core\Metadata\Property\PropertyMetadata; |
11
|
|
|
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
12
|
|
|
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
13
|
|
|
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer; |
14
|
|
|
use Symfony\Component\Serializer\NameConverter\NameConverterInterface; |
15
|
|
|
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Builds an OpenAPI Schema, an extended subset of the JSON Schema Specification. |
19
|
|
|
* |
20
|
|
|
* @see https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.2.md#schema-object |
21
|
|
|
* @see https://tools.ietf.org/html/draft-wright-json-schema-01 |
22
|
|
|
* |
23
|
|
|
* @author Kévin Dunglas <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
final class SchemaBuilder |
26
|
|
|
{ |
27
|
|
|
use TypeResolverTrait; |
28
|
|
|
|
29
|
|
|
private $resourceMetadataFactory; |
30
|
|
|
private $propertyNameCollectionFactory; |
31
|
|
|
private $propertyMetadataFactory; |
32
|
|
|
private $nameConverter; |
33
|
|
|
|
34
|
|
|
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, PropertyNameCollectionFactoryInterface $propertyNameCollectionFactory, PropertyMetadataFactoryInterface $propertyMetadataFactory, NameConverterInterface $nameConverter = null) |
35
|
|
|
{ |
36
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
37
|
|
|
$this->propertyNameCollectionFactory = $propertyNameCollectionFactory; |
38
|
|
|
$this->propertyMetadataFactory = $propertyMetadataFactory; |
39
|
|
|
$this->nameConverter = $nameConverter; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function buildSchemaName(string $resourceClass, bool $output = true, ?string $operationType = null, ?string $operationName = null, ?string $format = null, ?array $serializerContext = null, string $version = DocumentationNormalizer::OPENAPI_VERSION): ?string |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
if (null === $metadata = $this->getMetadata($resourceClass, $output, $operationType, $operationName, $serializerContext)) { |
45
|
|
|
return null; |
46
|
|
|
} |
47
|
|
|
[$resourceMetadata, $serializerContext, $inputOrOutputClass] = $metadata; |
48
|
|
|
|
49
|
|
|
$prefix = $resourceMetadata->getShortName(); |
50
|
|
|
if (null !== $inputOrOutputClass && $resourceClass !== $inputOrOutputClass) { |
51
|
|
|
$prefix .= ':'.md5($inputOrOutputClass); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/*if (null !== $format) { |
|
|
|
|
55
|
|
|
$prefix .= ':'.$format; |
56
|
|
|
}*/ |
57
|
|
|
|
58
|
|
|
if (isset($serializerContext[DocumentationNormalizer::SWAGGER_DEFINITION_NAME])) { |
59
|
|
|
$name = sprintf('%s-%s', $prefix, $serializerContext[DocumentationNormalizer::SWAGGER_DEFINITION_NAME]); |
60
|
|
|
} else { |
61
|
|
|
$groups = (array) ($serializerContext[AbstractNormalizer::GROUPS] ?? []); |
62
|
|
|
$name = $groups ? sprintf('%s-%s', $prefix, implode('_', $groups)) : $prefix; |
|
|
|
|
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
return $name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @throws ResourceClassNotFoundException |
70
|
|
|
*/ |
71
|
|
|
public function buildSchema(string $resourceClass, bool $output = true, ?string $operationType = null, ?string $operationName = null, ?string $format = null, ?array $serializerContext = null, string $version = DocumentationNormalizer::OPENAPI_VERSION): ?\ArrayObject |
72
|
|
|
{ |
73
|
|
|
if (null === $metadata = $this->getMetadata($resourceClass, $output, $operationType, $operationName, $serializerContext)) { |
74
|
|
|
return null; |
75
|
|
|
} |
76
|
|
|
[$resourceMetadata, $serializerContext, $inputOrOutputClass] = $metadata; |
77
|
|
|
|
78
|
|
|
$schema = ['type' => 'object']; |
79
|
|
|
if (null !== $description = $resourceMetadata->getDescription()) { |
80
|
|
|
$schema['description'] = $description; |
81
|
|
|
} |
82
|
|
|
if (null !== $iri = $resourceMetadata->getIri()) { |
83
|
|
|
$schema['externalDocs'] = ['url' => $iri]; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$options = isset($serializerContext[AbstractNormalizer::GROUPS]) ? ['serializer_groups' => $serializerContext[AbstractNormalizer::GROUPS]] : []; |
87
|
|
|
foreach ($this->propertyNameCollectionFactory->create($inputOrOutputClass, $options) as $propertyName) { |
88
|
|
|
$propertyMetadata = $this->propertyMetadataFactory->create($inputOrOutputClass, $propertyName); |
89
|
|
|
if (!$propertyMetadata->isReadable() && !$propertyMetadata->isWritable()) { |
|
|
|
|
90
|
|
|
continue; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
$normalizedPropertyName = $this->nameConverter ? $this->nameConverter->normalize($propertyName, $inputOrOutputClass, $format, $serializerContext) : $propertyName; |
|
|
|
|
94
|
|
|
if ($propertyMetadata->isRequired()) { |
95
|
|
|
$schema['required'][] = $normalizedPropertyName; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$schema['properties'][$normalizedPropertyName] = $this->buildPropertySchema($propertyMetadata, $serializerContext, $version); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return new \ArrayObject($schema); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
private function getMetadata(string $resourceClass, bool $output, ?string $operationType, ?string $operationName, ?array $serializerContext): ?array |
105
|
|
|
{ |
106
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($resourceClass); |
107
|
|
|
$attribute = $output ? 'output' : 'input'; |
108
|
|
|
if (null === $operationType || null === $operationName) { |
109
|
|
|
$inputOrOutput = $resourceMetadata->getAttribute($attribute, ['class' => $resourceClass]); |
110
|
|
|
} else { |
111
|
|
|
$inputOrOutput = $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, ['class' => $resourceClass], true); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
if (null === ($inputOrOutput['class'] ?? null)) { |
115
|
|
|
// input or output disabled |
116
|
|
|
return null; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
return [ |
120
|
|
|
$resourceMetadata, |
121
|
|
|
$serializerContext ?? $this->getSerializerContext($resourceMetadata, $output, $operationType, $operationName), |
122
|
|
|
$inputOrOutput['class'] |
123
|
|
|
]; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
private function buildPropertySchema(PropertyMetadata $propertyMetadata, array $serializerContext, string $version): \ArrayObject |
127
|
|
|
{ |
128
|
|
|
$propertySchema = $propertyMetadata->getAttributes()[$version === DocumentationNormalizer::OPENAPI_VERSION ? 'openapi_context' : 'swagger_context'] ?? []; |
129
|
|
|
|
130
|
|
|
if (false === $propertyMetadata->isWritable() && !$propertyMetadata->isInitializable()) { |
|
|
|
|
131
|
|
|
$propertySchema['readOnly'] = true; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if (null !== $description = $propertyMetadata->getDescription()) { |
135
|
|
|
$propertySchema['description'] = $description; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
if (null !== $iri = $propertyMetadata->getIri()) { |
139
|
|
|
$schema['externalDocs'] = ['url' => $iri]; |
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
if (null === $type = $propertyMetadata->getType()) { |
143
|
|
|
return $propertySchema; |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
$isCollection = $type->isCollection(); |
147
|
|
|
if (null === $valueType = $isCollection ? $type->getCollectionValueType() : $type) { |
148
|
|
|
$builtinType = 'string'; |
149
|
|
|
$className = null; |
150
|
|
|
} else { |
151
|
|
|
$builtinType = $valueType->getBuiltinType(); |
152
|
|
|
$className = $valueType->getClassName(); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
$valueSchema = $this->getType($builtinType, $isCollection, $className, $propertyMetadata->isReadableLink(), $serializerContext, $version); |
156
|
|
|
|
157
|
|
|
return new \ArrayObject($propertySchema + $valueSchema); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
private function getSerializerContext(ResourceMetadata $resourceMetadata, bool $output, ?string $operationType, ?string $operationName): array |
161
|
|
|
{ |
162
|
|
|
$attribute = $output ? 'normalization_context' : 'denormalization_context'; |
163
|
|
|
|
164
|
|
|
if (null === $operationType || null === $operationName) { |
165
|
|
|
return $resourceMetadata->getAttribute($attribute, []); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $resourceMetadata->getTypedOperationAttribute($operationType, $operationName, $attribute, [], true); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.