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

api-platform / core / 14246153067

03 Apr 2025 02:56PM UTC coverage: 7.286% (-0.002%) from 7.288%
14246153067

push

github

web-flow
Merge commit from fork

12 of 160 new or added lines in 7 files covered. (7.5%)

2343 existing lines in 152 files now uncovered.

12450 of 170870 relevant lines covered (7.29%)

12.06 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
    {
37
        parent::__construct(self::VERSION_JSON_SCHEMA === $this->version ? ['$schema' => 'http://json-schema.org/draft-07/schema#'] : []);
162✔
38
    }
39

40
    /**
41
     * The flavor used for this document: JSON Schema, OpenAPI v2 or OpenAPI v3.
42
     */
43
    public function getVersion(): string
44
    {
45
        return $this->version;
162✔
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
    {
55
        $schema = parent::getArrayCopy();
90✔
56

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

61
        return $schema;
90✔
62
    }
63

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

72
        return $definitions;
162✔
73
    }
74

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

83
            return;
33✔
84
        }
85

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

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

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

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

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

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

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

128
        return substr($definitionKey, $prefix);
150✔
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

© 2026 Coveralls, Inc