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

api-platform / core / 19799301771

30 Nov 2025 01:04PM UTC coverage: 25.229% (-0.03%) from 25.257%
19799301771

push

github

web-flow
fix(metadata): repeatable attribute mutators (#7542)

14557 of 57700 relevant lines covered (25.23%)

28.11 hits per line

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

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

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

88
        if (null === $inputOrOutputClass) {
66✔
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);
66✔
94
        $definitions = $schema->getDefinitions();
66✔
95
        $definitionName = $this->definitionNameFactory->create($className, $format, $className, $operation, $serializerContext);
66✔
96
        $prefix = $this->getSchemaUriPrefix($schema->getVersion());
66✔
97
        $collectionKey = $schema->getItemsDefinitionKey();
66✔
98

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

103
            return $schema;
46✔
104
        }
105

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

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

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

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

122
            return $schema;
56✔
123
        }
124

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

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

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

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

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