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

api-platform / core / 10490215375

21 Aug 2024 12:48PM UTC coverage: 7.704%. Remained the same
10490215375

push

github

web-flow
docs: cleanup and minor marketing improvements (#6525)

* fix: ignore more files in the Laravel package archive

* chore: various minor docs improvements

12476 of 161932 relevant lines covered (7.7%)

23.0 hits per line

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

96.91
/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) {
1,847✔
63
            $this->schemaFactory->setSchemaFactory($this);
1,847✔
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);
36✔
73
        if ('jsonld' !== $format) {
36✔
74
            return $schema;
36✔
75
        }
76

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

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

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

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

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

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

179
            return $schema;
36✔
180
        }
181

182
        return $schema;
36✔
183
    }
184

185
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
186
    {
187
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
1,847✔
188
            $this->schemaFactory->setSchemaFactory($schemaFactory);
1,847✔
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