• 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

96.94
/src/Hydra/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\Hydra\JsonSchema;
15

16
use ApiPlatform\JsonLd\ContextBuilder;
17
use ApiPlatform\JsonLd\Serializer\HydraPrefixTrait;
18
use ApiPlatform\JsonSchema\Schema;
19
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
20
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
21
use ApiPlatform\Metadata\Operation;
22

23
/**
24
 * Decorator factory which adds Hydra properties to the JSON Schema document.
25
 *
26
 * @author Kévin Dunglas <dunglas@gmail.com>
27
 */
28
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
29
{
30
    use HydraPrefixTrait;
31
    private const BASE_PROP = [
32
        'readOnly' => true,
33
        'type' => 'string',
34
    ];
35
    private const BASE_PROPS = [
36
        '@id' => self::BASE_PROP,
37
        '@type' => self::BASE_PROP,
38
    ];
39
    private const BASE_ROOT_PROPS = [
40
        '@context' => [
41
            'readOnly' => true,
42
            'oneOf' => [
43
                ['type' => 'string'],
44
                [
45
                    'type' => 'object',
46
                    'properties' => [
47
                        '@vocab' => [
48
                            'type' => 'string',
49
                        ],
50
                        'hydra' => [
51
                            'type' => 'string',
52
                            'enum' => [ContextBuilder::HYDRA_NS],
53
                        ],
54
                    ],
55
                    'required' => ['@vocab', 'hydra'],
56
                    'additionalProperties' => true,
57
                ],
58
            ],
59
        ],
60
    ] + self::BASE_PROPS;
61

62
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory, private readonly array $defaultContext = [])
63
    {
64
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
240✔
65
            $this->schemaFactory->setSchemaFactory($this);
240✔
66
        }
67
    }
68

69
    /**
70
     * {@inheritdoc}
71
     */
72
    public function buildSchema(string $className, string $format = 'jsonld', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
73
    {
74
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
53✔
75
        if ('jsonld' !== $format) {
53✔
76
            return $schema;
29✔
77
        }
78

79
        if ('input' === $type) {
34✔
80
            return $schema;
12✔
81
        }
82

83
        $definitions = $schema->getDefinitions();
30✔
84
        if ($key = $schema->getRootDefinitionKey()) {
30✔
85
            $definitions[$key]['properties'] = self::BASE_ROOT_PROPS + ($definitions[$key]['properties'] ?? []);
27✔
86

87
            return $schema;
27✔
88
        }
89
        if ($key = $schema->getItemsDefinitionKey()) {
16✔
90
            $definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
16✔
91
        }
92

93
        if (($schema['type'] ?? '') === 'array') {
16✔
94
            // hydra:collection
95
            $items = $schema['items'];
16✔
96
            unset($schema['items']);
16✔
97

98
            switch ($schema->getVersion()) {
16✔
99
                // JSON Schema + OpenAPI 3.1
100
                case Schema::VERSION_OPENAPI:
16✔
101
                case Schema::VERSION_JSON_SCHEMA:
6✔
102
                    $nullableStringDefinition = ['type' => ['string', 'null']];
16✔
103
                    break;
16✔
104
                    // Swagger
105
                default:
106
                    $nullableStringDefinition = ['type' => 'string'];
×
107
                    break;
×
108
            }
109

110
            $hydraPrefix = $this->getHydraPrefix(($serializerContext ?? []) + $this->defaultContext);
16✔
111
            $schema['type'] = 'object';
16✔
112
            $schema['properties'] = [
16✔
113
                $hydraPrefix.'member' => [
16✔
114
                    'type' => 'array',
16✔
115
                    'items' => $items,
16✔
116
                ],
16✔
117
                $hydraPrefix.'totalItems' => [
16✔
118
                    'type' => 'integer',
16✔
119
                    'minimum' => 0,
16✔
120
                ],
16✔
121
                $hydraPrefix.'view' => [
16✔
122
                    'type' => 'object',
16✔
123
                    'properties' => [
16✔
124
                        '@id' => [
16✔
125
                            'type' => 'string',
16✔
126
                            'format' => 'iri-reference',
16✔
127
                        ],
16✔
128
                        '@type' => [
16✔
129
                            'type' => 'string',
16✔
130
                        ],
16✔
131
                        $hydraPrefix.'first' => [
16✔
132
                            'type' => 'string',
16✔
133
                            'format' => 'iri-reference',
16✔
134
                        ],
16✔
135
                        $hydraPrefix.'last' => [
16✔
136
                            'type' => 'string',
16✔
137
                            'format' => 'iri-reference',
16✔
138
                        ],
16✔
139
                        $hydraPrefix.'previous' => [
16✔
140
                            'type' => 'string',
16✔
141
                            'format' => 'iri-reference',
16✔
142
                        ],
16✔
143
                        $hydraPrefix.'next' => [
16✔
144
                            'type' => 'string',
16✔
145
                            'format' => 'iri-reference',
16✔
146
                        ],
16✔
147
                    ],
16✔
148
                    'example' => [
16✔
149
                        '@id' => 'string',
16✔
150
                        'type' => 'string',
16✔
151
                        $hydraPrefix.'first' => 'string',
16✔
152
                        $hydraPrefix.'last' => 'string',
16✔
153
                        $hydraPrefix.'previous' => 'string',
16✔
154
                        $hydraPrefix.'next' => 'string',
16✔
155
                    ],
16✔
156
                ],
16✔
157
                $hydraPrefix.'search' => [
16✔
158
                    'type' => 'object',
16✔
159
                    'properties' => [
16✔
160
                        '@type' => ['type' => 'string'],
16✔
161
                        $hydraPrefix.'template' => ['type' => 'string'],
16✔
162
                        $hydraPrefix.'variableRepresentation' => ['type' => 'string'],
16✔
163
                        $hydraPrefix.'mapping' => [
16✔
164
                            'type' => 'array',
16✔
165
                            'items' => [
16✔
166
                                'type' => 'object',
16✔
167
                                'properties' => [
16✔
168
                                    '@type' => ['type' => 'string'],
16✔
169
                                    'variable' => ['type' => 'string'],
16✔
170
                                    'property' => $nullableStringDefinition,
16✔
171
                                    'required' => ['type' => 'boolean'],
16✔
172
                                ],
16✔
173
                            ],
16✔
174
                        ],
16✔
175
                    ],
16✔
176
                ],
16✔
177
            ];
16✔
178
            $schema['required'] = [
16✔
179
                $hydraPrefix.'member',
16✔
180
            ];
16✔
181

182
            return $schema;
16✔
183
        }
184

UNCOV
185
        return $schema;
×
186
    }
187

188
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
189
    {
190
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
240✔
191
            $this->schemaFactory->setSchemaFactory($schemaFactory);
240✔
192
        }
193
    }
194
}
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