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

api-platform / core / 19171073905

07 Nov 2025 02:17PM UTC coverage: 0.0% (-24.3%) from 24.303%
19171073905

push

github

web-flow
feat(symfony): allow symfony makers namespace configuration (#7497)

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

14716 existing lines in 467 files now uncovered.

0 of 56762 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/Hydra/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\Hydra\JsonSchema;
15

16
use ApiPlatform\JsonLd\ContextBuilder;
17
use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait;
18
use ApiPlatform\JsonSchema\DefinitionNameFactory;
19
use ApiPlatform\JsonSchema\DefinitionNameFactoryInterface;
20
use ApiPlatform\JsonSchema\ResourceMetadataTrait;
21
use ApiPlatform\JsonSchema\Schema;
22
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
23
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
24
use ApiPlatform\JsonSchema\SchemaUriPrefixTrait;
25
use ApiPlatform\Metadata\Operation;
26
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
27

28
/**
29
 * Decorator factory which adds Hydra properties to the JSON Schema document.
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
34
{
35
    use HydraPrefixTrait;
36
    use ResourceMetadataTrait;
37
    use SchemaUriPrefixTrait;
38

39
    private const ITEM_BASE_SCHEMA_NAME = 'HydraItemBaseSchema';
40
    private const ITEM_WITHOUT_ID_BASE_SCHEMA_NAME = 'HydraItemBaseSchemaWithoutId';
41
    private const COLLECTION_BASE_SCHEMA_NAME = 'HydraCollectionBaseSchema';
42

43
    private const BASE_PROP = [
44
        'type' => 'string',
45
    ];
46
    private const BASE_PROPS = [
47
        '@id' => self::BASE_PROP,
48
        '@type' => self::BASE_PROP,
49
    ];
50
    private const ITEM_BASE_SCHEMA = [
51
        'type' => 'object',
52
        'properties' => [
53
            '@context' => [
54
                'oneOf' => [
55
                    ['type' => 'string'],
56
                    [
57
                        'type' => 'object',
58
                        'properties' => [
59
                            '@vocab' => [
60
                                'type' => 'string',
61
                            ],
62
                            'hydra' => [
63
                                'type' => 'string',
64
                                'enum' => [ContextBuilder::HYDRA_NS],
65
                            ],
66
                        ],
67
                        'required' => ['@vocab', 'hydra'],
68
                        'additionalProperties' => true,
69
                    ],
70
                ],
71
            ],
72
        ] + self::BASE_PROPS,
73
    ];
74

75
    private const ITEM_BASE_SCHEMA_WITH_ID = self::ITEM_BASE_SCHEMA + [
76
        'required' => ['@id', '@type'],
77
    ];
78

79
    private const ITEM_BASE_SCHEMA_WITHOUT_ID = self::ITEM_BASE_SCHEMA + [
80
        'required' => ['@type'],
81
    ];
82

83
    /**
84
     * @var array<string, true>
85
     */
86
    private array $transformed = [];
87

88
    /**
89
     * @param array<string, mixed> $defaultContext
90
     */
91
    public function __construct(
92
        private readonly SchemaFactoryInterface $schemaFactory,
93
        private readonly array $defaultContext = [],
94
        private ?DefinitionNameFactoryInterface $definitionNameFactory = null,
95
        ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null,
96
    ) {
UNCOV
97
        if (!$definitionNameFactory) {
×
98
            $this->definitionNameFactory = new DefinitionNameFactory();
×
99
        }
UNCOV
100
        $this->resourceMetadataFactory = $resourceMetadataFactory;
×
101

UNCOV
102
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
UNCOV
103
            $this->schemaFactory->setSchemaFactory($this);
×
104
        }
105
    }
106

107
    /**
108
     * {@inheritdoc}
109
     */
