• 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.22
/src/Hal/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\Hal\JsonSchema;
15

16
use ApiPlatform\JsonSchema\Schema;
17
use ApiPlatform\JsonSchema\SchemaFactoryAwareInterface;
18
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
19
use ApiPlatform\Metadata\Operation;
20

21
/**
22
 * Decorator factory which adds HAL properties to the JSON Schema document.
23
 *
24
 * @author Kévin Dunglas <dunglas@gmail.com>
25
 * @author Jachim Coudenys <jachimcoudenys@gmail.com>
26
 */
27
final class SchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
28
{
29
    private const HREF_PROP = [
30
        'href' => [
31
            'type' => 'string',
32
            'format' => 'iri-reference',
33
        ],
34
    ];
35
    private const BASE_PROPS = [
36
        '_links' => [
37
            'type' => 'object',
38
            'properties' => [
39
                'self' => [
40
                    'type' => 'object',
41
                    'properties' => self::HREF_PROP,
42
                ],
43
            ],
44
        ],
45
    ];
46

47
    public function __construct(private readonly SchemaFactoryInterface $schemaFactory)
48
    {
49
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
1,062✔
50
            $this->schemaFactory->setSchemaFactory($this);
1,062✔
51
        }
52
    }
53

54
    /**
55
     * {@inheritdoc}
56
     */
57
    public function buildSchema(string $className, string $format = 'jsonhal', string $type = Schema::TYPE_OUTPUT, ?Operation $operation = null, ?Schema $schema = null, ?array $serializerContext = null, bool $forceCollection = false): Schema
58
    {
59
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
118✔
60
        if ('jsonhal' !== $format) {
118✔
61
            return $schema;
98✔
62
        }
63

64
        $definitions = $schema->getDefinitions();
52✔
65
        if ($key = $schema->getRootDefinitionKey()) {
52✔
66
            $definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
46✔
67

68
            return $schema;
46✔
69
        }
70
        if ($key = $schema->getItemsDefinitionKey()) {
40✔
71
            $definitions[$key]['properties'] = self::BASE_PROPS + ($definitions[$key]['properties'] ?? []);
40✔
72
        }
73

74
        if (($schema['type'] ?? '') === 'array') {
40✔
75
            $items = $schema['items'];
40✔
76
            unset($schema['items']);
40✔
77

78
            $schema['type'] = 'object';
40✔
79
            $schema['properties'] = [
40✔
80
                '_embedded' => [
40✔
81
                    'anyOf' => [
40✔
82
                        [
40✔
83
                            'type' => 'object',
40✔
84
                            'properties' => [
40✔
85
                                'item' => [
40✔
86
                                    'type' => 'array',
40✔
87
                                    'items' => $items,
40✔
88
                                ],
40✔
89
                            ],
40✔
90
                        ],
40✔
91
                        ['type' => 'object'],
40✔
92
                    ],
40✔
93
                ],
40✔
94
                'totalItems' => [
40✔
95
                    'type' => 'integer',
40✔
96
                    'minimum' => 0,
40✔
97
                ],
40✔
98
                'itemsPerPage' => [
40✔
99
                    'type' => 'integer',
40✔
100
                    'minimum' => 0,
40✔
101
                ],
40✔
102
                '_links' => [
40✔
103
                    'type' => 'object',
40✔
104
                    'properties' => [
40✔
105
                        'self' => [
40✔
106
                            'type' => 'object',
40✔
107
                            'properties' => self::HREF_PROP,
40✔
108
                        ],
40✔
109
                        'first' => [
40✔
110
                            'type' => 'object',
40✔
111
                            'properties' => self::HREF_PROP,
40✔
112
                        ],
40✔
113
                        'last' => [
40✔
114
                            'type' => 'object',
40✔
115
                            'properties' => self::HREF_PROP,
40✔
116
                        ],
40✔
117
                        'next' => [
40✔
118
                            'type' => 'object',
40✔
119
                            'properties' => self::HREF_PROP,
40✔
120
                        ],
40✔
121
                        'previous' => [
40✔
122
                            'type' => 'object',
40✔
123
                            'properties' => self::HREF_PROP,
40✔
124
                        ],
40✔
125
                    ],
40✔
126
                ],
40✔
127
            ];
40✔
128
            $schema['required'] = [
40✔
129
                '_links',
40✔
130
                '_embedded',
40✔
131
            ];
40✔
132

133
            return $schema;
40✔
134
        }
135

UNCOV
136
        return $schema;
12✔
137
    }
138

139
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
140
    {
141
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
×
142
            $this->schemaFactory->setSchemaFactory($schemaFactory);
×
143
        }
144
    }
145
}
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