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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

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

11040 existing lines in 370 files now uncovered.

0 of 48303 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
    {
UNCOV
119
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
UNCOV
120
            $this->schemaFactory->setSchemaFactory($this);
×
121
        }
UNCOV
122
        $this->resourceClassResolver = $resourceClassResolver;
×
UNCOV
123
        $this->resourceMetadataFactory = $resourceMetadataFactory;
×
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
    {
UNCOV
131
        if ('jsonapi' !== $format) {
×
UNCOV
132
            return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
×
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
UNCOV
136
        $serializerContext ??= $this->getSerializerContext($operation ?? $this->findOperation($className, $type, $operation, $serializerContext, $format), $type);
×
UNCOV
137
        $jsonApiSerializerContext = !($serializerContext[self::DISABLE_JSON_SCHEMA_SERIALIZER_GROUPS] ?? true) ? $serializerContext : [];
×
UNCOV
138
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $jsonApiSerializerContext, $forceCollection);
×
139

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

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

UNCOV
152
                return $schema;
×
153
            }
154

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

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

UNCOV
162
            if ($schema->getRootDefinitionKey()) {
×
UNCOV
163
                return $schema;
×
164
            }
165
        }
166

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

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

UNCOV
185
            return $schema;
×
186
        }
187

188
        return $schema;
×
189
    }
190

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

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

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

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

UNCOV
240
        $replacement = self::PROPERTY_PROPS;
×
UNCOV
241
        $replacement['attributes']['properties'] = $attributes;
×
242

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

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

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

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

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

UNCOV
312
        return $isRelationship ? [$isOne, $relatedClasses] : null;
×
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

© 2025 Coveralls, Inc