110
    public function buildSchema(string $className, string $format = 'jsonld', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
111
    {
112
        // The input schema must not include `@id` or `@type` as required fields, so it should be a pure JSON schema.
113
        // Strictly speaking, it is possible to include `@id` or `@context` in the input,
114
        // but the generated JSON Schema does not include `"additionalProperties": false` by default,
115
        // so it is possible to include `@id` or `@context` in the input even if the input schema is a JSON schema.
UNCOV
116
        if (Schema::TYPE_INPUT === $type) {
×
UNCOV
117
            $format = 'json';
×
118
        }
119

UNCOV
120
        if ('jsonld' !== $format || !$this->isResourceClass($className)) {
×
UNCOV
121
            return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
×
122
        }
123

UNCOV
124
        $operation = $this->findOperation($className, $type, $operation, $serializerContext, $format);
×
UNCOV
125
        $inputOrOutputClass = $this->findOutputClass($className, $type, $operation, $serializerContext);
×
UNCOV
126
        $serializerContext ??= $this->getSerializerContext($operation, $type);
×
127

UNCOV
128
        if (null === $inputOrOutputClass) {
×
129
            // input or output disabled
130
            return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
×
131
        }
132

UNCOV
133
        $schema = $this->schemaFactory->buildSchema($className, 'jsonld', $type, $operation, $schema, $serializerContext, $forceCollection);
×
UNCOV
134
        $definitions = $schema->getDefinitions();
×
UNCOV
135
        $prefix = $this->getSchemaUriPrefix($schema->getVersion());
×
UNCOV
136
        $collectionKey = $schema->getItemsDefinitionKey();
×
137

UNCOV
138
        if (!$collectionKey) {
×
UNCOV
139
            $definitionName = $schema->getRootDefinitionKey() ?? $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
×
UNCOV
140
            $this->decorateItemDefinition($definitionName, $definitions, $prefix, $type, $serializerContext);
×
141

UNCOV
142
            if (isset($definitions[$definitionName])) {
×
UNCOV
143
                $currentDefinitions = $schema->getDefinitions();
×
UNCOV
144
                $schema->exchangeArray([]); // Clear the schema
×
UNCOV
145
                $schema['$ref'] = $prefix.$definitionName;
×
UNCOV
146
                $schema->setDefinitions($currentDefinitions);
×
147
            }
148

UNCOV
149
            return $schema;
×
150
        }
151

UNCOV
152
        if (($schema['type'] ?? '') !== 'array') {
×
153
            return $schema;
×
154
        }
155

UNCOV
156
        $hydraPrefix = $this->getHydraPrefix($serializerContext + $this->defaultContext);
×
157

UNCOV
158
        if (!isset($definitions[self::COLLECTION_BASE_SCHEMA_NAME])) {
×
UNCOV
159
            switch ($schema->getVersion()) {
×
160
                // JSON Schema + OpenAPI 3.1
UNCOV
161
                case Schema::VERSION_OPENAPI:
×
UNCOV
162
                case Schema::VERSION_JSON_SCHEMA:
×
UNCOV
163
                    $nullableStringDefinition = ['type' => ['string', 'null']];
×
UNCOV
164
                    break;
×
165
                    // Swagger
166
                default:
167
                    $nullableStringDefinition = ['type' => 'string'];
×
168
                    break;
×
169
            }
170

UNCOV
171
            $definitions[self::COLLECTION_BASE_SCHEMA_NAME] = [
×
UNCOV
172
                'type' => 'object',
×
UNCOV
173
                'properties' => [
×
UNCOV
174
                    $hydraPrefix.'totalItems' => [
×
UNCOV
175
                        'type' => 'integer',
×
UNCOV
176
                        'minimum' => 0,
×
UNCOV
177
                    ],
×
UNCOV
178
                    $hydraPrefix.'view' => [
×
UNCOV
179
                        'type' => 'object',
×
UNCOV
180
                        'properties' => [
×
UNCOV
181
                            '@id' => [
×
UNCOV
182
                                'type' => 'string',
×
UNCOV
183
                                'format' => 'iri-reference',
×
UNCOV
184
                            ],
×
UNCOV
185
                            '@type' => [
×
UNCOV
186
                                'type' => 'string',
×
UNCOV
187
                            ],
×
UNCOV
188
                            $hydraPrefix.'first' => [
×
UNCOV
189
                                'type' => 'string',
×
UNCOV
190
                                'format' => 'iri-reference',
×
UNCOV
191
                            ],
×
UNCOV
192
                            $hydraPrefix.'last' => [
×
UNCOV
193
                                'type' => 'string',
×
UNCOV
194
                                'format' => 'iri-reference',
×
UNCOV
195
                            ],
×
UNCOV
196
                            $hydraPrefix.'previous' => [
×
UNCOV
197
                                'type' => 'string',
×
UNCOV
198
                                'format' => 'iri-reference',
×
UNCOV
199
                            ],
×
UNCOV
200
                            $hydraPrefix.'next' => [
×
UNCOV
201
                                'type' => 'string',
×
UNCOV
202
                                'format' => 'iri-reference',
×
UNCOV
203
                            ],
×
UNCOV
204
                        ],
×
UNCOV
205
                        'example' => [
×
UNCOV
206
                            '@id' => 'string',
×
UNCOV
207
                            'type' => 'string',
×
UNCOV
208
                            $hydraPrefix.'first' => 'string',
×
UNCOV
209
                            $hydraPrefix.'last' => 'string',
×
UNCOV
210
                            $hydraPrefix.'previous' => 'string',
×
UNCOV
211
                            $hydraPrefix.'next' => 'string',
×
UNCOV
212
                        ],
×
UNCOV
213
                    ],
×
UNCOV
214
                    $hydraPrefix.'search' => [
×
UNCOV
215
                        'type' => 'object',
×
UNCOV
216
                        'properties' => [
×
UNCOV
217
                            '@type' => ['type' => 'string'],
×
UNCOV
218
                            $hydraPrefix.'template' => ['type' => 'string'],
×
UNCOV
219
                            $hydraPrefix.'variableRepresentation' => ['type' => 'string'],
×
UNCOV
220
                            $hydraPrefix.'mapping' => [
×
UNCOV
221
                                'type' => 'array',
×
UNCOV
222
                                'items' => [
×
UNCOV
223
                                    'type' => 'object',
×
UNCOV
224
                                    'properties' => [
×
UNCOV
225
                                        '@type' => ['type' => 'string'],
×
UNCOV
226
                                        'variable' => ['type' => 'string'],
×
UNCOV
227
                                        'property' => $nullableStringDefinition,
×
UNCOV
228
                                        'required' => ['type' => 'boolean'],
×
UNCOV
229
                                    ],
×
UNCOV
230
                                ],
×
UNCOV
231
                            ],
×
UNCOV
232
                        ],
×
UNCOV
233
                    ],
×
UNCOV
234
                ],
×
UNCOV
235
            ];
×
236
        }
237

UNCOV
238
        $definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
×
UNCOV
239
        $schema['type'] = 'object';
×
UNCOV
240
        $schema['description'] = "$definitionName collection.";
×
UNCOV
241
        $schema['allOf'] = [
×
UNCOV
242
            ['$ref' => $prefix.self::COLLECTION_BASE_SCHEMA_NAME],
×
UNCOV
243
            [
×
UNCOV
244
                'type' => 'object',
×
UNCOV
245
                'required' => [
×
UNCOV
246
                    $hydraPrefix.'member',
×
UNCOV
247
                ],
×
UNCOV
248
                'properties' => [
×
UNCOV
249
                    $hydraPrefix.'member' => [
×
UNCOV
250
                        'type' => 'array',
×
UNCOV
251
                        'items' => $schema['items'],
×
UNCOV
252
                    ],
×
UNCOV
253
                ],
×
UNCOV
254
            ],
×
UNCOV
255
        ];
×
256

UNCOV
257
        unset($schema['items']);
×
258

UNCOV
259
        if (isset($definitions[$collectionKey])) {
×
UNCOV
260
            $this->decorateItemDefinition($collectionKey, $definitions, $prefix, $type, $serializerContext);
×
261
        }
262

UNCOV
263
        return $schema;
×
264
    }
265

266
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
267
    {
UNCOV
268
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
UNCOV
269
            $this->schemaFactory->setSchemaFactory($schemaFactory);
×
270
        }
271
    }
