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

api-platform / core / 10027319583

21 Jul 2024 09:36AM UTC coverage: 7.847%. Remained the same
10027319583

push

github

web-flow
ci: fix naming of Windows Behat matrix line (#6489)

12688 of 161690 relevant lines covered (7.85%)

26.88 hits per line

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

97.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\JsonSchema\Schema;
18
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
19
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
20
use ApiPlatform\Metadata\Operation;
21

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

60
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory)
61
    {
62
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
2,246✔
63
            $this->schemaFactory->setSchemaFactory($this);
2,246✔
64
        }
65
    }
66

67
    /**
68
     * {@inheritdoc}
69
     */
70
    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
71
    {
72
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
174✔
73
        if ('jsonld' !== $format) {
174✔
74
            return $schema;
114✔
75
        }
76

77
        if ('input' === $type) {
117✔
78
            return $schema;
66✔
79
        }
80

81
        $definitions = $schema->getDefinitions();
105✔
82
        if ($key = $schema->getRootDefinitionKey()) {
105✔
83
            $definitions[$key]['properties'] = self::BASE_ROOT_PROPS + ($definitions[$key]['properties'] ?? []);
102✔
84

85
            return $schema;
102✔
86
        }
87
        if ($key = $schema->getItemsDefinitionKey()) {
72✔
88
            $definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
72✔
89
        }
90

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

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

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

179
            return $schema;
72✔
180
        }
181

182
        return $schema;
36✔
183
    }
184

185
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
186
    {
187
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
2,246✔
188
            $this->schemaFactory->setSchemaFactory($schemaFactory);
2,246✔
189
        }
190
    }
191
}
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