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

api-platform / core / 14838030052

05 May 2025 01:48PM UTC coverage: 7.178% (-1.3%) from 8.459%
14838030052

Pull #7122

github

web-flow
Merge ecae612bf into f55606b01
Pull Request #7122: Cherry picks from main (deprecations)

0 of 35 new or added lines in 9 files covered. (0.0%)

2157 existing lines in 159 files now uncovered.

10774 of 150101 relevant lines covered (7.18%)

3.23 hits per line

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

95.83
/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\Schema;
17
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
18
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
19
use ApiPlatform\Metadata\Operation;
20

21
/**
22
 * Decorator factory which adds HAL properties to the JSON Schema document.
23
 *
24
 * @author Kévin Dunglas <dunglas@gmail.com>
25
 * @author Jachim Coudenys <jachimcoudenys@gmail.com>
26
 */
27
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
28
{
29
    private const HREF_PROP = [
30
        'href' => [
31
            'type' => 'string',
32
            'format' => 'iri-reference',
33
        ],
34
    ];
35
    private const BASE_PROPS = [
36
        '_links' => [
37
            'type' => 'object',
38
            'properties' => [
39
                'self' => [
40
                    'type' => 'object',
41
                    'properties' => self::HREF_PROP,
42
                ],
43
            ],
44
        ],
45
    ];
46

47
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory)
48
    {
49
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
240✔
50
            $this->schemaFactory->setSchemaFactory($this);
240✔
51
        }
52
    }
53

54
    /**
55
     * {@inheritdoc}
56
     */
57
    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
58
    {
59
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
53✔
60
        if ('jsonhal' !== $format) {
53✔
61
            return $schema;
43✔
62
        }
63

64
        $definitions = $schema->getDefinitions();
20✔
65
        if ($key = $schema->getRootDefinitionKey()) {
20✔
66
            $definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
17✔
67

68
            return $schema;
17✔
69
        }
70
        if ($key = $schema->getItemsDefinitionKey()) {
14✔
71
            $definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
14✔
72
        }
73

74
        if (($schema['type'] ?? '') === 'array') {
14✔
75
            $items = $schema['items'];
14✔
76
            unset($schema['items']);
14✔
77

78
            $schema['type'] = 'object';
14✔
79
            $schema['properties'] = [
14✔
80
                '_embedded' => [
14✔
81
                    'anyOf' => [
14✔
82
                        [
14✔
83
                            'type' => 'object',
14✔
84
                            'properties' => [
14✔
85
                                'item' => [
14✔
86
                                    'type' => 'array',
14✔
87
                                    'items' => $items,
14✔
88
                                ],
14✔
89
                            ],
14✔
90
                        ],
14✔
91
                        ['type' => 'object'],
14✔
92
                    ],
14✔
93
                ],
14✔
94
                'totalItems' => [
14✔
95
                    'type' => 'integer',
14✔
96
                    'minimum' => 0,
14✔
97
                ],
14✔
98
                'itemsPerPage' => [
14✔
99
                    'type' => 'integer',
14✔
100
                    'minimum' => 0,
14✔
101
                ],
14✔
102
                '_links' => [
14✔
103
                    'type' => 'object',
14✔
104
                    'properties' => [
14✔
105
                        'self' => [
14✔
106
                            'type' => 'object',
14✔
107
                            'properties' => self::HREF_PROP,
14✔
108
                        ],
14✔
109
                        'first' => [
14✔
110
                            'type' => 'object',
14✔
111
                            'properties' => self::HREF_PROP,
14✔
112
                        ],
14✔
113
                        'last' => [
14✔
114
                            'type' => 'object',
14✔
115
                            'properties' => self::HREF_PROP,
14✔
116
                        ],
14✔
117
                        'next' => [
14✔
118
                            'type' => 'object',
14✔
119
                            'properties' => self::HREF_PROP,
14✔
120
                        ],
14✔
121
                        'previous' => [
14✔
122
                            'type' => 'object',
14✔
123
                            'properties' => self::HREF_PROP,
14✔
124
                        ],
14✔
125
                    ],
14✔
126
                ],
14✔
127
            ];
14✔
128
            $schema['required'] = [
14✔
129
                '_links',
14✔
130
                '_embedded',
14✔
131
            ];
14✔
132

133
            return $schema;
14✔
134
        }
135

UNCOV
136
        return $schema;
×
137
    }
138

139
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
140
    {
141
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
142
            $this->schemaFactory->setSchemaFactory($schemaFactory);
×
143
        }
144
    }
145
}
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