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

api-platform / core / 14854964964

06 May 2025 08:21AM UTC coverage: 6.873% (-1.6%) from 8.459%
14854964964

Pull #7122

github

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

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

2031 existing lines in 148 files now uncovered.

10887 of 158407 relevant lines covered (6.87%)

6.19 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) {
493✔
65
            $this->schemaFactory->setSchemaFactory($this);
493✔
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);
106✔
75
        if ('jsonld' !== $format) {
106✔
76
            return $schema;
58✔
77
        }
78

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

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

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

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

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

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

182
            return $schema;
32✔
183
        }
184

UNCOV
185
        return $schema;
×
186
    }
187

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