• 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

100.0
/src/JsonSchema/Schema.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\JsonSchema;
15

16
/**
17
 * Represents a JSON Schema document.
18
 *
19
 * Both the standard version and the OpenAPI flavors (v2 and v3) are supported.
20
 *
21
 * @see https://json-schema.org/latest/json-schema-core.html
22
 * @see https://github.com/OAI/OpenAPI-Specification
23
 *
24
 * @author Kévin Dunglas <dunglas@gmail.com>
25
 */
26
final class Schema extends \ArrayObject
27
{
28
    public const TYPE_INPUT = 'input';
29
    public const TYPE_OUTPUT = 'output';
30
    public const VERSION_JSON_SCHEMA = 'json-schema';
31
    public const VERSION_OPENAPI = 'openapi';
32
    public const VERSION_SWAGGER = 'swagger';
33
    public const UNKNOWN_TYPE = 'unknown_type';
34

35
    public function __construct(private readonly string $version = self::VERSION_JSON_SCHEMA)
36
    {
UNCOV
37
        parent::__construct(self::VERSION_JSON_SCHEMA === $this->version ? ['$schema' => 'http://json-schema.org/draft-07/schema#'] : []);
65✔
38
    }
39

40
    /**
41
     * The flavor used for this document: JSON Schema, OpenAPI v2 or OpenAPI v3.
42
     */
43
    public function getVersion(): string
44
    {
UNCOV
45
        return $this->version;
65✔
46
    }
47

48
    /**
49
     * {@inheritdoc}
50
     *
51
     * @param bool $includeDefinitions if set to false, definitions will not be included in the resulting array
52
     */
53
    public function getArrayCopy(bool $includeDefinitions = true): array
54
    {
UNCOV
55
        $schema = parent::getArrayCopy();
41✔
56

UNCOV
57
        if (!$includeDefinitions) {
41✔
UNCOV
58
            unset($schema['definitions'], $schema['components']);
22✔
59
        }
60

UNCOV
61
        return $schema;
41✔
62
    }
63

64
    /**
65
     * Retrieves the definitions used by this schema.
66
     */
67
    public function getDefinitions(): \ArrayObject
68
    {
UNCOV
69
        $definitions = $this['definitions'] ?? $this['components']['schemas'] ?? new \ArrayObject();
65✔
UNCOV
70
        $this->setDefinitions($definitions);
65✔
71

UNCOV
72
        return $definitions;
65✔
73
    }
74

75
    /**
76
     * Associates existing definitions to this schema.
77
     */
78
    public function setDefinitions(\ArrayObject $definitions): void
79
    {
UNCOV
80
        if (self::VERSION_OPENAPI === $this->version) {
65✔
UNCOV
81
            $this['components']['schemas'] = $definitions;
22✔
82

UNCOV
83
            return;
22✔
84
        }
85

UNCOV
86
        $this['definitions'] = $definitions;
43✔
87
    }
88

89
    /**
90
     * Returns the name of the root definition, if defined.
91
     */
92
    public function getRootDefinitionKey(): ?string
93
    {
UNCOV
94
        if (!isset($this['$ref'])) {
61✔
UNCOV
95
            return null;
36✔
96
        }
97

UNCOV
98
        return $this->removeDefinitionKeyPrefix($this['$ref']);
55✔
99
    }
100

101
    /**
102
     * Returns the name of the items definition, if defined.
103
     */
104
    public function getItemsDefinitionKey(): ?string
105
    {
UNCOV
106
        $ref = $this['items']['$ref'] ?? null;
36✔
UNCOV
107
        if (null === $ref) {
36✔
108
            return null;
12✔
109
        }
110

UNCOV
111
        return $this->removeDefinitionKeyPrefix($ref);
36✔
112
    }
113

114
    /**
115
     * Checks if this schema is initialized.
116
     */
117
    public function isDefined(): bool
118
    {
UNCOV
119
        return isset($this['$ref']) || isset($this['type']);
22✔
120
    }
121

122
    private function removeDefinitionKeyPrefix(string $definitionKey): string
123
    {
124
        // strlen('#/definitions/') = 14
125
        // strlen('#/components/schemas/') = 21
UNCOV
126
        $prefix = self::VERSION_OPENAPI === $this->version ? 21 : 14;
61✔
127

UNCOV
128
        return substr($definitionKey, $prefix);
61✔
129
    }
130
}
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