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

api-platform / core / 16705318661

03 Aug 2025 01:05PM UTC coverage: 0.0% (-21.9%) from 21.944%
16705318661

Pull #7317

github

web-flow
Merge 1ca8642ff into d06b1a0a0
Pull Request #7317: Fix/4372 skip null values in hal

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

11680 existing lines in 376 files now uncovered.

0 of 51817 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_BASE_SCHEMA_OUTPUT_NAME = 'HydraOutputBaseSchema';
41
    private const COLLECTION_BASE_SCHEMA_NAME = 'HydraCollectionBaseSchema';
42
    private const BASE_PROP = [
43
        'type' => 'string',
44
    ];
45
    private const BASE_PROPS = [
46
        '@id' => self::BASE_PROP,
47
        '@type' => self::BASE_PROP,
48
    ];
49
    private const ITEM_BASE_SCHEMA = [
50
        'type' => 'object',
51
        'properties' => [
52
            '@context' => [
53
                'oneOf' => [
54
                    ['type' => 'string'],
55
                    [
56
                        'type' => 'object',
57
                        'properties' => [
58
                            '@vocab' => [
59
                                'type' => 'string',
60
                            ],
61
                            'hydra' => [
62
                                'type' => 'string',
63
                                'enum' => [ContextBuilder::HYDRA_NS],
64
                            ],
65
                        ],
66
                        'required' => ['@vocab', 'hydra'],
67
                        'additionalProperties' => true,
68
                    ],
69
                ],
70
            ],
71
        ] + self::BASE_PROPS,
72
    ];
73

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

78
    /**
79
     * @var array<string, true>
80
     */
81
    private array $transformed = [];
82

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

UNCOV
97
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
UNCOV
98
            $this->schemaFactory->setSchemaFactory($this);
×
99
        }
100
    }
101

102
    /**
103
     * {@inheritdoc}
104
     */
105
    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
