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

api-platform / core / 14954769666

11 May 2025 10:14AM UTC coverage: 0.0% (-8.5%) from 8.457%
14954769666

Pull #7135

github

web-flow
Merge bf21e0bc7 into 4dd0cdfc4
Pull Request #7135: fix(symfony,laravel): InvalidUriVariableException status code (e400)

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

11040 existing lines in 370 files now uncovered.

0 of 48303 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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) {
×
UNCOV
65
            $this->schemaFactory->setSchemaFactory($this);
×
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);
×
UNCOV
75
        if ('jsonld' !== $format) {
×
UNCOV
76
            return $schema;
×
77
        }
78

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

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

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

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

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

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

UNCOV
182
            return $schema;
×
183
        }
184

185
        return $schema;
×
186
    }
187

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

© 2025 Coveralls, Inc