272

273
    private function decorateItemDefinition(string $definitionName, \ArrayObject $definitions, string $prefix, string $type, ?array $serializerContext): void
274
    {
UNCOV
275
        if (!isset($definitions[$definitionName]) || ($this->transformed[$definitionName] ?? false)) {
×
UNCOV
276
            return;
×
277
        }
278

UNCOV
279
        $hasNoId = Schema::TYPE_OUTPUT === $type && false === ($serializerContext['gen_id'] ?? true);
×
UNCOV
280
        $baseName = self::ITEM_BASE_SCHEMA_NAME;
×
UNCOV
281
        if ($hasNoId) {
×
UNCOV
282
            $baseName = self::ITEM_WITHOUT_ID_BASE_SCHEMA_NAME;
×
283
        }
284

UNCOV
285
        if (!isset($definitions[$baseName])) {
×
UNCOV
286
            $definitions[$baseName] = $hasNoId ? self::ITEM_BASE_SCHEMA_WITHOUT_ID : self::ITEM_BASE_SCHEMA_WITH_ID;
×
287
        }
288

UNCOV
289
        $allOf = new \ArrayObject(['allOf' => [
×
UNCOV
290
            ['$ref' => $prefix.$baseName],
×
UNCOV
291
            $definitions[$definitionName],
×
UNCOV
292
        ]]);
×
293

UNCOV
294
        if (isset($definitions[$definitionName]['description'])) {
×
UNCOV
295
            $allOf['description'] = $definitions[$definitionName]['description'];
×
296
        }
297

UNCOV
298
        $definitions[$definitionName] = $allOf;
×
UNCOV
299
        unset($definitions[$definitionName]['allOf'][1]['description']);
×
300

UNCOV
301
        $this->transformed[$definitionName] = true;
×
302
    }
303
}
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