106
    {
UNCOV
107
        if ('jsonld' !== $format || 'input' === $type) {
×
UNCOV
108
            return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
×
109
        }
UNCOV
110
        if (!$this->isResourceClass($className)) {
×
UNCOV
111
            $operation = null;
×
UNCOV
112
            $inputOrOutputClass = null;
×
UNCOV
113
            $serializerContext ??= [];
×
114
        } else {
UNCOV
115
            $operation = $this->findOperation($className, $type, $operation, $serializerContext, $format);
×
UNCOV
116
            $inputOrOutputClass = $this->findOutputClass($className, $type, $operation, $serializerContext);
×
UNCOV
117
            $serializerContext ??= $this->getSerializerContext($operation, $type);
×
118
        }
119

UNCOV
120
        if (null === $inputOrOutputClass) {
×
121
            // input or output disabled
UNCOV
122
            return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
×
123
        }
124

UNCOV
125
        $definitionName = $this->definitionNameFactory->create($className, $format, $inputOrOutputClass, $operation, $serializerContext);
×
126

127
        // JSON-LD is slightly different then JSON:API or HAL
128
        // All the references that are resources must also be in JSON-LD therefore combining
129
        // the HydraItemBaseSchema and the JSON schema is harder (unless we loop again through all relationship)
130
        // The less intensive path is to compute the jsonld schemas, then to combine in an allOf
UNCOV
131
        $schema = $this->schemaFactory->buildSchema($className, 'jsonld', $type, $operation, $schema, $serializerContext, $forceCollection);
×
UNCOV
132
        $definitions = $schema->getDefinitions();
×
133

UNCOV
134
        $prefix = $this->getSchemaUriPrefix($schema->getVersion());
×
UNCOV
135
        $collectionKey = $schema->getItemsDefinitionKey();
×
UNCOV
136
        $key = $schema->getRootDefinitionKey() ?? $collectionKey;
×
137

UNCOV
138
        if (!$collectionKey) {
×
UNCOV
139
            if ($this->transformed[$definitionName] ?? false) {
×
UNCOV
140
                return $schema;
×
141
            }
142

UNCOV
143
            $baseName = Schema::TYPE_OUTPUT === $type ? self::ITEM_BASE_SCHEMA_NAME : self::ITEM_BASE_SCHEMA_OUTPUT_NAME;
×
144

UNCOV
145
            if ($this->isResourceClass($inputOrOutputClass)) {
×
UNCOV
146
                if (!isset($definitions[$baseName])) {
×
UNCOV
147
                    $definitions[$baseName] = Schema::TYPE_OUTPUT === $type ? self::ITEM_BASE_SCHEMA_OUTPUT : self::ITEM_BASE_SCHEMA;
×
148
                }
149
            }
150

UNCOV
151
            $allOf = new \ArrayObject(['allOf' => [
×
UNCOV
152
                ['$ref' => $prefix.$baseName],
×
UNCOV
153
                $definitions[$key],
×
UNCOV
154
            ]]);
×
155

UNCOV
156
            if (isset($definitions[$key]['description'])) {
×
UNCOV
157
                $allOf['description'] = $definitions[$key]['description'];
×
158
            }
159

UNCOV
160
            $definitions[$definitionName] = $allOf;
×
UNCOV
161
            unset($definitions[$definitionName]['allOf'][1]['description']);
×
162

UNCOV
163
            $schema['$ref'] = $prefix.$definitionName;
×
164

UNCOV
165
            $this->transformed[$definitionName] = true;
×
166

UNCOV
167
            return $schema;
×
168
        }
169

UNCOV
170
        if (($schema['type'] ?? '') !== 'array') {
×
171
            return $schema;
×
172
        }
173

UNCOV
174
        $hydraPrefix = $this->getHydraPrefix($serializerContext + $this->defaultContext);
×
175

UNCOV
176
        if (!isset($definitions[self::COLLECTION_BASE_SCHEMA_NAME])) {
×
UNCOV
177
            switch ($schema->getVersion()) {
×
178
                // JSON Schema + OpenAPI 3.1
UNCOV
179
                case Schema::VERSION_OPENAPI:
×
UNCOV
180
                case Schema::VERSION_JSON_SCHEMA:
×
UNCOV
181
                    $nullableStringDefinition = ['type' => ['string', 'null']];
×
UNCOV
182
                    break;
×
183
                    // Swagger
184
                default:
185
                    $nullableStringDefinition = ['type' => 'string'];
×
186
                    break;
×
187
            }
188

UNCOV
189
            $definitions[self::COLLECTION_BASE_SCHEMA_NAME] = [
×
UNCOV
190
                'type' => 'object',
×
UNCOV
191
                'required' => [
×
UNCOV
192
                    $hydraPrefix.'member',
×
UNCOV
193
                ],
×
UNCOV
194
                'properties' => [
×
UNCOV
195
                    $hydraPrefix.'member' => [
×
UNCOV
196
                        'type' => 'array',
×
UNCOV
197
                    ],
×
UNCOV
198
                    $hydraPrefix.'totalItems' => [
×
UNCOV
199
                        'type' => 'integer',
×
UNCOV
200
                        'minimum' => 0,
×
UNCOV
201
                    ],
×
UNCOV
202
                    $hydraPrefix.'view' => [
×
UNCOV
203
                        'type' => 'object',
×
UNCOV
204
                        'properties' => [
×
UNCOV
205
                            '@id' => [
×
UNCOV
206
                                'type' => 'string',
×
UNCOV
207
                                'format' => 'iri-reference',
×
UNCOV
208
                            ],
×
UNCOV
209
                            '@type' => [
×
UNCOV
210
                                'type' => 'string',
×
UNCOV
211
                            ],
×
UNCOV
212
                            $hydraPrefix.'first' => [
×
UNCOV
213
                                'type' => 'string',
×
UNCOV
214
                                'format' => 'iri-reference',
×
UNCOV
215
                            ],
×
UNCOV
216
                            $hydraPrefix.'last' => [
×
UNCOV
217
                                'type' => 'string',
×
UNCOV
218
                                'format' => 'iri-reference',
×
UNCOV
219
                            ],
×
UNCOV
220
                            $hydraPrefix.'previous' => [
×
UNCOV
221
                                'type' => 'string',
×
UNCOV
222
                                'format' => 'iri-reference',
×
UNCOV
223
                            ],
×
UNCOV
224
                            $hydraPrefix.'next' => [
×
UNCOV
225
                                'type' => 'string',
×
UNCOV
226
                                'format' => 'iri-reference',
×
UNCOV
227
                            ],
×
UNCOV
228
                        ],
×
UNCOV
229
                        'example' => [
×
UNCOV
230
                            '@id' => 'string',
×
UNCOV
231
                            'type' => 'string',
×
UNCOV
232
                            $hydraPrefix.'first' => 'string',
×
UNCOV
233
                            $hydraPrefix.'last' => 'string',
×
UNCOV
234
                            $hydraPrefix.'previous' => 'string',
×
UNCOV
235
                            $hydraPrefix.'next' => 'string',
×
UNCOV
236
                        ],
×
UNCOV
237
                    ],
×
UNCOV
238
                    $hydraPrefix.'search' => [
×
UNCOV
239
                        'type' => 'object',
×
UNCOV
240
                        'properties' => [
×
UNCOV
241
                            '@type' => ['type' => 'string'],
×
UNCOV
242
                            $hydraPrefix.'template' => ['type' => 'string'],
×
UNCOV
243
                            $hydraPrefix.'variableRepresentation' => ['type' => 'string'],
×
UNCOV
244
                            $hydraPrefix.'mapping' => [
×
UNCOV
245
                                'type' => 'array',
×
UNCOV
246
                                'items' => [
×
UNCOV
247
                                    'type' => 'object',
×
UNCOV
248
                                    'properties' => [
×
UNCOV
249
                                        '@type' => ['type' => 'string'],
×
UNCOV
250
                                        'variable' => ['type' => 'string'],
×
UNCOV
251
                                        'property' => $nullableStringDefinition,
×
UNCOV
252
                                        'required' => ['type' => 'boolean'],
×
UNCOV
253
                                    ],
×
UNCOV
254
                                ],
×
UNCOV
255
                            ],
×
UNCOV
256
                        ],
×
UNCOV
257
                    ],
×
UNCOV
258
                ],
×
UNCOV
259
            ];
×
260
        }
261

UNCOV
262
        $schema['type'] = 'object';
×
UNCOV
263
        $schema['description'] = "$definitionName collection.";
×
UNCOV
264
        $schema['allOf'] = [
×
UNCOV
265
            ['$ref' => $prefix.self::COLLECTION_BASE_SCHEMA_NAME],
×
UNCOV
266
            [
×
UNCOV
267
                'type' => 'object',
×
UNCOV
268
                'properties' => [
×
UNCOV
269
                    $hydraPrefix.'member' => [
×
UNCOV
270
                        'type' => 'array',
×
UNCOV
271
                        'items' => $schema['items'],
×
UNCOV
272
                    ],
×
UNCOV
273
                ],
×
UNCOV
274
            ],
×
UNCOV
275
        ];
×
276

UNCOV
277
        unset($schema['items']);
×
278

UNCOV
279
        return $schema;
×
280
    }
281

282
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
283
    {
UNCOV
284
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
UNCOV
285
            $this->schemaFactory->setSchemaFactory($schemaFactory);
×
286
        }
287
    }
288
}
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