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

api-platform / core / 3713134090

pending completion
3713134090

Pull #5254

github

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

197 of 197 new or added lines in 5 files covered. (100.0%)

7493 of 12362 relevant lines covered (60.61%)

67.56 hits per line

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

70.79
/src/Metadata/Extractor/YamlResourceExtractor.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\RequestBody;
27
use Symfony\Component\Yaml\Exception\ParseException;
28
use Symfony\Component\Yaml\Yaml;
29

30
/**
31
 * Extracts an array of metadata from a list of YAML files.
32
 *
33
 * @author Antoine Bluchet <soyuka@gmail.com>
34
 * @author Baptiste Meyer <baptiste.meyer@gmail.com>
35
 * @author Kévin Dunglas <dunglas@gmail.com>
36
 * @author Vincent Chalamon <vincentchalamon@gmail.com>
37
 */
38
final class YamlResourceExtractor extends AbstractResourceExtractor
39
{
40
    use ResourceExtractorTrait;
41

42
    /**
43
     * {@inheritdoc}
44
     */
45
    protected function extractPath(string $path): void
46
    {
47
        try {
48
            $resourcesYaml = Yaml::parse((string) file_get_contents($path), Yaml::PARSE_CONSTANT);
22✔
49
        } catch (ParseException $e) {
×
50
            $e->setParsedFile($path);
×
51

52
            throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
×
53
        }
54

55
        if (null === $resourcesYaml = $resourcesYaml['resources'] ?? $resourcesYaml) {
22✔
56
            return;
×
57
        }
58

59
        if (!\is_array($resourcesYaml)) {
22✔
60
            throw new InvalidArgumentException(sprintf('"resources" setting is expected to be null or an array, %s given in "%s".', \gettype($resourcesYaml), $path));
×
61
        }
62

63
        $this->buildResources($resourcesYaml, $path);
22✔
64
    }
65

66
    private function buildResources(array $resourcesYaml, string $path): void
67
    {
68
        foreach ($resourcesYaml as $resourceName => $resourceYaml) {
22✔
69
            $resourceName = $this->resolve($resourceName);
22✔
70

71
            if (null === $resourceYaml) {
22✔
72
                $resourceYaml = [[]];
22✔
73
            }
74

75
            if (!\array_key_exists(0, $resourceYaml)) {
22✔
76
                $resourceYaml = [$resourceYaml];
22✔
77
            }
78

79
            foreach ($resourceYaml as $key => $resourceYamlDatum) {
22✔
80
                if (null === $resourceYamlDatum) {
22✔
81
                    $resourceYamlDatum = [];
22✔
82
                }
83

84
                try {
85
                    $base = $this->buildExtendedBase($resourceYamlDatum);
22✔
86
                    $this->resources[$resourceName][$key] = array_merge($base, [
22✔
87
                        'operations' => $this->buildOperations($resourceYamlDatum, $base),
22✔
88
                        'graphQlOperations' => $this->buildGraphQlOperations($resourceYamlDatum, $base),
22✔
89
                    ]);
22✔
90
                } catch (InvalidArgumentException $exception) {
×
91
                    throw new InvalidArgumentException(sprintf('%s in "%s" (%s).', $exception->getMessage(), $resourceName, $path));
×
92
                }
93
            }
94
        }
95
    }
96

97
    private function buildExtendedBase(array $resource): array
98
    {
99
        return array_merge($this->buildBase($resource), [
22✔
100
            'uriTemplate' => $this->phpize($resource, 'uriTemplate', 'string'),
22✔
101
            'routePrefix' => $this->phpize($resource, 'routePrefix', 'string'),
22✔
102
            'stateless' => $this->phpize($resource, 'stateless', 'bool'),
22✔
103
            'sunset' => $this->phpize($resource, 'sunset', 'string'),
22✔
104
            'acceptPatch' => $this->phpize($resource, 'acceptPatch', 'string'),
22✔
105
            'host' => $this->phpize($resource, 'host', 'string'),
22✔
106
            'condition' => $this->phpize($resource, 'condition', 'string'),
22✔
107
            'controller' => $this->phpize($resource, 'controller', 'string'),
22✔
108
            'queryParameterValidationEnabled' => $this->phpize($resource, 'queryParameterValidationEnabled', 'bool'),
22✔
109
            'types' => $this->buildArrayValue($resource, 'types'),
22✔
110
            'cacheHeaders' => $this->buildArrayValue($resource, 'cacheHeaders'),
22✔
111
            'hydraContext' => $this->buildArrayValue($resource, 'hydraContext'),
22✔
112
            'openapiContext' => $this->buildArrayValue($resource, 'openapiContext'), // TODO Remove in 4.0
22✔
113
            'openapi' => $this->buildOpenapi($resource),
22✔
114
            'paginationViaCursor' => $this->buildArrayValue($resource, 'paginationViaCursor'),
22✔
115
            'exceptionToStatus' => $this->buildArrayValue($resource, 'exceptionToStatus'),
22✔
116
            'defaults' => $this->buildArrayValue($resource, 'defaults'),
22✔
117
            'requirements' => $this->buildArrayValue($resource, 'requirements'),
22✔
118
            'options' => $this->buildArrayValue($resource, 'options'),
22✔
119
            'status' => $this->phpize($resource, 'status', 'integer'),
22✔
120
            'schemes' => $this->buildArrayValue($resource, 'schemes'),
22✔
121
            'formats' => $this->buildArrayValue($resource, 'formats'),
22✔
122
            'uriVariables' => $this->buildUriVariables($resource),
22✔
123
            'inputFormats' => $this->buildArrayValue($resource, 'inputFormats'),
22✔
124
            'outputFormats' => $this->buildArrayValue($resource, 'outputFormats'),
22✔
125
        ]);
22✔
126
    }
127

128
    private function buildBase(array $resource): array
129
    {
130
        return [
22✔
131
            'shortName' => $this->phpize($resource, 'shortName', 'string'),
22✔
132
            'description' => $this->phpize($resource, 'description', 'string'),
22✔
133
            'urlGenerationStrategy' => $this->phpize($resource, 'urlGenerationStrategy', 'integer'),
22✔
134
            'deprecationReason' => $this->phpize($resource, 'deprecationReason', 'string'),
22✔
135
            'elasticsearch' => $this->phpize($resource, 'elasticsearch', 'bool'),
22✔
136
            'fetchPartial' => $this->phpize($resource, 'fetchPartial', 'bool'),
22✔
137
            'forceEager' => $this->phpize($resource, 'forceEager', 'bool'),
22✔
138
            'paginationClientEnabled' => $this->phpize($resource, 'paginationClientEnabled', 'bool'),
22✔
139
            'paginationClientItemsPerPage' => $this->phpize($resource, 'paginationClientItemsPerPage', 'bool'),
22✔
140
            'paginationClientPartial' => $this->phpize($resource, 'paginationClientPartial', 'bool'),
22✔
141
            'paginationEnabled' => $this->phpize($resource, 'paginationEnabled', 'bool'),
22✔
142
            'paginationFetchJoinCollection' => $this->phpize($resource, 'paginationFetchJoinCollection', 'bool'),
22✔
143
            'paginationUseOutputWalkers' => $this->phpize($resource, 'paginationUseOutputWalkers', 'bool'),
22✔
144
            'paginationItemsPerPage' => $this->phpize($resource, 'paginationItemsPerPage', 'integer'),
22✔
145
            'paginationMaximumItemsPerPage' => $this->phpize($resource, 'paginationMaximumItemsPerPage', 'integer'),
22✔
146
            'paginationPartial' => $this->phpize($resource, 'paginationPartial', 'bool'),
22✔
147
            'paginationType' => $this->phpize($resource, 'paginationType', 'string'),
22✔
148
            'processor' => $this->phpize($resource, 'processor', 'string'),
22✔
149
            'provider' => $this->phpize($resource, 'provider', 'string'),
22✔
150
            'security' => $this->phpize($resource, 'security', 'string'),
22✔
151
            'securityMessage' => $this->phpize($resource, 'securityMessage', 'string'),
22✔
152
            'securityPostDenormalize' => $this->phpize($resource, 'securityPostDenormalize', 'string'),
22✔
153
            'securityPostDenormalizeMessage' => $this->phpize($resource, 'securityPostDenormalizeMessage', 'string'),
22✔
154
            'securityPostValidation' => $this->phpize($resource, 'securityPostValidation', 'string'),
22✔
155
            'securityPostValidationMessage' => $this->phpize($resource, 'securityPostValidationMessage', 'string'),
22✔
156
            'input' => $this->phpize($resource, 'input', 'bool|string'),
22✔
157
            'output' => $this->phpize($resource, 'output', 'bool|string'),
22✔
158
            'normalizationContext' => $this->buildArrayValue($resource, 'normalizationContext'),
22✔
159
            'denormalizationContext' => $this->buildArrayValue($resource, 'denormalizationContext'),
22✔
160
            'validationContext' => $this->buildArrayValue($resource, 'validationContext'),
22✔
161
            'filters' => $this->buildArrayValue($resource, 'filters'),
22✔
162
            'order' => $this->buildArrayValue($resource, 'order'),
22✔
163
            'extraProperties' => $this->buildArrayValue($resource, 'extraProperties'),
22✔
164
            'mercure' => $this->buildMercure($resource),
22✔
165
            'messenger' => $this->buildMessenger($resource),
22✔
166
            'read' => $this->phpize($resource, 'read', 'bool'),
22✔
167
            'write' => $this->phpize($resource, 'write', 'bool'),
22✔
168
        ];
22✔
169
    }
170

171
    private function buildUriVariables(array $resource): ?array
172
    {
173
        if (!\array_key_exists('uriVariables', $resource)) {
22✔
174
            return null;
22✔
175
        }
176

177
        $uriVariables = [];
22✔
178
        foreach ($resource['uriVariables'] as $parameterName => $data) {
22✔
179
            if (\is_string($data)) {
22✔
180
                $uriVariables[$data] = $data;
×
181
                continue;
×
182
            }
183

184
            if (2 === (is_countable($data) ? \count($data) : 0) && isset($data[0]) && isset($data[1])) {
22✔
185
                $data['fromClass'] = $data[0];
22✔
186
                $data['fromProperty'] = $data[1];
22✔
187
                unset($data[0], $data[1]);
22✔
188
            }
189
            if (isset($data['fromClass'])) {
22✔
190
                $uriVariables[$parameterName]['from_class'] = $data['fromClass'];
22✔
191
            }
192
            if (isset($data['fromProperty'])) {
22✔
193
                $uriVariables[$parameterName]['from_property'] = $data['fromProperty'];
22✔
194
            }
195
            if (isset($data['toClass'])) {
22✔
196
                $uriVariables[$parameterName]['to_class'] = $data['toClass'];
×
197
            }
198
            if (isset($data['toProperty'])) {
22✔
199
                $uriVariables[$parameterName]['to_property'] = $data['toProperty'];
22✔
200
            }
201
            if (isset($data['identifiers'])) {
22✔
202
                $uriVariables[$parameterName]['identifiers'] = $data['identifiers'];
×
203
            }
204
            if (isset($data['compositeIdentifier'])) {
22✔
205
                $uriVariables[$parameterName]['composite_identifier'] = $data['compositeIdentifier'];
×
206
            }
207
        }
208

209
        return $uriVariables;
22✔
210
    }
211

212
    private function buildOpenapi(array $resource): bool|OpenApiOperation|null
213
    {
214
        if (!\array_key_exists('openapi', $resource)) {
22✔
215
            return null;
22✔
216
        }
217

218
        if (!\is_array($resource['openapi'])) {
×
219
            return $this->phpize($resource, 'openapi', 'bool');
×
220
        }
221

222
        $allowedProperties = array_map(fn (\ReflectionProperty $reflProperty): string => $reflProperty->getName(), (new \ReflectionClass(OpenApiOperation::class))->getProperties());
×
223
        foreach ($resource['openapi'] as $key => $value) {
×
224
            $resource['openapi'][$key] = match ($key) {
×
225
                'externalDocs' => new ExternalDocumentation(description: $value['description'] ?? '', url: $value['url'] ?? ''),
×
226
                'requestBody' => new RequestBody(description: $value['description'] ?? '', content: isset($value['content']) ? new \ArrayObject($value['content'] ?? []) : null, required: $value['required'] ?? false),
×
227
                'callbacks' => new \ArrayObject($value ?? []),
×
228
                default => $value,
×
229
            };
×
230

231
            if (\in_array($key, $allowedProperties, true)) {
×
232
                continue;
×
233
            }
234

235
            $resource['openapi']['extensionProperties'][$key] = $value;
×
236
            unset($resource['openapi'][$key]);
×
237
        }
238

239
        return new OpenApiOperation(...$resource['openapi']);
×
240
    }
241

242
    /**
243
     * @return bool|string|string[]|null
244
     */
245
    private function buildMercure(array $resource): array|bool|string|null
246
    {
247
        if (!\array_key_exists('mercure', $resource)) {
22✔
248
            return null;
22✔
249
        }
250

251
        if (\is_string($resource['mercure'])) {
×
252
            return $this->phpize($resource, 'mercure', 'bool|string');
×
253
        }
254

255
        return $resource['mercure'];
×
256
    }
257

258
    private function buildMessenger(array $resource): bool|array|string|null
259
    {
260
        if (!\array_key_exists('messenger', $resource)) {
22✔
261
            return null;
22✔
262
        }
263

264
        return $this->phpize($resource, 'messenger', 'bool|string');
×
265
    }
266

267
    private function buildOperations(array $resource, array $root): ?array
268
    {
269
        if (!\array_key_exists('operations', $resource)) {
22✔
270
            return null;
22✔
271
        }
272

273
        $data = [];
22✔
274
        foreach ($resource['operations'] as $class => $operation) {
22✔
275
            if (null === $operation) {
22✔
276
                $operation = [];
22✔
277
            }
278

279
            if (\array_key_exists('class', $operation)) {
22✔
280
                if (!\array_key_exists('name', $operation) && \is_string($class)) {
×
281
                    $operation['name'] = $class;
×
282
                }
283
                $class = $operation['class'];
×
284
            }
285

286
            if (empty($class)) {
22✔
287
                throw new InvalidArgumentException('Missing "class" attribute');
×
288
            }
289

290
            if (!class_exists($class)) {
22✔
291
                throw new InvalidArgumentException(sprintf('Operation class "%s" does not exist', $class));
×
292
            }
293

294
            $datum = $this->buildExtendedBase($operation);
22✔
295
            foreach ($datum as $key => $value) {
22✔
296
                if (null === $value) {
22✔
297
                    $datum[$key] = $root[$key];
22✔
298
                }
299
            }
300

301
            if (\in_array((string) $class, [GetCollection::class, Post::class], true)) {
22✔
302
                $datum['itemUriTemplate'] = $this->phpize($operation, 'itemUriTemplate', 'string');
22✔
303
            }
304

305
            $data[] = array_merge($datum, [
22✔
306
                'read' => $this->phpize($operation, 'read', 'bool'),
22✔
307
                'deserialize' => $this->phpize($operation, 'deserialize', 'bool'),
22✔
308
                'validate' => $this->phpize($operation, 'validate', 'bool'),
22✔
309
                'write' => $this->phpize($operation, 'write', 'bool'),
22✔
310
                'serialize' => $this->phpize($operation, 'serialize', 'bool'),
22✔
311
                'queryParameterValidate' => $this->phpize($operation, 'queryParameterValidate', 'bool'),
22✔
312
                'priority' => $this->phpize($operation, 'priority', 'integer'),
22✔
313
                'name' => $this->phpize($operation, 'name', 'string'),
22✔
314
                'class' => (string) $class,
22✔
315
            ]);
22✔
316
        }
317

318
        return $data;
22✔
319
    }
320

321
    private function buildGraphQlOperations(array $resource, array $root): ?array
322
    {
323
        if (!\array_key_exists('graphQlOperations', $resource) || !\is_array($resource['graphQlOperations'])) {
22✔
324
            return null;
22✔
325
        }
326

327
        $data = [];
22✔
328
        foreach (['mutations' => Mutation::class, 'queries' => Query::class, 'subscriptions' => Subscription::class] as $type => $class) {
22✔
329
            if (!\array_key_exists($type, $resource['graphQlOperations'])) {
22✔
330
                continue;
22✔
331
            }
332

333
            foreach ($resource['graphQlOperations'][$type] as $operation) {
×
334
                $datum = $this->buildBase($operation);
×
335
                foreach ($datum as $key => $value) {
×
336
                    if (null === $value) {
×
337
                        $datum[$key] = $root[$key];
×
338
                    }
339
                }
340

341
                $collection = $this->phpize($operation, 'collection', 'bool', false);
×
342
                if (Query::class === $class && $collection) {
×
343
                    $class = QueryCollection::class;
×
344
                }
345

346
                $delete = $this->phpize($operation, 'delete', 'bool', false);
×
347
                if (Mutation::class === $class && $delete) {
×
348
                    $class = DeleteMutation::class;
×
349
                }
350

351
                $data[] = array_merge($datum, [
×
352
                    'graphql_operation_class' => $class,
×
353
                    'resolver' => $this->phpize($operation, 'resolver', 'string'),
×
354
                    'args' => $operation['args'] ?? null,
×
355
                    'class' => $this->phpize($operation, 'class', 'string'),
×
356
                    'read' => $this->phpize($operation, 'read', 'bool'),
×
357
                    'deserialize' => $this->phpize($operation, 'deserialize', 'bool'),
×
358
                    'validate' => $this->phpize($operation, 'validate', 'bool'),
×
359
                    'write' => $this->phpize($operation, 'write', 'bool'),
×
360
                    'serialize' => $this->phpize($operation, 'serialize', 'bool'),
×
361
                    'priority' => $this->phpize($operation, 'priority', 'integer'),
×
362
                ]);
×
363
            }
364
        }
365

366
        return $data ?: null;
22✔
367
    }
368
}
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