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

api-platform / core / 20894952430

11 Jan 2026 12:14PM UTC coverage: 20.145% (-8.7%) from 28.875%
20894952430

Pull #7667

github

VincentLanglet
Add failing test
Pull Request #7667: POC NameConverterAwareInterface

11577 of 57467 relevant lines covered (20.15%)

27.48 hits per line

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

96.8
/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) {
692✔
61
            $this->definitionNameFactory = new DefinitionNameFactory();
×
62
        }
63
        $this->resourceMetadataFactory = $resourceMetadataFactory;
692✔
64
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
692✔
65
            $this->schemaFactory->setSchemaFactory($this);
692✔
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) {
2✔
75
            return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
2✔
76
        }
77

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

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

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

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

103
            return $schema;
2✔
104
        }
105

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

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

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

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

122
            return $schema;
2✔
123
        }
124

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

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

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

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

215
            return $schema;
2✔
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