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

api-platform / core / 13814792797

12 Mar 2025 03:09PM UTC coverage: 5.889% (-1.4%) from 7.289%
13814792797

Pull #7012

github

web-flow
Merge 199d44919 into 284937039
Pull Request #7012: doc: comment typo in ApiResource.php

10048 of 170615 relevant lines covered (5.89%)

5.17 hits per line

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

98.31
/src/JsonApi/JsonSchema/SchemaFactory.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\JsonApi\JsonSchema;
15

16
use ApiPlatform\JsonSchema\DefinitionNameFactoryInterface;
17
use ApiPlatform\JsonSchema\ResourceMetadataTrait;
18
use ApiPlatform\JsonSchema\Schema;
19
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
20
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
21
use ApiPlatform\Metadata\Operation;
22
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
23
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
24
use ApiPlatform\Metadata\ResourceClassResolverInterface;
25

26
/**
27
 * Decorator factory which adds JSON:API properties to the JSON Schema document.
28
 *
29
 * @author Gwendolen Lynch <gwendolen.lynch@gmail.com>
30
 */
31
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
32
{
33
    use ResourceMetadataTrait;
34
    private const LINKS_PROPS = [
35
        'type' => 'object',
36
        'properties' => [
37
            'self' => [
38
                'type' => 'string',
39
                'format' => 'iri-reference',
40
            ],
41
            'first' => [
42
                'type' => 'string',
43
                'format' => 'iri-reference',
44
            ],
45
            'prev' => [
46
                'type' => 'string',
47
                'format' => 'iri-reference',
48
            ],
49
            'next' => [
50
                'type' => 'string',
51
                'format' => 'iri-reference',
52
            ],
53
            'last' => [
54
                'type' => 'string',
55
                'format' => 'iri-reference',
56
            ],
57
        ],
58
        'example' => [
59
            'self' => 'string',
60
            'first' => 'string',
61
            'prev' => 'string',
62
            'next' => 'string',
63
            'last' => 'string',
64
        ],
65
    ];
66
    private const META_PROPS = [
67
        'type' => 'object',
68
        'properties' => [
69
            'totalItems' => [
70
                'type' => 'integer',
71
                'minimum' => 0,
72
            ],
73
            'itemsPerPage' => [
74
                'type' => 'integer',
75
                'minimum' => 0,
76
            ],
77
            'currentPage' => [
78
                'type' => 'integer',
79
                'minimum' => 0,
80
            ],
81
        ],
82
    ];
83
    private const RELATION_PROPS = [
84
        'type' => 'object',
85
        'properties' => [
86
            'type' => [
87
                'type' => 'string',
88
            ],
89
            'id' => [
90
                'type' => 'string',
91
                'format' => 'iri-reference',
92
            ],
93
        ],
94
    ];
95
    private const PROPERTY_PROPS = [
96
        'id' => [
97
            'type' => 'string',
98
        ],
99
        'type' => [
100
            'type' => 'string',
101
        ],
102
        'attributes' => [
103
            'type' => 'object',
104
            'properties' => [],
105
        ],
106
    ];
107

108
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, private readonly ?DefinitionNameFactoryInterface $definitionNameFactory = null)
109
    {
110
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
429✔
111
            $this->schemaFactory->setSchemaFactory($this);
429✔
112
        }
113
        $this->resourceClassResolver = $resourceClassResolver;
429✔
114
        $this->resourceMetadataFactory = $resourceMetadataFactory;
429✔
115
    }
116

117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function buildSchema(string $className, string $format = 'jsonapi', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
121
    {
122
        if ('jsonapi' !== $format) {
138✔
123
            return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
114✔
124
        }
125
        // We don't use the serializer context here as JSON:API doesn't leverage serializer groups for related resources.
126
        // That is done by query parameter. @see https://jsonapi.org/format/#fetching-includes
127
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, [], $forceCollection);
45✔
128

129
        if (($key = $schema->getRootDefinitionKey()) || ($key = $schema->getItemsDefinitionKey())) {
45✔
130
            $definitions = $schema->getDefinitions();
45✔
131
            $properties = $definitions[$key]['properties'] ?? [];
45✔
132

133
            // Prevent reapplying
134
            if (isset($properties['id'], $properties['type']) || isset($properties['data'])) {
45✔
135
                return $schema;
24✔
136
            }
137

138
            $definitions[$key]['properties'] = $this->buildDefinitionPropertiesSchema($key, $className, $format, $type, $operation, $schema, []);
45✔
139

140
            if ($schema->getRootDefinitionKey()) {
45✔
141
                return $schema;
42✔
142
            }
143
        }
144

145
        if (($schema['type'] ?? '') === 'array') {
21✔
146
            // data
147
            $items = $schema['items'];
21✔
148
            unset($schema['items']);
21✔
149

150
            $schema['type'] = 'object';
21✔
151
            $schema['properties'] = [
21✔
152
                'links' => self::LINKS_PROPS,
21✔
153
                'meta' => self::META_PROPS,
21✔
154
                'data' => [
21✔
155
                    'type' => 'array',
21✔
156
                    'items' => $items,
21✔
157
                ],
21✔
158
            ];
21✔
159
            $schema['required'] = [
21✔
160
                'data',
21✔
161
            ];
21✔
162

163
            return $schema;
21✔
164
        }
165

166
        return $schema;
×
167
    }
168

169
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
170
    {
171
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
429✔
172
            $this->schemaFactory->setSchemaFactory($schemaFactory);
429✔
173
        }
