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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

93.69
/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

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

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

68
    /**
69
     * {@inheritdoc}
70
     */
71
    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
72
    {
73
        if ('jsonhal' !== $format) {
124✔
74
            return $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
110✔
75
        }
76

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

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

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

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

102
            return $schema;
20✔
103
        }
104

105
        $key = $schema->getRootDefinitionKey() ?? $collectionKey;
40✔
106

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

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

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

121
            return $schema;
32✔
122
        }
123

124
        if (($schema['type'] ?? '') === 'array') {
30✔
125
            if (!isset($definitions[self::COLLECTION_BASE_SCHEMA_NAME])) {
30✔
126
                $definitions[self::COLLECTION_BASE_SCHEMA_NAME] = [
30✔
127
                    'type' => 'object',
30✔
128
                    'properties' => [
30✔
129
                        '_embedded' => [
30✔
130
                            'anyOf' => [
30✔
131
                                [
30✔
132
                                    'type' => 'object',
30✔
133
                                    'properties' => [
30✔
134
                                        'item' => [
30✔
135
                                            'type' => 'array',
30✔
136
                                        ],
30✔
137
                                    ],
30✔
138
                                ],
30✔
139
                                ['type' => 'object'],
30✔
140
                            ],
30✔
141
                        ],
30✔
142
                        'totalItems' => [
30✔
143
                            'type' => 'integer',
30✔
144
                            'minimum' => 0,
30✔
145
                        ],
30✔
146
                        'itemsPerPage' => [
30✔
147
                            'type' => 'integer',
30✔
148
                            'minimum' => 0,
30✔
149
                        ],
30✔
150
                        '_links' => [
30✔
151
                            'type' => 'object',
30✔
152
                            'properties' => [
30✔
153
                                'self' => [
30✔
154
                                    'type' => 'object',
30✔
155
                                    'properties' => self::HREF_PROP,
30✔
156
                                ],
30✔
157
                                'first' => [
30✔
158
                                    'type' => 'object',
30✔
159
                                    'properties' => self::HREF_PROP,
30✔
160
                                ],
30✔
161
                                'last' => [
30✔
162
                                    'type' => 'object',
30✔
163
                                    'properties' => self::HREF_PROP,
30✔
164
                                ],
30✔
165
                                'next' => [
30✔
166
                                    'type' => 'object',
30✔
167
                                    'properties' => self::HREF_PROP,
30✔
168
                                ],
30✔
169
                                'previous' => [
30✔
170
                                    'type' => 'object',
30✔
171
                                    'properties' => self::HREF_PROP,
30✔
172
                                ],
30✔
173
                            ],
30✔
174
                        ],
30✔
175
                    ],
30✔
176
                    'required' => ['_links', '_embedded'],
30✔
177
                ];
30✔
178
            }
179

180
            unset($schema['items']);
30✔
181
            unset($schema['type']);
30✔
182

183
            $schema['description'] = "$definitionName collection.";
30✔
184
            $schema['allOf'] = [
30✔
185
                ['$ref' => $prefix.self::COLLECTION_BASE_SCHEMA_NAME],
30✔
186
                [
30✔
187
                    'type' => 'object',
30✔
188
                    'properties' => [
30✔
189
                        '_embedded' => [
30✔
190
                            'additionalProperties' => [
30✔
191
                                'type' => 'array',
30✔
192
                                'items' => ['$ref' => $prefix.$definitionName],
30✔
193
                            ],
30✔
194
                        ],
30✔
195
                    ],
30✔
196
                ],
30✔
197
            ];
30✔
198

199
            return $schema;
30✔
200
        }
201

202
        return $schema;
×
203
    }
204

205
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
206
    {
207
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
208
            $this->schemaFactory->setSchemaFactory($schemaFactory);
×
209
        }
210
    }
211
}
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