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

api-platform / core / 15133993414

20 May 2025 09:30AM UTC coverage: 26.313% (-1.2%) from 27.493%
15133993414

Pull #7161

github

web-flow
Merge e2c03d45f into 5459ba375
Pull Request #7161: fix(metadata): infer parameter string type from schema

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

11019 existing lines in 363 files now uncovered.

12898 of 49018 relevant lines covered (26.31%)

34.33 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
    {
UNCOV
49
        if ($this->schemaFactory instanceof SchemaFactoryAwareInterface) {
828✔
UNCOV
50
            $this->schemaFactory->setSchemaFactory($this);
828✔
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
    {
UNCOV
59
        $schema = $this->schemaFactory->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
65✔
UNCOV
60
        if ('jsonhal' !== $format) {
65✔
UNCOV
61
            return $schema;
55✔
62
        }
63

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

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

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

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

UNCOV
133
            return $schema;
26✔
134
        }
135

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