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

api-platform / core / 7545112132

16 Jan 2024 05:12PM UTC coverage: 66.567% (-0.03%) from 66.597%
7545112132

push

github

web-flow
fix(jsonschema): keep integer and number properties draft 4 compliant (#6098)

16 of 18 new or added lines in 2 files covered. (88.89%)

16231 of 24383 relevant lines covered (66.57%)

37.31 hits per line

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

87.5
/src/JsonSchema/BackwardCompatibleSchemaFactory.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
use ApiPlatform\Metadata\Operation;
17

18
/**
19
 * This factory decorates range integer and number properties to keep Draft 4 backward compatibility.
20
 *
21
 * @see https://github.com/api-platform/core/issues/6041
22
 *
23
 * @internal
24
 */
25
final class BackwardCompatibleSchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface
26
{
27
    public const SCHEMA_DRAFT4_VERSION = 'draft_4';
28

29
    public function __construct(private readonly SchemaFactoryInterface $decorated)
30
    {
31
    }
150✔
32

33
    /**
34
     * {@inheritDoc}
35
     */
36
    public function buildSchema(string $className, string $format = 'json', string $type = Schema::TYPE_OUTPUT, Operation $operation = null, Schema $schema = null, array $serializerContext = null, bool $forceCollection = false): Schema
37
    {
38
        $schema = $this->decorated->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection);
84✔
39

40
        if (!($serializerContext[self::SCHEMA_DRAFT4_VERSION] ?? false)) {
84✔
41
            return $schema;
60✔
42
        }
43

44
        foreach ($schema->getDefinitions() as $definition) {
24✔
45
            foreach ($definition['properties'] ?? [] as $property) {
24✔
46
                if (isset($property['type']) && \in_array($property['type'], ['integer', 'number'], true)) {
24✔
47
                    if (isset($property['exclusiveMinimum'])) {
12✔
48
                        $property['minimum'] = $property['exclusiveMinimum'];
4✔
49
                        $property['exclusiveMinimum'] = true;
4✔
50
                    }
51
                    if (isset($property['exclusiveMaximum'])) {
12✔
52
                        $property['maximum'] = $property['exclusiveMaximum'];
4✔
53
                        $property['exclusiveMaximum'] = true;
4✔
54
                    }
55
                }
56
            }
57
        }
58

59
        return $schema;
24✔
60
    }
61

62
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
63
    {
NEW
64
        if ($this->decorated instanceof SchemaFactoryAwareInterface) {
×
NEW
65
            $this->decorated->setSchemaFactory($schemaFactory);
×
66
        }
67
    }
68
}
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