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

api-platform / core / 7142557150

08 Dec 2023 02:28PM UTC coverage: 36.003% (-1.4%) from 37.36%
7142557150

push

github

web-flow
fix(jsonld): remove link to ApiDocumentation when doc is disabled (#6029)

0 of 1 new or added line in 1 file covered. (0.0%)

2297 existing lines in 182 files now uncovered.

9992 of 27753 relevant lines covered (36.0%)

147.09 hits per line

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

96.88
/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\SchemaFactory as BaseSchemaFactory;
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
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
        $this->addDistinctFormat('jsonld');
2,052✔
63
    }
64

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

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

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

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

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

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

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

177
            return $schema;
36✔
178
        }
179

180
        return $schema;
36✔
181
    }
182

183
    public function addDistinctFormat(string $format): void
184
    {
185
        if ($this->schemaFactory instanceof BaseSchemaFactory) {
2,052✔
186
            $this->schemaFactory->addDistinctFormat($format);
2,052✔
187
        }
188
    }
189
}
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