• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 3712739783

pending completion
3712739783

Pull #5254

github

GitHub
Merge 9dfa88fa6 into ac711530f
Pull Request #5254: [OpenApi] Add ApiResource::openapi and deprecate openapiContext

199 of 199 new or added lines in 6 files covered. (100.0%)

7494 of 12363 relevant lines covered (60.62%)

67.55 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.52
/src/Metadata/Extractor/XmlResourceExtractor.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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
declare(strict_types=1);
13

14
namespace ApiPlatform\Metadata\Extractor;
15

16
use ApiPlatform\Exception\InvalidArgumentException;
17
use ApiPlatform\Metadata\GetCollection;
18
use ApiPlatform\Metadata\GraphQl\DeleteMutation;
19
use ApiPlatform\Metadata\GraphQl\Mutation;
20
use ApiPlatform\Metadata\GraphQl\Query;
21
use ApiPlatform\Metadata\GraphQl\QueryCollection;
22
use ApiPlatform\Metadata\GraphQl\Subscription;
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;
28
use Symfony\Component\Config\Util\XmlUtils;
29

30
/**
31
 * Extracts an array of metadata from a list of XML files.
32
 *
33
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
34
 */
35
final class XmlResourceExtractor extends AbstractResourceExtractor
36
{
37
    use ResourceExtractorTrait;
38

39
    public const SCHEMA = __DIR__.'/schema/resources.xsd';
40

41
    /**
42
     * {@inheritdoc}
43
     */
44
    protected function extractPath(string $path): void
45
    {
46
        try {
47
            /** @var \SimpleXMLElement $xml */
48
            $xml = simplexml_import_dom(XmlUtils::loadFile($path, self::SCHEMA));
22✔
49
        } catch (\InvalidArgumentException $e) {
22✔
50
            // Ensure it's not a resource
51
            try {
52
                simplexml_import_dom(XmlUtils::loadFile($path, XmlPropertyExtractor::SCHEMA));
22✔
53
            } catch (\InvalidArgumentException) {
×
54
                throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
×
55
            }
56

57
            // It's a property: ignore error
58
            return;
22✔
59
        }
60

61
        foreach ($xml->resource as $resource) {
22✔
62
            $base = $this->buildExtendedBase($resource);
22✔
63
            $this->resources[$this->resolve((string) $resource['class'])][] = array_merge($base, [
22✔
64
                'class' => $this->phpize($resource, 'class', 'string'),
22✔
65
                'operations' => $this->buildOperations($resource, $base),
22✔
66
                'graphQlOperations' => $this->buildGraphQlOperations($resource, $base),
22✔
67
            ]);
22✔
68
        }
69
    }
70

71
    private function buildExtendedBase(\SimpleXMLElement $resource): array
72
    {
73
        return array_merge($this->buildBase($resource), [
22✔
74
            'uriTemplate' => $this->phpize($resource, 'uriTemplate', 'string'),
22✔
75
            'routePrefix' => $this->phpize($resource, 'routePrefix', 'string'),
22✔
76
            'stateless' => $this->phpize($resource, 'stateless', 'bool'),
22✔
77
            'sunset' => $this->phpize($resource, 'sunset', 'string'),
22✔
78
            'acceptPatch' => $this->phpize($resource, 'acceptPatch', 'string'),
22✔
79
            'status' => $this->phpize($resource, 'status', 'integer'),
22✔
80
            'host' => $this->phpize($resource, 'host', 'string'),
22✔
81
            'condition' => $this->phpize($resource, 'condition', 'string'),
22✔
82
            'controller' => $this->phpize($resource, 'controller', 'string'),
22✔
83
            'types' => $this->buildArrayValue($resource, 'type'),
22✔
84
            'formats' => $this->buildFormats($resource, 'formats'),
22✔
85
            'inputFormats' => $this->buildFormats($resource, 'inputFormats'),
22✔
86
            'outputFormats' => $this->buildFormats($resource, 'outputFormats'),
22✔
87
            'uriVariables' => $this->buildUriVariables($resource),
22✔
88
            'defaults' => isset($resource->defaults->values) ? $this->buildValues($resource->defaults->values) : null,
22✔
89
            'requirements' => $this->buildRequirements($resource),
22✔
90
            'options' => isset($resource->options->values) ? $this->buildValues($resource->options->values) : null,
22✔
91
            'schemes' => $this->buildArrayValue($resource, 'scheme'),
22✔
92
            'cacheHeaders' => $this->buildCacheHeaders($resource),
22✔
93
            'hydraContext' => isset($resource->hydraContext->values) ? $this->buildValues($resource->hydraContext->values) : null,
22✔
94
            'openapiContext' => isset($resource->openapiContext->values) ? $this->buildValues($resource->openapiContext->values) : null, // TODO Remove in 4.0
22✔
95
            'openapi' => $this->buildOpenapi($resource),
22✔
96
            'paginationViaCursor' => $this->buildPaginationViaCursor($resource),
22✔
97
            'exceptionToStatus' => $this->buildExceptionToStatus($resource),
22✔
98
            'queryParameterValidationEnabled' => $this->phpize($resource, 'queryParameterValidationEnabled', 'bool'),
22✔
99
        ]);
22✔
100
    }
101

102
    private function buildBase(\SimpleXMLElement $resource): array
103
    {
104
        return [
22✔
105
            'shortName' => $this->phpize($resource, 'shortName', 'string'),
22✔
106
            'description' => $this->phpize($resource, 'description', 'string'),
22✔
107
            'urlGenerationStrategy' => $this->phpize($resource, 'urlGenerationStrategy', 'integer'),
22✔
108
            'deprecationReason' => $this->phpize($resource, 'deprecationReason', 'string'),
22✔
109
            'elasticsearch' => $this->phpize($resource, 'elasticsearch', 'bool'),
22✔
110
            'messenger' => $this->phpize($resource, 'messenger', 'bool|string'),
22✔
111
            'mercure' => $this->buildMercure($resource),
22✔
112
            'input' => $this->phpize($resource, 'input', 'bool|string'),
22✔
113
            'output' => $this->phpize($resource, 'output', 'bool|string'),
22✔
114
            'fetchPartial' => $this->phpize($resource, 'fetchPartial', 'bool'),
22✔
115
            'forceEager' => $this->phpize($resource, 'forceEager', 'bool'),
22✔
116
            'paginationClientEnabled' => $this->phpize($resource, 'paginationClientEnabled', 'bool'),
22✔
117
            'paginationClientItemsPerPage' => $this->phpize($resource, 'paginationClientItemsPerPage', 'bool'),
22✔
118
            'paginationClientPartial' => $this->phpize($resource, 'paginationClientPartial', 'bool'),
22✔
119
            'paginationEnabled' => $this->phpize($resource, 'paginationEnabled', 'bool'),
22✔
120
            'paginationFetchJoinCollection' => $this->phpize($resource, 'paginationFetchJoinCollection', 'bool'),
22✔
121
            'paginationUseOutputWalkers' => $this->phpize($resource, 'paginationUseOutputWalkers', 'bool'),
22✔
122
            'paginationItemsPerPage' => $this->phpize($resource, 'paginationItemsPerPage', 'integer'),
22✔
123
            'paginationMaximumItemsPerPage' => $this->phpize($resource, 'paginationMaximumItemsPerPage', 'integer'),
22✔
124
            'paginationPartial' => $this->phpize($resource, 'paginationPartial', 'bool'),
22✔
125
            'paginationType' => $this->phpize($resource, 'paginationType', 'string'),
22✔
126
            'processor' => $this->phpize($resource, 'processor', 'string'),
22✔
127
            'provider' => $this->phpize($resource, 'provider', 'string'),
22✔
128
            'security' => $this->phpize($resource, 'security', 'string'),
22✔
129
            'securityMessage' => $this->phpize($resource, 'securityMessage', 'string'),
22✔
130
            'securityPostDenormalize' => $this->phpize($resource, 'securityPostDenormalize', 'string'),
22✔
131
            'securityPostDenormalizeMessage' => $this->phpize($resource, 'securityPostDenormalizeMessage', 'string'),
22✔
132
            'securityPostValidation' => $this->phpize($resource, 'securityPostValidation', 'string'),
22✔
133
            'securityPostValidationMessage' => $this->phpize($resource, 'securityPostValidationMessage', 'string'),
22✔
134
            'normalizationContext' => isset($resource->normalizationContext->values) ? $this->buildValues($resource->normalizationContext->values) : null,
22✔
135
            'denormalizationContext' => isset($resource->denormalizationContext->values) ? $this->buildValues($resource->denormalizationContext->values) : null,
22✔
136
            'validationContext' => isset($resource->validationContext->values) ? $this->buildValues($resource->validationContext->values) : null,
22✔
137
            'filters' => $this->buildArrayValue($resource, 'filter'),
22✔
138
            'order' => isset($resource->order->values) ? $this->buildValues($resource->order->values) : null,
22✔
139
            'extraProperties' => $this->buildExtraProperties($resource, 'extraProperties'),
22✔
140
            'read' => $this->phpize($resource, 'read', 'bool'),
22✔
141
            'write' => $this->phpize($resource, 'write', 'bool'),
22✔
142
        ];
22✔
143
    }
144

145
    private function buildFormats(\SimpleXMLElement $resource, string $key): ?array
146
    {
147
        if (!isset($resource->{$key}->format)) {
22✔
148
            return null;
22✔
149
        }
150

151
        $data = [];
×
152
        foreach ($resource->{$key}->format as $format) {
×
153
            if (isset($format['name'])) {
×
154
                $data[(string) $format['name']] = (string) $format;
×
155
                continue;
×
156
            }
157

158
            $data[] = (string) $format;
×
159
        }
160

161
        return $data;
×
162
    }
163

164
    private function buildOpenapi(\SimpleXMLElement $resource): bool|OpenApiOperation|null
165
    {
166
        if (!isset($resource->openapi) && !isset($resource['openapi'])) {
22✔
167
            return null;
22✔
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

249
    private function buildUriVariables(\SimpleXMLElement $resource): ?array
250
    {
251
        if (!isset($resource->uriVariables->uriVariable)) {
22✔
252
            return null;
22✔
253
        }
254

255
        $uriVariables = [];
22✔
256
        foreach ($resource->uriVariables->uriVariable as $data) {
22✔
257
            $parameterName = (string) $data['parameterName'];
22✔
258
            if (1 === (null === $data->attributes() ? 0 : \count($data->attributes()))) {
22✔
259
                $uriVariables[$parameterName] = $parameterName;
22✔
260
                continue;
22✔
261
            }
262

263
            if ($fromProperty = $this->phpize($data, 'fromProperty', 'string')) {
22✔
264
                $uriVariables[$parameterName]['from_property'] = $fromProperty;
×
265
            }
266
            if ($toProperty = $this->phpize($data, 'toProperty', 'string')) {
22✔
267
                $uriVariables[$parameterName]['to_property'] = $toProperty;
22✔
268
            }
269
            if ($fromClass = $this->phpize($data, 'fromClass', 'string')) {
22✔
270
                $uriVariables[$parameterName]['from_class'] = $fromClass;
22✔
271
            }
272
            if ($toClass = $this->phpize($data, 'toClass', 'string')) {
22✔
273
                $uriVariables[$parameterName]['to_class'] = $toClass;
×
274
            }
275
            if (isset($data->identifiers->values)) {
22✔
276
                $uriVariables[$parameterName]['identifiers'] = $this->buildValues($data->identifiers->values);
×
277
            }
278
            if (null !== ($compositeIdentifier = $this->phpize($data, 'compositeIdentifier', 'bool'))) {
22✔
279
                $uriVariables[$parameterName]['composite_identifier'] = $compositeIdentifier;
×
280
            }
281
        }
282

283
        return $uriVariables;
22✔
284
    }
285

286
    private function buildCacheHeaders(\SimpleXMLElement $resource): ?array
287
    {
288
        if (!isset($resource->cacheHeaders->cacheHeader)) {
22✔
289
            return null;
22✔
290
        }
291

292
        $data = [];
×
293
        foreach ($resource->cacheHeaders->cacheHeader as $cacheHeader) {
×
294
            if (isset($cacheHeader->values->value)) {
×
295
                $data[(string) $cacheHeader['name']] = $this->buildValues($cacheHeader->values);
×
296
                continue;
×
297
            }
298

299
            $data[(string) $cacheHeader['name']] = (string) $cacheHeader;
×
300
        }
301

302
        return $data;
×
303
    }
304

305
    private function buildRequirements(\SimpleXMLElement $resource): ?array
306
    {
307
        if (!isset($resource->requirements->requirement)) {
22✔
308
            return null;
22✔
309
        }
310

311
        $data = [];
×
312
        foreach ($resource->requirements->requirement as $requirement) {
×
313
            $data[(string) $requirement->attributes()->property] = (string) $requirement;
×
314
        }
315

316
        return $data;
×
317
    }
318

319
    private function buildMercure(\SimpleXMLElement $resource): array|bool|null
320
    {
321
        if (!isset($resource->mercure)) {
22✔
322
            return null;
22✔
323
        }
324

325
        if (null !== $resource->mercure->attributes()->private) {
×
326
            return ['private' => $this->phpize($resource->mercure->attributes(), 'private', 'bool')];
×
327
        }
328

329
        return true;
×
330
    }
331

332
    private function buildPaginationViaCursor(\SimpleXMLElement $resource): ?array
333
    {
334
        if (!isset($resource->paginationViaCursor->paginationField)) {
22✔
335
            return null;
22✔
336
        }
337

338
        $data = [];
×
339
        foreach ($resource->paginationViaCursor->paginationField as $paginationField) {
×
340
            $data[(string) $paginationField['field']] = (string) $paginationField['direction'];
×
341
        }
342

343
        return $data;
×
344
    }
345

346
    private function buildExceptionToStatus(\SimpleXMLElement $resource): ?array
347
    {
348
        if (!isset($resource->exceptionToStatus->exception)) {
22✔
349
            return null;
22✔
350
        }
351

352
        $data = [];
×
353
        foreach ($resource->exceptionToStatus->exception as $exception) {
×
354
            $data[(string) $exception['class']] = (int) $exception['statusCode'];
×
355
        }
356

357
        return $data;
×
358
    }
359

360
    private function buildExtraProperties(\SimpleXMLElement $resource, string $key = null): ?array
361
    {
362
        if (null !== $key) {
22✔
363
            if (!isset($resource->{$key})) {
22✔
364
                return null;
22✔
365
            }
366

367
            $resource = $resource->{$key};
×
368
        }
369

370
        return $this->buildValues($resource->values);
×
371
    }
372

373
    private function buildOperations(\SimpleXMLElement $resource, array $root): ?array
374
    {
375
        if (!isset($resource->operations->operation)) {
22✔
376
            return null;
22✔
377
        }
378

379
        $data = [];
22✔
380
        foreach ($resource->operations->operation as $operation) {
22✔
381
            $datum = $this->buildExtendedBase($operation);
22✔
382
            foreach ($datum as $key => $value) {
22✔
383
                if (null === $value) {
22✔
384
                    $datum[$key] = $root[$key];
22✔
385
                }
386
            }
387

388
            if (\in_array((string) $operation['class'], [GetCollection::class, Post::class], true)) {
22✔
389
                $datum['itemUriTemplate'] = $this->phpize($operation, 'itemUriTemplate', 'string');
22✔
390
            }
391

392
            $data[] = array_merge($datum, [
22✔
393
                'collection' => $this->phpize($operation, 'collection', 'bool'),
22✔
394
                'class' => (string) $operation['class'],
22✔
395
                'method' => $this->phpize($operation, 'method', 'string'),
22✔
396
                'read' => $this->phpize($operation, 'read', 'bool'),
22✔
397
                'deserialize' => $this->phpize($operation, 'deserialize', 'bool'),
22✔
398
                'validate' => $this->phpize($operation, 'validate', 'bool'),
22✔
399
                'write' => $this->phpize($operation, 'write', 'bool'),
22✔
400
                'serialize' => $this->phpize($operation, 'serialize', 'bool'),
22✔
401
                'queryParameterValidate' => $this->phpize($operation, 'queryParameterValidate', 'bool'),
22✔
402
                'priority' => $this->phpize($operation, 'priority', 'integer'),
22✔
403
                'name' => $this->phpize($operation, 'name', 'string'),
22✔
404
            ]);
22✔
405
        }
406

407
        return $data;
22✔
408
    }
409

410
    private function buildGraphQlOperations(\SimpleXMLElement $resource, array $root): ?array
411
    {
412
        if (!isset($resource->graphQlOperations->mutation) && !isset($resource->graphQlOperations->query) && !isset($resource->graphQlOperations->subscription)) {
22✔
413
            return null;
22✔
414
        }
415

416
        $data = [];
×
417
        foreach (['mutation' => Mutation::class, 'query' => Query::class, 'subscription' => Subscription::class] as $type => $class) {
×
418
            foreach ($resource->graphQlOperations->{$type} as $operation) {
×
419
                $datum = $this->buildBase($operation);
×
420
                foreach ($datum as $key => $value) {
×
421
                    if (null === $value) {
×
422
                        $datum[$key] = $root[$key];
×
423
                    }
424
                }
425

426
                $collection = $this->phpize($operation, 'collection', 'bool', false);
×
427
                if (Query::class === $class && $collection) {
×
428
                    $class = QueryCollection::class;
×
429
                }
430

431
                $delete = $this->phpize($operation, 'delete', 'bool', false);
×
432
                if (Mutation::class === $class && $delete) {
×
433
                    $class = DeleteMutation::class;
×
434
                }
435

436
                $data[] = array_merge($datum, [
×
437
                    'graphql_operation_class' => $class,
×
438
                    'resolver' => $this->phpize($operation, 'resolver', 'string'),
×
439
                    'args' => $this->buildArgs($operation),
×
440
                    'class' => $this->phpize($operation, 'class', 'string'),
×
441
                    'read' => $this->phpize($operation, 'read', 'bool'),
×
442
                    'deserialize' => $this->phpize($operation, 'deserialize', 'bool'),
×
443
                    'validate' => $this->phpize($operation, 'validate', 'bool'),
×
444
                    'write' => $this->phpize($operation, 'write', 'bool'),
×
445
                    'serialize' => $this->phpize($operation, 'serialize', 'bool'),
×
446
                    'priority' => $this->phpize($operation, 'priority', 'integer'),
×
447
                ]);
×
448
            }
449
        }
450

451
        return $data;
×
452
    }
453
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc