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

api-platform / core / 13857701466

14 Mar 2025 01:32PM UTC coverage: 7.91% (-0.6%) from 8.515%
13857701466

Pull #7020

github

web-flow
Merge ed1c6e6d2 into 1e7076c65
Pull Request #7020: feat(laravel): stateOptions modelClass for eloquent

0 of 31 new or added lines in 7 files covered. (0.0%)

2655 existing lines in 185 files now uncovered.

10732 of 135670 relevant lines covered (7.91%)

7.04 hits per line

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

98.44
/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
use ApiPlatform\State\ApiResource\Error;
26

27
/**
28
 * Decorator factory which adds JSON:API properties to the JSON Schema document.
29
 *
30
 * @author Gwendolen Lynch <gwendolen.lynch@gmail.com>
31
 */
32
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
33
{
34
    use ResourceMetadataTrait;
35

36
    /**
37
     * As JSON:API recommends using [includes](https://jsonapi.org/format/#fetching-includes) instead of groups
38
     * this flag allows to force using groups to generate the JSON:API JSONSchema. Defaults to true, use it in
39
     * a serializer context.
40
     */
41
    public const DISABLE_JSON_SCHEMA_SERIALIZER_GROUPS = 'disable_json_schema_serializer_groups';
42

43
    private const LINKS_PROPS = [
44
        'type' => 'object',
45
        'properties' => [
46
            'self' => [
47
                'type' => 'string',
48
                'format' => 'iri-reference',
49
            ],
50
            'first' => [
51
                'type' => 'string',
52
                'format' => 'iri-reference',
53
            ],
54
            'prev' => [
55
                'type' => 'string',
56
                'format' => 'iri-reference',
57
            ],
58
            'next' => [
59
                'type' => 'string',
60
                'format' => 'iri-reference',
61
            ],
62
            'last' => [
63
                'type' => 'string',
64
                'format' => 'iri-reference',
65
            ],
66
        ],
67
        'example' => [
68
            'self' => 'string',
69
            'first' => 'string',
70
            'prev' => 'string',
71
            'next' => 'string',
72
            'last' => 'string',
73
        ],
74
    ];
75
    private const META_PROPS = [
76
        'type' => 'object',
77
        'properties' => [
78
            'totalItems' => [
79
                'type' => 'integer',
80
                'minimum' => 0,
81
            ],
82
            'itemsPerPage' => [
83
                'type' => 'integer',
84
                'minimum' => 0,
85
            ],
86
            'currentPage' => [
87
                'type' => 'integer',
88
                'minimum' => 0,
89
            ],
90
        ],
91
    ];
92
    private const RELATION_PROPS = [
93
        'type' => 'object',
94
        'properties' => [
95
            'type' => [
96
                'type' => 'string',
97
            ],
98
            'id' => [
99
                'type' => 'string',
100
                'format' => 'iri-reference',
101
            ],
102
        ],
103
    ];
104
    private const PROPERTY_PROPS = [
105
        'id' => [
106
            'type' => 'string',
107
        ],
108
        'type' => [
109
            'type' => 'string',
110
        ],
111
        'attributes' => [
112
            'type' => 'object',
113
            'properties' => [],
114
        ],
115
    ];
116

117
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory, private readonly PropertyMetadataFactoryInterface $propertyMetadataFactory, ResourceClassResolverInterface $resourceClassResolver, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null, private readonly ?DefinitionNameFactoryInterface $definitionNameFactory = null)
118
    {
119
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
473✔
120
            $this->schemaFactory->setSchemaFactory($this);
473✔
121
        }
122
        $this->resourceClassResolver = $resourceClassResolver;
473✔
123
        $this->resourceMetadataFactory = $resourceMetadataFactory;
473✔
124
    }
125

126
    /**
127
     * {@inheritdoc}
128
     */
129
    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
130
    {
131
        if ('jsonapi' !== $format) {
98✔
132
            return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
82✔
133
        }
134
        // We don't use the serializer context here as JSON:API doesn't leverage serializer groups for related resources.
135
        // That is done by query parameter. @see https://jsonapi.org/format/#fetching-includes
136
        $serializerContext ??= $this->getSerializerContext($operation ?? $this->findOperation($className, $type, $operation, $serializerContext, $format), $type);
36✔
137
        $jsonApiSerializerContext = !($serializerContext[self::DISABLE_JSON_SCHEMA_SERIALIZER_GROUPS] ?? true) ? $serializerContext : [];
36✔
138
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $jsonApiSerializerContext, $forceCollection);
36✔
139

140
        if (($key = $schema->getRootDefinitionKey()) || ($key = $schema->getItemsDefinitionKey())) {
36✔
141
            $definitions = $schema->getDefinitions();
36✔
142
            $properties = $definitions[$key]['properties'] ?? [];
36✔
143

144
            if (Error::class === $className && !isset($properties['errors'])) {
36✔
145
                $definitions[$key]['properties'] = [
18✔
146
                    'errors' => [
18✔
147
                        'type' => 'object',
18✔
148
                        'properties' => $properties,
18✔
149
                    ],
18✔
150
                ];
18✔
151

152
                return $schema;
18✔
153
            }
154

155
            // Prevent reapplying
156
            if (isset($properties['id'], $properties['type']) || isset($properties['data']) || isset($properties['errors'])) {
36✔
157
                return $schema;
20✔
158
            }
159

160
            $definitions[$key]['properties'] = $this->buildDefinitionPropertiesSchema($key, $className, $format, $type, $operation, $schema, []);
36✔
161

162
            if ($schema->getRootDefinitionKey()) {
36✔
163
                return $schema;
32✔
164
            }
165
        }
166

167
        if (($schema['type'] ?? '') === 'array') {
20✔
168
            // data
169
            $items = $schema['items'];
20✔
170
            unset($schema['items']);
20✔
171

172
            $schema['type'] = 'object';
20✔
173
            $schema['properties'] = [
20✔
174
                'links' => self::LINKS_PROPS,
20✔
175
                'meta' => self::META_PROPS,
20✔
176
                'data' => [
20✔
177
                    'type' => 'array',
20✔
178
                    'items' => $items,
20✔
179
                ],
20✔
180
            ];
20✔
181
            $schema['required'] = [
20✔
182
                'data',
20✔
183
            ];
20✔
184

185
            return $schema;
20✔
186
        }
187

UNCOV
188
        return $schema;
×
189
    }
190

191
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
192
    {
193
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
473✔
194
            $this->schemaFactory->setSchemaFactory($schemaFactory);
473✔
195
        }
196
    }
197

198
    private function buildDefinitionPropertiesSchema(string $key, string $className, string $format, string $type, ?Operation $operation, Schema $schema, ?array $serializerContext): array
199
    {
200
        $definitions = $schema->getDefinitions();
36✔
201
        $properties = $definitions[$key]['properties'] ?? [];
36✔
202

203
        $attributes = [];
36✔
204
        $relationships = [];
36✔
205
        $relatedDefinitions = [];
36✔
206
        foreach ($properties as $propertyName => $property) {
36✔
207
            if ($relation = $this->getRelationship($className, $propertyName, $serializerContext)) {
36✔
208
                [$isOne, $relatedClasses] = $relation;
10✔
209
                $refs = [];
10✔
210
                foreach ($relatedClasses as $relatedClassName => $hasOperations) {
10✔
211
                    if (false === $hasOperations) {
10✔
UNCOV
212
                        continue;
×
213
                    }
214

215
                    $operation = $this->findOperation($relatedClassName, $type, $operation, $serializerContext);
10✔
216
                    $inputOrOutputClass = $this->findOutputClass($relatedClassName, $type, $operation, $serializerContext);
10✔
217
                    $serializerContext ??= $this->getSerializerContext($operation, $type);
10✔
218
                    $definitionName = $this->definitionNameFactory->create($relatedClassName, $format, $inputOrOutputClass, $operation, $serializerContext);
10✔
219
                    $ref = Schema::VERSION_OPENAPI === $schema->getVersion() ? '#/components/schemas/'.$definitionName : '#/definitions/'.$definitionName;
10✔
220
                    $refs[$ref] = '$ref';
10✔
221
                }
222
                $relatedDefinitions[$propertyName] = array_flip($refs);
10✔
223
                if ($isOne) {
10✔
224
                    $relationships[$propertyName]['properties']['data'] = self::RELATION_PROPS;
8✔
225
                    continue;
8✔
226
                }
227
                $relationships[$propertyName]['properties']['data'] = [
10✔
228
                    'type' => 'array',
10✔
229
                    'items' => self::RELATION_PROPS,
10✔
230
                ];
10✔
231
                continue;
10✔
232
            }
233
            if ('id' === $propertyName) {
36✔
234
                $attributes['_id'] = $property;
30✔
235
                continue;
30✔
236
            }
237
            $attributes[$propertyName] = $property;
32✔
238
        }
239

240
        $replacement = self::PROPERTY_PROPS;
36✔
241
        $replacement['attributes']['properties'] = $attributes;
36✔
242

243
        $included = [];
36✔
244
        if (\count($relationships) > 0) {
36✔
245
            $replacement['relationships'] = [
10✔
246
                'type' => 'object',
10✔
247
                'properties' => $relationships,
10✔
248
            ];
10✔
249
            $included = [
10✔
250
                'included' => [
10✔
251
                    'description' => 'Related resources requested via the "include" query parameter.',
10✔
252
                    'type' => 'array',
10✔
253
                    'items' => [
10✔
254
                        'anyOf' => array_values($relatedDefinitions),
10✔
255
                    ],
10✔
256
                    'readOnly' => true,
10✔
257
                    'externalDocs' => [
10✔
258
                        'url' => 'https://jsonapi.org/format/#fetching-includes',
10✔
259
                    ],
10✔
260
                ],
10✔
261
            ];
10✔
262
        }
263

264
        if ($required = $definitions[$key]['required'] ?? null) {
36✔
265
            foreach ($required as $require) {
8✔
266
                if (isset($replacement['attributes']['properties'][$require])) {
8✔
267
                    $replacement['attributes']['required'][] = $require;
6✔
268
                    continue;
6✔
269
                }
270
                if (isset($relationships[$require])) {
8✔
271
                    $replacement['relationships']['required'][] = $require;
8✔
272
                }
273
            }
274
            unset($definitions[$key]['required']);
8✔
275
        }
276

277
        return [
36✔
278
            'data' => [
36✔
279
                'type' => 'object',
36✔
280
                'properties' => $replacement,
36✔
281
                'required' => ['type', 'id'],
36✔
282
            ],
36✔
283
        ] + $included;
36✔
284
    }
285

286
    private function getRelationship(string $resourceClass, string $property, ?array $serializerContext): ?array
287
    {
288
        $propertyMetadata = $this->propertyMetadataFactory->create($resourceClass, $property, $serializerContext ?? []);
36✔
289
        $types = $propertyMetadata->getBuiltinTypes() ?? [];
36✔
290
        $isRelationship = false;
36✔
291
        $isOne = $isMany = false;
36✔
292
        $relatedClasses = [];
36✔
293

294
        foreach ($types as $type) {
36✔
295
            if ($type->isCollection()) {
36✔
296
                $collectionValueType = $type->getCollectionValueTypes()[0] ?? null;
12✔
297
                $isMany = $collectionValueType && ($className = $collectionValueType->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
12✔
298
            } else {
299
                $isOne = ($className = $type->getClassName()) && $this->resourceClassResolver->isResourceClass($className);
36✔
300
            }
301
            if (!isset($className) || (!$isOne && !$isMany)) {
36✔
302
                continue;
36✔
303
            }
304
            $isRelationship = true;
10✔
305
            $resourceMetadata = $this->resourceMetadataFactory->create($className);
10✔
306
            $operation = $resourceMetadata->getOperation();
10✔
307
            // @see https://github.com/api-platform/core/issues/5501
308
            // @see https://github.com/api-platform/core/pull/5722
309
            $relatedClasses[$className] = $operation->canRead();
10✔
310
        }
311

312
        return $isRelationship ? [$isOne, $relatedClasses] : null;
36✔
313
    }
314
}
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

© 2026 Coveralls, Inc