|
21 | 21 | use ApiPlatform\Metadata\GraphQl\QueryCollection;
|
22 | 22 | use ApiPlatform\Metadata\GraphQl\Subscription;
|
23 | 23 | use ApiPlatform\Metadata\Post;
|
| 24 | +use ApiPlatform\OpenApi\Model\ExternalDocumentation; |
| 25 | +use ApiPlatform\OpenApi\Model\Operation as OpenApiOperation; |
| 26 | +use ApiPlatform\OpenApi\Model\Parameter; |
| 27 | +use ApiPlatform\OpenApi\Model\RequestBody; |
24 | 28 | use Symfony\Component\Config\Util\XmlUtils;
|
25 | 29 |
|
26 | 30 | /**
|
@@ -87,7 +91,8 @@ private function buildExtendedBase(\SimpleXMLElement $resource): array
|
87 | 91 | 'schemes' => $this->buildArrayValue($resource, 'scheme'),
|
88 | 92 | 'cacheHeaders' => $this->buildCacheHeaders($resource),
|
89 | 93 | 'hydraContext' => isset($resource->hydraContext->values) ? $this->buildValues($resource->hydraContext->values) : null,
|
90 |
| - 'openapiContext' => isset($resource->openapiContext->values) ? $this->buildValues($resource->openapiContext->values) : null, |
| 94 | + 'openapiContext' => isset($resource->openapiContext->values) ? $this->buildValues($resource->openapiContext->values) : null, // TODO Remove in 4.0 |
| 95 | + 'openapi' => $this->buildOpenapi($resource), |
91 | 96 | 'paginationViaCursor' => $this->buildPaginationViaCursor($resource),
|
92 | 97 | 'exceptionToStatus' => $this->buildExceptionToStatus($resource),
|
93 | 98 | 'queryParameterValidationEnabled' => $this->phpize($resource, 'queryParameterValidationEnabled', 'bool'),
|
@@ -156,6 +161,91 @@ private function buildFormats(\SimpleXMLElement $resource, string $key): ?array
|
156 | 161 | return $data;
|
157 | 162 | }
|
158 | 163 |
|
| 164 | + private function buildOpenapi(\SimpleXMLElement $resource): bool|OpenApiOperation|null |
| 165 | + { |
| 166 | + if (!isset($resource->openapi) && !isset($resource['openapi'])) { |
| 167 | + return null; |
| 168 | + } |
| 169 | + |
| 170 | + if (isset($resource['openapi']) && (\is_bool($resource['openapi']) || \in_array((string) $resource['openapi'], ['1', '0', 'true', 'false'], true))) { |
| 171 | + return $this->phpize($resource, 'openapi', 'bool'); |
| 172 | + } |
| 173 | + |
| 174 | + $openapi = $resource->openapi; |
| 175 | + $data = []; |
| 176 | + $attributes = $openapi->attributes(); |
| 177 | + foreach ($attributes as $attribute) { |
| 178 | + $data[$attribute->getName()] = $this->phpize($attributes, 'deprecated', 'deprecated' === $attribute->getName() ? 'bool' : 'string'); |
| 179 | + } |
| 180 | + |
| 181 | + $data['tags'] = $this->buildArrayValue($resource, 'tag'); |
| 182 | + |
| 183 | + if (isset($openapi->responses->response)) { |
| 184 | + foreach ($openapi->responses->response as $response) { |
| 185 | + $data['responses'][(string) $response->attributes()->status] = [ |
| 186 | + 'description' => $this->phpize($response, 'description', 'string'), |
| 187 | + 'content' => isset($response->content->values) ? $this->buildValues($response->content->values) : null, |
| 188 | + 'headers' => isset($response->headers->values) ? $this->buildValues($response->headers->values) : null, |
| 189 | + 'links' => isset($response->links->values) ? $this->buildValues($response->links->values) : null, |
| 190 | + ]; |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + $data['externalDocs'] = isset($openapi->externalDocs) ? new ExternalDocumentation( |
| 195 | + description: $this->phpize($resource, 'description', 'string'), |
| 196 | + url: $this->phpize($resource, 'url', 'string'), |
| 197 | + ) : null; |
| 198 | + |
| 199 | + if (isset($openapi->parameters->parameter)) { |
| 200 | + foreach ($openapi->parameters->parameter as $parameter) { |
| 201 | + $data['parameters'][(string) $parameter->attributes()->name] = new Parameter( |
| 202 | + name: $this->phpize($parameter, 'name', 'string'), |
| 203 | + in: $this->phpize($parameter, 'in', 'string'), |
| 204 | + description: $this->phpize($parameter, 'description', 'string'), |
| 205 | + required: $this->phpize($parameter, 'required', 'bool'), |
| 206 | + deprecated: $this->phpize($parameter, 'deprecated', 'bool'), |
| 207 | + allowEmptyValue: $this->phpize($parameter, 'allowEmptyValue', 'bool'), |
| 208 | + schema: isset($parameter->schema->values) ? $this->buildValues($parameter->schema->values) : null, |
| 209 | + style: $this->phpize($parameter, 'style', 'string'), |
| 210 | + explode: $this->phpize($parameter, 'explode', 'bool'), |
| 211 | + allowReserved: $this->phpize($parameter, 'allowReserved', 'bool'), |
| 212 | + example: $this->phpize($parameter, 'example', 'string'), |
| 213 | + examples: isset($parameter->examples->values) ? new \ArrayObject($this->buildValues($parameter->examples->values)) : null, |
| 214 | + content: isset($parameter->content->values) ? new \ArrayObject($this->buildValues($parameter->content->values)) : null, |
| 215 | + ); |
| 216 | + } |
| 217 | + } |
| 218 | + $data['requestBody'] = isset($openapi->requestBody) ? new RequestBody( |
| 219 | + description: $this->phpize($openapi->requestBody, 'description', 'string'), |
| 220 | + content: isset($openapi->requestBody->content->values) ? new \ArrayObject($this->buildValues($openapi->requestBody->values)) : null, |
| 221 | + required: $this->phpize($openapi->requestBody, 'required', 'bool'), |
| 222 | + ) : null; |
| 223 | + |
| 224 | + $data['callbacks'] = isset($openapi->callbacks->values) ? new \ArrayObject($this->buildValues($openapi->callbacks->values)) : null; |
| 225 | + |
| 226 | + $data['security'] = isset($openapi->security->values) ? $this->buildValues($openapi->security->values) : null; |
| 227 | + |
| 228 | + if (isset($openapi->servers->server)) { |
| 229 | + foreach ($openapi->servers->server as $server) { |
| 230 | + $data['servers'][] = [ |
| 231 | + 'description' => $this->phpize($server, 'description', 'string'), |
| 232 | + 'url' => $this->phpize($server, 'url', 'string'), |
| 233 | + 'variables' => isset($server->variables->values) ? $this->buildValues($server->variables->values) : null, |
| 234 | + ]; |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + $data['extensionProperties'] = isset($openapi->extensionProperties->values) ? $this->buildValues($openapi->extensionProperties->values) : null; |
| 239 | + |
| 240 | + foreach ($data as $key => $value) { |
| 241 | + if (null === $value) { |
| 242 | + unset($data[$key]); |
| 243 | + } |
| 244 | + } |
| 245 | + |
| 246 | + return new OpenApiOperation(...$data); |
| 247 | + } |
| 248 | + |
159 | 249 | private function buildUriVariables(\SimpleXMLElement $resource): ?array
|
160 | 250 | {
|
161 | 251 | if (!isset($resource->uriVariables->uriVariable)) {
|
@@ -300,7 +390,6 @@ private function buildOperations(\SimpleXMLElement $resource, array $root): ?arr
|
300 | 390 | }
|
301 | 391 |
|
302 | 392 | $data[] = array_merge($datum, [
|
303 |
| - 'openapi' => $this->phpize($operation, 'openapi', 'bool'), |
304 | 393 | 'collection' => $this->phpize($operation, 'collection', 'bool'),
|
305 | 394 | 'class' => (string) $operation['class'],
|
306 | 395 | 'method' => $this->phpize($operation, 'method', 'string'),
|
|
0 commit comments