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

api-platform / core / 15040977736

15 May 2025 09:02AM UTC coverage: 21.754% (+13.3%) from 8.423%
15040977736

Pull #6960

github

web-flow
Merge 7a7a13526 into 1862d03b7
Pull Request #6960: feat(json-schema): mutualize json schema between formats

320 of 460 new or added lines in 24 files covered. (69.57%)

1863 existing lines in 109 files now uncovered.

11069 of 50882 relevant lines covered (21.75%)

29.49 hits per line

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

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

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

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

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

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

102
            return $schema;
15✔
103
        }
104

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

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

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

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

121
            return $schema;
15✔
122
        }
123

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

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

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

199
            return $schema;
15✔
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