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

api-platform / core / 15040977736

15 May 2025 09:02AM UTC coverage: 21.754% (+13.3%) from 8.423%
15040977736

Pull #6960

github

web-flow
Merge 7a7a13526 into 1862d03b7
Pull Request #6960: feat(json-schema): mutualize json schema between formats

320 of 460 new or added lines in 24 files covered. (69.57%)

1863 existing lines in 109 files now uncovered.

11069 of 50882 relevant lines covered (21.75%)

29.49 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

85.19
/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 VERSION_LOCAL = 'local';
34
    public const UNKNOWN_TYPE = 'unknown_type';
35

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

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

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

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

62
        return $schema;
15✔
63
    }
64

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

73
        return $definitions;
15✔
74
    }
75

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

84
            return;
15✔
85
        }
86

UNCOV
87
        $this['definitions'] = $definitions;
×
88
    }
89

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

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

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

112
        return $this->removeDefinitionKeyPrefix($ref);
15✔
113
    }
114

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

123
    private function removeDefinitionKeyPrefix(string $definitionKey): string
124
    {
125
        // strlen('#/definitions/') = 14
126
        // strlen('#/components/schemas/') = 21
127
        $prefix = match ($this->version) {
15✔
NEW
128
            self::VERSION_LOCAL => 9,
×
129
            self::VERSION_OPENAPI => 21,
15✔
NEW
130
            default => 14,
×
131
        };
15✔
132

133
        return substr($definitionKey, $prefix);
15✔
134
    }
135
}
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