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

api-platform / core / 3711198772

pending completion
3711198772

push

github

GitHub
fix(jsonschema): remove @id @type @context from input schema  (#5267)

2 of 2 new or added lines in 1 file covered. (100.0%)

7447 of 12203 relevant lines covered (61.03%)

82.13 hits per line

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

76.22
/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 Symfony\Component\Yaml\Exception\ParseException;
25
use Symfony\Component\Yaml\Yaml;
26

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

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

49
            throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
×
50
        }
51

52
        if (null === $resourcesYaml = $resourcesYaml['resources'] ?? $resourcesYaml) {
22✔
53
            return;
×
54
        }
55

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

60
        $this->buildResources($resourcesYaml, $path);
22✔
61
    }
62

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

68
            if (null === $resourceYaml) {
22✔
69
                $resourceYaml = [[]];
22✔
70
            }
71

72
            if (!\array_key_exists(0, $resourceYaml)) {
22✔
73
                $resourceYaml = [$resourceYaml];
22✔
74
            }
75

76
            foreach ($resourceYaml as $key => $resourceYamlDatum) {
22✔
77
                if (null === $resourceYamlDatum) {
22✔
78
                    $resourceYamlDatum = [];
22✔
79
                }
80

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

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

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

167
    private function buildUriVariables(array $resource): ?array
168
    {
169
        if (!\array_key_exists('uriVariables', $resource)) {
22✔
170
            return null;
22✔
171
        }
172

173
        $uriVariables = [];
22✔
174
        foreach ($resource['uriVariables'] as $parameterName => $data) {
22✔
175
            if (\is_string($data)) {
22✔
176
                $uriVariables[$data] = $data;
×
177
                continue;
×
178
            }
179

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

205
        return $uriVariables;
22✔
206
    }
207

208
    /**
209
     * @return bool|string|string[]|null
210
     */
211
    private function buildMercure(array $resource): array|bool|string|null
212
    {
213
        if (!\array_key_exists('mercure', $resource)) {
22✔
214
            return null;
22✔
215
        }
216

217
        if (\is_string($resource['mercure'])) {
×
218
            return $this->phpize($resource, 'mercure', 'bool|string');
×
219
        }
220

221
        return $resource['mercure'];
×
222
    }
223

224
    private function buildMessenger(array $resource): bool|array|string|null
225
    {
226
        if (!\array_key_exists('messenger', $resource)) {
22✔
227
            return null;
22✔
228
        }
229

230
        return $this->phpize($resource, 'messenger', 'bool|string');
×
231
    }
232

233
    private function buildOperations(array $resource, array $root): ?array
234
    {
235
        if (!\array_key_exists('operations', $resource)) {
22✔
236
            return null;
22✔
237
        }
238

239
        $data = [];
22✔
240
        foreach ($resource['operations'] as $class => $operation) {
22✔
241
            if (null === $operation) {
22✔
242
                $operation = [];
22✔
243
            }
244

245
            if (\array_key_exists('class', $operation)) {
22✔
246
                if (!\array_key_exists('name', $operation) && \is_string($class)) {
×
247
                    $operation['name'] = $class;
×
248
                }
249
                $class = $operation['class'];
×
250
            }
251

252
            if (empty($class)) {
22✔
253
                throw new InvalidArgumentException('Missing "class" attribute');
×
254
            }
255

256
            if (!class_exists($class)) {
22✔
257
                throw new InvalidArgumentException(sprintf('Operation class "%s" does not exist', $class));
×
258
            }
259

260
            $datum = $this->buildExtendedBase($operation);
22✔
261
            foreach ($datum as $key => $value) {
22✔
262
                if (null === $value) {
22✔
263
                    $datum[$key] = $root[$key];
22✔
264
                }
265
            }
266

267
            if (\in_array((string) $class, [GetCollection::class, Post::class], true)) {
22✔
268
                $datum['itemUriTemplate'] = $this->phpize($operation, 'itemUriTemplate', 'string');
22✔
269
            }
270

271
            $data[] = array_merge($datum, [
22✔
272
                'read' => $this->phpize($operation, 'read', 'bool'),
22✔
273
                'deserialize' => $this->phpize($operation, 'deserialize', 'bool'),
22✔
274
                'openapi' => $this->phpize($operation, 'openapi', 'bool'),
22✔
275
                'validate' => $this->phpize($operation, 'validate', 'bool'),
22✔
276
                'write' => $this->phpize($operation, 'write', 'bool'),
22✔
277
                'serialize' => $this->phpize($operation, 'serialize', 'bool'),
22✔
278
                'queryParameterValidate' => $this->phpize($operation, 'queryParameterValidate', 'bool'),
22✔
279
                'priority' => $this->phpize($operation, 'priority', 'integer'),
22✔
280
                'name' => $this->phpize($operation, 'name', 'string'),
22✔
281
                'class' => (string) $class,
22✔
282
            ]);
22✔
283
        }
284

285
        return $data;
22✔
286
    }
287

288
    private function buildGraphQlOperations(array $resource, array $root): ?array
289
    {
290
        if (!\array_key_exists('graphQlOperations', $resource) || !\is_array($resource['graphQlOperations'])) {
22✔
291
            return null;
22✔
292
        }
293

294
        $data = [];
22✔
295
        foreach (['mutations' => Mutation::class, 'queries' => Query::class, 'subscriptions' => Subscription::class] as $type => $class) {
22✔
296
            if (!\array_key_exists($type, $resource['graphQlOperations'])) {
22✔
297
                continue;
22✔
298
            }
299

300
            foreach ($resource['graphQlOperations'][$type] as $operation) {
×
301
                $datum = $this->buildBase($operation);
×
302
                foreach ($datum as $key => $value) {
×
303
                    if (null === $value) {
×
304
                        $datum[$key] = $root[$key];
×
305
                    }
306
                }
307

308
                $collection = $this->phpize($operation, 'collection', 'bool', false);
×
309
                if (Query::class === $class && $collection) {
×
310
                    $class = QueryCollection::class;
×
311
                }
312

313
                $delete = $this->phpize($operation, 'delete', 'bool', false);
×
314
                if (Mutation::class === $class && $delete) {
×
315
                    $class = DeleteMutation::class;
×
316
                }
317

318
                $data[] = array_merge($datum, [
×
319
                    'graphql_operation_class' => $class,
×
320
                    'resolver' => $this->phpize($operation, 'resolver', 'string'),
×
321
                    'args' => $operation['args'] ?? null,
×
322
                    'class' => $this->phpize($operation, 'class', 'string'),
×
323
                    'read' => $this->phpize($operation, 'read', 'bool'),
×
324
                    'deserialize' => $this->phpize($operation, 'deserialize', 'bool'),
×
325
                    'validate' => $this->phpize($operation, 'validate', 'bool'),
×
326
                    'write' => $this->phpize($operation, 'write', 'bool'),
×
327
                    'serialize' => $this->phpize($operation, 'serialize', 'bool'),
×
328
                    'priority' => $this->phpize($operation, 'priority', 'integer'),
×
329
                ]);
×
330
            }
331
        }
332

333
        return $data ?: null;
22✔
334
    }
335
}
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