174
    }
175

176
    private function buildDefinitionPropertiesSchema(string $key, string $className, string $format, string $type, ?Operation $operation, Schema $schema, ?array $serializerContext): array
177
    {
178
        $definitions = $schema->getDefinitions();
45✔
179
        $properties = $definitions[$key]['properties'] ?? [];
45✔
180

181
        $attributes = [];
45✔
182
        $relationships = [];
45✔
183
        $relatedDefinitions = [];
45✔
184
        foreach ($properties as $propertyName => $property) {
45✔
185
            if ($relation = $this->getRelationship($className, $propertyName, $serializerContext)) {
45✔
186
                [$isOne, $relatedClasses] = $relation;
15✔
187
                $refs = [];
15✔
188
                foreach ($relatedClasses as $relatedClassName => $hasOperations) {
15✔
189
                    if (false === $hasOperations) {
15✔
190
                        continue;
×
191
                    }
192

193
                    $operation = $this->findOperation($relatedClassName, $type, $operation, $serializerContext);
15✔
194
                    $inputOrOutputClass = $this->findOutputClass($relatedClassName, $type, $operation, $serializerContext);
15✔
195
                    $serializerContext ??= $this->getSerializerContext($operation, $type);
15✔
196
                    $definitionName = $this->definitionNameFactory->create($relatedClassName, $format, $inputOrOutputClass, $operation, $serializerContext);
15✔
197
                    $ref = Schema::VERSION_OPENAPI === $schema->getVersion() ? '#/components/schemas/'.$definitionName : '#/definitions/'.$definitionName;
15✔
198
                    $refs[$ref] = '$ref';
15✔
199
                }
200
                $relatedDefinitions[$propertyName] = array_flip($refs);
15✔
201
                if ($isOne) {
15✔
202
                    $relationships[$propertyName]['properties']['data'] = self::RELATION_PROPS;
12✔
203
                    continue;
12✔
204
                }
205
                $relationships[$propertyName]['properties']['data'] = [
15✔
206
                    'type' => 'array',
15✔
207
                    'items' => self::RELATION_PROPS,
15✔
208
                ];
15✔
209
                continue;
15✔
210
            }
211
            if ('id' === $propertyName) {
45✔
212
                $attributes['_id'] = $property;
36✔
213
                continue;
36✔
214
            }
215
            $attributes[$propertyName] = $property;
42✔
216
        }
217

218
        $replacement = self::PROPERTY_PROPS;
45✔
219
        $replacement['attributes']['properties'] = $attributes;
45✔
220

221
        $included = [];
45✔
222
        if (\count($relationships) > 0) {
45✔
223
            $replacement['relationships'] = [
15✔
224
                'type' => 'object',
15✔
225
                'properties' => $relationships,
15✔
226
            ];
15✔
227
            $included = [
15✔
228
                'included' => [
15✔
229
                    'description' => 'Related resources requested via the "include" query parameter.',
15✔
230
                    'type' => 'array',
15✔
231
                    'items' => [
15✔
232
                        'anyOf' => array_values($relatedDefinitions),
15✔
233
                    ],
15✔
234
                    'readOnly' => true,
15✔
235
                    'externalDocs' => [
15✔
236
                        'url' => 'https://jsonapi.org/format/#fetching-includes',
15✔
237
                    ],
15✔
238
                ],
15✔
239
            ];
15✔
240
        }
241

242
        if ($required = $definitions[$key]['required'] ?? null) {
45✔
243
            foreach ($required as $require) {
12✔
244
                if (isset($replacement['attributes']['properties'][$require])) {
12✔
245
                    $replacement['attributes']['required'][] = $require;
9✔
246
                    continue;
9✔
247
                }
248
                if (isset($relationships[$require])) {
12✔
249
                    $replacement['relationships']['required'][] = $require;
12✔
250
                }
251
            }
252
            unset($definitions[$key]['required']);
12✔
253
        }
254

255
        return [
45✔
256
            'data' => [
45✔
257
                'type' => 'object',
45✔
258
                'properties' => $replacement,
45✔
259
                'required' => ['type', 'id'],
45✔
260
            ],
45✔
261
        ] + $included;
45✔
262
    }
263

264
    private function getRelationship(string $resourceClass, string $property, ?array $serializerContext): ?array
265
    {
266
        $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property, $serializerContext ?? []);
45✔
267
        $types = $propertyMetadata->getBuiltinTypes() ?? [];
45✔
268
        $isRelationship = false;
45✔
269
        $isOne = $isMany = false;
45✔
270
        $relatedClasses = [];
45✔
271

272
        foreach ($types as $type) {
45✔
273
            if ($type->isCollection()) {
45✔
274
                $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
18✔
275
                $isMany = $collectionValueType && ($className = $collectionValueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
18✔
276
            } else {
277
                $isOne = ($className = $type->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
45✔
278
            }
279
            if (!isset($className) || (!$isOne && !$isMany)) {
45✔
280
                continue;
45✔
281
            }
282
            $isRelationship = true;
15✔
283
            $resourceMetadata = $this->resourceMetadataFactory->create($className);
15✔
284
            $operation = $resourceMetadata->getOperation();
15✔
285
            // @see https://github.com/api-platform/core/issues/5501
286
            // @see https://github.com/api-platform/core/pull/5722
287
            $relatedClasses[$className] = $operation->canRead();
15✔
288
        }
289

290
        return $isRelationship ? [$isOne, $relatedClasses] : null;
45✔
291
    }
292
}
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