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

api-platform / core / 10737347263

06 Sep 2024 11:11AM UTC coverage: 7.645% (-0.01%) from 7.655%
10737347263

push

github

web-flow
Merge pull request #6591 from soyuka/merge-34

Merge 3.4

10 of 526 new or added lines in 17 files covered. (1.9%)

9692 existing lines in 300 files now uncovered.

12523 of 163810 relevant lines covered (7.64%)

22.82 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
    {
UNCOV
64
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
1,850✔
UNCOV
65
            $this->schemaFactory->setSchemaFactory($this);
1,850✔
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
    {
UNCOV
74
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
36✔
UNCOV
75
        if ('jsonld' !== $format) {
36✔
UNCOV
76
            return $schema;
36✔
77
        }
78

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

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

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

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

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

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

UNCOV
182
            return $schema;
36✔
183
        }
184

UNCOV
185
        return $schema;
36✔
186
    }
187

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