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

api-platform / core / 19337721455

13 Nov 2025 04:02PM UTC coverage: 0.0% (-24.6%) from 24.631%
19337721455

push

github

soyuka
Merge 4.1

0 of 56854 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/Hal/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\Hal\JsonSchema;
15

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

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

37
    private const COLLECTION_BASE_SCHEMA_NAME = 'HalCollectionBaseSchema';
38
    private const COLLECTION_BASE_SCHEMA_NAME_NO_PAGINATION = 'HalCollectionBaseSchemaNoPagination';
39

40
    private const HREF_PROP = [
41
        'href' => [
42
            'type' => 'string',
43
            'format' => 'iri-reference',
44
        ],
45
    ];
46
    private const BASE_PROPS = [
47
        '_links' => [
48
            'type' => 'object',
49
            'properties' => [
50
                'self' => [
51
                    'type' => 'object',
52
                    'properties' => self::HREF_PROP,
53
                ],
54
            ],
55
        ],
56
    ];
57

58
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory, private ?DefinitionNameFactoryInterface $definitionNameFactory = null, ?ResourceMetadataCollectionFactoryInterface $resourceMetadataFactory = null)
59
    {
60
        if (!$definitionNameFactory) {
×
61
            $this->definitionNameFactory = new DefinitionNameFactory();
×
62
        }
63
        $this->resourceMetadataFactory = $resourceMetadataFactory;
×
64
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
65
            $this->schemaFactory->setSchemaFactory($this);
×
66
        }
67
    }
68

69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function buildSchema(string $className, string $format = 'jsonhal', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
73
    {
74
        if ('jsonhal' !== $format) {
×
75
            return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
×
76
        }
77

78
        if (!$this->isResourceClass($className)) {
×
79
            $operation = null;
×
80
            $inputOrOutputClass = null;
×
81
            $serializerContext ??= [];
×
82
        } else {
83
            $operation = $this->findOperation($className, $type, $operation, $serializerContext, $format);
×
84
            $inputOrOutputClass = $this->findOutputClass($className, $type, $operation, $serializerContext);
×
85
            $serializerContext ??= $this->getSerializerContext($operation, $type);
×
86
        }
87

88
        if (null === $inputOrOutputClass) {
×
89
            // input or output disabled
90
            return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
×
91
        }
92

93
        $schema = $this->schemaFactory->buildSchema($className, 'json', $type, $operation, $schema, $serializerContext, $forceCollection);
×
94
        $definitions = $schema->getDefinitions();
×
95
        $definitionName = $this->definitionNameFactory->create($className, $format, $className, $operation, $serializerContext);
×
96
        $prefix = $this->getSchemaUriPrefix($schema->getVersion());
×
97
        $collectionKey = $schema->getItemsDefinitionKey();
×
98

99
        // Already computed
100
        if (!$collectionKey && isset($definitions[$definitionName])) {
×
101
            $schema['$ref'] = $prefix.$definitionName;
×
102

103
            return $schema;
×
104
        }
105

106
        $key = $schema->getRootDefinitionKey() ?? $collectionKey;
×
107

108
        $definitions[$definitionName] = [
×
109
            'allOf' => [
×
110
                ['type' => 'object', 'properties' => self::BASE_PROPS],
×
111
                ['$ref' => $prefix.$key],
×
112
            ],
×
113
        ];
×
114

115
        if (isset($definitions[$key]['description'])) {
×
116
            $definitions[$definitionName]['description'] = $definitions[$key]['description'];
×
117
        }
118

119
        if (!$collectionKey) {
×
120
            $schema['$ref'] = $prefix.$definitionName;
×
121

122
            return $schema;
×
123
        }
124

125
        if (($schema['type'] ?? '') === 'array') {
×
126
            if (!isset($definitions[self::COLLECTION_BASE_SCHEMA_NAME_NO_PAGINATION])) {
×
127
                $definitions[self::COLLECTION_BASE_SCHEMA_NAME_NO_PAGINATION] = [
×
128
                    'type' => 'object',
×
129
                    'properties' => [
×
130
                        'totalItems' => [
×
131
                            'type' => 'integer',
×
132
                            'minimum' => 0,
×
133
                        ],
×
134
                        '_embedded' => [
×
135
                            'anyOf' => [
×
136
                                [
×
137
                                    'type' => 'object',
×
138
                                    'properties' => [
×
139
                                        'item' => [
×
140
                                            'type' => 'array',
×
141
                                        ],
×
142
                                    ],
×
143
                                ],
×
144
                                ['type' => 'object'],
×
145
                            ],
×
146
                        ],
×
147
                        '_links' => [
×
148
                            'type' => 'object',
×
149
                            'properties' => [
×
150
                                'self' => [
×
151
                                    'type' => 'object',
×
152
                                    'properties' => self::HREF_PROP,
×
153
                                ],
×
154
                            ],
×
155
                        ],
×
156
                    ],
×
157
                    'required' => ['_links', '_embedded'],
×
158
                ];
×
159

160
                $definitions[self::COLLECTION_BASE_SCHEMA_NAME] = [
×
161
                    'allOf' => [
×
162
                        ['$ref' => $prefix.self::COLLECTION_BASE_SCHEMA_NAME_NO_PAGINATION],
×
163
                        [
×
164
                            'type' => 'object',
×
165
                            'properties' => [
×
166
                                'itemsPerPage' => [
×
167
                                    'type' => 'integer',
×
168
                                    'minimum' => 0,
×
169
                                ],
×
170
                                '_links' => [
×
171
                                    'properties' => [
×
172
                                        'first' => [
×
173
                                            'type' => 'object',
×
174
                                            'properties' => self::HREF_PROP,
×
175
                                        ],
×
176
                                        'last' => [
×
177
                                            'type' => 'object',
×
178
                                            'properties' => self::HREF_PROP,
×
179
                                        ],
×
180
                                        'next' => [
×
181
                                            'type' => 'object',
×
182
                                            'properties' => self::HREF_PROP,
×
183
                                        ],
×
184
                                        'previous' => [
×
185
                                            'type' => 'object',
×
186
                                            'properties' => self::HREF_PROP,
×
187
                                        ],
×
188
                                    ],
×
189
                                ],
×
190
                            ],
×
191
                        ],
×
192
                    ],
×
193
                ];
×
194
            }
195

196
            unset($schema['items']);
×
197
            unset($schema['type']);
×
198

199
            $schema['description'] = "$definitionName collection.";
×
200
            $schema['allOf'] = [
×
201
                ['$ref' => $prefix.(false === $operation->getPaginationEnabled() ? self::COLLECTION_BASE_SCHEMA_NAME_NO_PAGINATION : self::COLLECTION_BASE_SCHEMA_NAME)],
×
202
                [
×
203
                    'type' => 'object',
×
204
                    'properties' => [
×
205
                        '_embedded' => [
×
206
                            'additionalProperties' => [
×
207
                                'type' => 'array',
×
208
                                'items' => ['$ref' => $prefix.$definitionName],
×
209
                            ],
×
210
                        ],
×
211
                    ],
×
212
                ],
×
213
            ];
×
214

215
            return $schema;
×
216
        }
217

218
        return $schema;
×
219
    }
220

221
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
222
    {
223
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
224
            $this->schemaFactory->setSchemaFactory($schemaFactory);
×
225
        }
226
    }
227
}
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