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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

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

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 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,062✔
65
            $this->schemaFactory->setSchemaFactory($this);
1,062✔
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);
118✔
75
        if ('jsonld' !== $format) {
118✔
76
            return $schema;
70✔
77
        }
78

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

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

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

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

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

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

182
            return $schema;
44✔
183
        }
184

UNCOV
185
        return $schema;
12✔
186
    }
187

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