• 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

29.03
/src/OpenApi/Factory/TypeFactoryTrait.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\OpenApi\Factory;
15

16
use Ramsey\Uuid\UuidInterface;
17
use Symfony\Component\PropertyInfo\Type;
18
use Symfony\Component\Uid\Ulid;
19
use Symfony\Component\Uid\Uuid;
20

21
/**
22
 * @internal
23
 */
24
trait TypeFactoryTrait
25
{
26
    private function getType(Type $type): array
27
    {
UNCOV
28
        if ($type->isCollection()) {
15✔
UNCOV
29
            $keyType = $type->getCollectionKeyTypes()[0] ?? null;
15✔
UNCOV
30
            $subType = ($type->getCollectionValueTypes()[0] ?? null) ?? new Type($type->getBuiltinType(), false, $type->getClassName(), false);
15✔
31

UNCOV
32
            if (null !== $keyType && Type::BUILTIN_TYPE_STRING === $keyType->getBuiltinType()) {
15✔
33
                return $this->addNullabilityToTypeDefinition([
×
34
                    'type' => 'object',
×
35
                    'additionalProperties' => $this->getType($subType),
×
36
                ], $type);
×
37
            }
38

UNCOV
39
            return $this->addNullabilityToTypeDefinition([
15✔
UNCOV
40
                'type' => 'array',
15✔
UNCOV
41
                'items' => $this->getType($subType),
15✔
UNCOV
42
            ], $type);
15✔
43
        }
44

UNCOV
45
        return $this->addNullabilityToTypeDefinition($this->makeBasicType($type), $type);
15✔
46
    }
47

48
    private function makeBasicType(Type $type): array
49
    {
UNCOV
50
        return match ($type->getBuiltinType()) {
15✔
UNCOV
51
            Type::BUILTIN_TYPE_INT => ['type' => 'integer'],
14✔
UNCOV
52
            Type::BUILTIN_TYPE_FLOAT => ['type' => 'number'],
14✔
UNCOV
53
            Type::BUILTIN_TYPE_BOOL => ['type' => 'boolean'],
14✔
54
            Type::BUILTIN_TYPE_OBJECT => $this->getClassType($type->getClassName(), $type->isNullable()),
12✔
UNCOV
55
            default => ['type' => 'string'],
15✔
UNCOV
56
        };
15✔
57
    }
58

59
    /**
60
     * Gets the JSON Schema document which specifies the data type corresponding to the given PHP class, and recursively adds needed new schema to the current schema if provided.
61
     */
62
    private function getClassType(?string $className, bool $nullable): array
63
    {
64
        if (null === $className) {
×
65
            return ['type' => 'string'];
×
66
        }
67

68
        if (is_a($className, \DateTimeInterface::class, true)) {
×
69
            return [
×
70
                'type' => 'string',
×
71
                'format' => 'date-time',
×
72
            ];
×
73
        }
74
        if (is_a($className, \DateInterval::class, true)) {
×
75
            return [
×
76
                'type' => 'string',
×
77
                'format' => 'duration',
×
78
            ];
×
79
        }
80
        if (is_a($className, UuidInterface::class, true) || is_a($className, Uuid::class, true)) {
×
81
            return [
×
82
                'type' => 'string',
×
83
                'format' => 'uuid',
×
84
            ];
×
85
        }
86
        if (is_a($className, Ulid::class, true)) {
×
87
            return [
×
88
                'type' => 'string',
×
89
                'format' => 'ulid',
×
90
            ];
×
91
        }
92
        if (is_a($className, \SplFileInfo::class, true)) {
×
93
            return [
×
94
                'type' => 'string',
×
95
                'format' => 'binary',
×
96
            ];
×
97
        }
98
        if (is_a($className, \BackedEnum::class, true)) {
×
99
            $enumCases = array_map(static fn (\BackedEnum $enum): string|int => $enum->value, $className::cases());
×
100

101
            $type = \is_string($enumCases[0] ?? '') ? 'string' : 'integer';
×
102

103
            if ($nullable) {
×
104
                $enumCases[] = null;
×
105
            }
106

107
            return [
×
108
                'type' => $type,
×
109
                'enum' => $enumCases,
×
110
            ];
×
111
        }
112

113
        return ['type' => 'string'];
×
114
    }
115

116
    /**
117
     * @param array<string, mixed> $jsonSchema
118
     *
119
     * @return array<string, mixed>
120
     */
121
    private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type): array
122
    {
UNCOV
123
        if (!$type->isNullable()) {
15✔
UNCOV
124
            return $jsonSchema;
15✔
125
        }
126

127
        $typeDefinition = ['anyOf' => [$jsonSchema]];
×
128
        $typeDefinition['anyOf'][] = ['type' => 'null'];
×
129

130
        return $typeDefinition;
×
131
    }
132
}
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