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

api-platform / core / 13814792797

12 Mar 2025 03:09PM UTC coverage: 5.889% (-1.4%) from 7.289%
13814792797

Pull #7012

github

web-flow
Merge 199d44919 into 284937039
Pull Request #7012: doc: comment typo in ApiResource.php

10048 of 170615 relevant lines covered (5.89%)

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

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

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

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

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

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

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

182
            return $schema;
36✔
183
        }
184

185
        return $schema;
×
186
    }
187

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