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

api-platform / core / 13718525764

07 Mar 2025 10:15AM UTC coverage: 7.289%. Remained the same
13718525764

push

github

web-flow
fix(doctrine): Add a proper exception when a doctrine manager could not be found for a resource class (#6995)

* fix(doctrine): Add a proper exception when a doctrine manager could not be found for a resource class

* Update ItemProvider.php

---------

Co-authored-by: Antoine Bluchet <soyuka@users.noreply.github.com>

1 of 2 new or added lines in 1 file covered. (50.0%)

2387 existing lines in 157 files now uncovered.

12437 of 170624 relevant lines covered (7.29%)

11.99 hits per line

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

97.96
/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) {
1,014✔
65
            $this->schemaFactory->setSchemaFactory($this);
1,014✔
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);
162✔
75
        if ('jsonld' !== $format) {
162✔
76
            return $schema;
90✔
77
        }
78

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

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

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

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

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

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

182
            return $schema;
48✔
183
        }
184

UNCOV
185
        return $schema;
12✔
186
    }
187

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