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

api-platform / core / 6067528200

04 Sep 2023 12:12AM UTC coverage: 36.875% (-21.9%) from 58.794%
6067528200

Pull #5791

github

web-flow
Merge 64157e578 into d09cfc9d2
Pull Request #5791: fix: strip down any sql function name

3096 of 3096 new or added lines in 205 files covered. (100.0%)

9926 of 26918 relevant lines covered (36.87%)

6.5 hits per line

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

23.33
/src/JsonSchema/TypeFactory.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\ResourceClassResolverInterface;
17
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
18
use Ramsey\Uuid\UuidInterface;
19
use Symfony\Component\PropertyInfo\Type;
20
use Symfony\Component\Uid\Ulid;
21
use Symfony\Component\Uid\Uuid;
22

23
/**
24
 * {@inheritdoc}
25
 *
26
 * @author Kévin Dunglas <dunglas@gmail.com>
27
 */
28
final class TypeFactory implements TypeFactoryInterface
29
{
30
    use ResourceClassInfoTrait;
31

32
    private ?SchemaFactoryInterface $schemaFactory = null;
33

34
    public function __construct(ResourceClassResolverInterface $resourceClassResolver = null)
35
    {
36
        $this->resourceClassResolver = $resourceClassResolver;
67✔
37
    }
38

39
    public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void
40
    {
41
        $this->schemaFactory = $schemaFactory;
67✔
42
    }
43

44
    /**
45
     * {@inheritdoc}
46
     */
47
    public function getType(Type $type, string $format = 'json', bool $readableLink = null, array $serializerContext = null, Schema $schema = null): array
48
    {
49
        if ($type->isCollection()) {
9✔
50
            $keyType = $type->getCollectionKeyTypes()[0] ?? null;
9✔
51
            $subType = ($type->getCollectionValueTypes()[0] ?? null) ?? new Type($type->getBuiltinType(), false, $type->getClassName(), false);
9✔
52

53
            if (null !== $keyType && Type::BUILTIN_TYPE_STRING === $keyType->getBuiltinType()) {
9✔
54
                return $this->addNullabilityToTypeDefinition([
×
55
                    'type' => 'object',
×
56
                    'additionalProperties' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
×
57
                ], $type, $schema);
×
58
            }
59

60
            return $this->addNullabilityToTypeDefinition([
9✔
61
                'type' => 'array',
9✔
62
                'items' => $this->getType($subType, $format, $readableLink, $serializerContext, $schema),
9✔
63
            ], $type, $schema);
9✔
64
        }
65

66
        return $this->addNullabilityToTypeDefinition($this->makeBasicType($type, $format, $readableLink, $serializerContext, $schema), $type, $schema);
9✔
67
    }
68

69
    private function makeBasicType(Type $type, string $format = 'json', bool $readableLink = null, array $serializerContext = null, Schema $schema = null): array
70
    {
71
        return match ($type->getBuiltinType()) {
9✔
72
            Type::BUILTIN_TYPE_INT => ['type' => 'integer'],
9✔
73
            Type::BUILTIN_TYPE_FLOAT => ['type' => 'number'],
9✔
74
            Type::BUILTIN_TYPE_BOOL => ['type' => 'boolean'],
9✔
75
            Type::BUILTIN_TYPE_OBJECT => $this->getClassType($type->getClassName(), $type->isNullable(), $format, $readableLink, $serializerContext, $schema),
9✔
76
            default => ['type' => 'string'],
9✔
77
        };
9✔
78
    }
79

80
    /**
81
     * 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.
82
     */
83
    private function getClassType(?string $className, bool $nullable, string $format, ?bool $readableLink, ?array $serializerContext, ?Schema $schema): array
84
    {
85
        if (null === $className) {
×
86
            return ['type' => 'string'];
×
87
        }
88

89
        if (is_a($className, \DateTimeInterface::class, true)) {
×
90
            return [
×
91
                'type' => 'string',
×
92
                'format' => 'date-time',
×
93
            ];
×
94
        }
95
        if (is_a($className, \DateInterval::class, true)) {
×
96
            return [
×
97
                'type' => 'string',
×
98
                'format' => 'duration',
×
99
            ];
×
100
        }
101
        if (is_a($className, UuidInterface::class, true) || is_a($className, Uuid::class, true)) {
×
102
            return [
×
103
                'type' => 'string',
×
104
                'format' => 'uuid',
×
105
            ];
×
106
        }
107
        if (is_a($className, Ulid::class, true)) {
×
108
            return [
×
109
                'type' => 'string',
×
110
                'format' => 'ulid',
×
111
            ];
×
112
        }
113
        if (is_a($className, \SplFileInfo::class, true)) {
×
114
            return [
×
115
                'type' => 'string',
×
116
                'format' => 'binary',
×
117
            ];
×
118
        }
119
        if (!$this->isResourceClass($className) && is_a($className, \BackedEnum::class, true)) {
×
120
            $enumCases = array_map(static fn (\BackedEnum $enum): string|int => $enum->value, $className::cases());
×
121

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

124
            if ($nullable) {
×
125
                $enumCases[] = null;
×
126
            }
127

128
            return [
×
129
                'type' => $type,
×
130
                'enum' => $enumCases,
×
131
            ];
×
132
        }
133

134
        // Skip if $schema is null (filters only support basic types)
135
        if (null === $schema) {
×
136
            return ['type' => 'string'];
×
137
        }
138

139
        if (true !== $readableLink && $this->isResourceClass($className)) {
×
140
            return [
×
141
                'type' => 'string',
×
142
                'format' => 'iri-reference',
×
143
            ];
×
144
        }
145

146
        $version = $schema->getVersion();
×
147

148
        $subSchema = new Schema($version);
×
149
        $subSchema->setDefinitions($schema->getDefinitions()); // Populate definitions of the main schema
×
150

151
        if (null === $this->schemaFactory) {
×
152
            throw new \LogicException('The schema factory must be injected by calling the "setSchemaFactory" method.');
×
153
        }
154

155
        $serializerContext += [SchemaFactory::FORCE_SUBSCHEMA => true];
×
156
        $subSchema = $this->schemaFactory->buildSchema($className, $format, Schema::TYPE_OUTPUT, null, $subSchema, $serializerContext, false);
×
157

158
        return ['$ref' => $subSchema['$ref']];
×
159
    }
160

161
    /**
162
     * @param array<string, mixed> $jsonSchema
163
     *
164
     * @return array<string, mixed>
165
     */
166
    private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type, ?Schema $schema): array
167
    {
168
        if ($schema && Schema::VERSION_SWAGGER === $schema->getVersion()) {
9✔
169
            return $jsonSchema;
×
170
        }
171

172
        if (!$type->isNullable()) {
9✔
173
            return $jsonSchema;
9✔
174
        }
175

176
        if (\array_key_exists('$ref', $jsonSchema)) {
×
177
            $typeDefinition = ['anyOf' => [$jsonSchema]];
×
178

179
            if ($schema && Schema::VERSION_JSON_SCHEMA === $schema->getVersion()) {
×
180
                $typeDefinition['anyOf'][] = ['type' => 'null'];
×
181
            } else {
182
                // OpenAPI < 3.1
183
                $typeDefinition['nullable'] = true;
×
184
            }
185

186
            return $typeDefinition;
×
187
        }
188

189
        if ($schema && Schema::VERSION_JSON_SCHEMA === $schema->getVersion()) {
×
190
            return [...$jsonSchema, ...[
×
191
                'type' => \is_array($jsonSchema['type'])
×
192
                    ? array_merge($jsonSchema['type'], ['null'])
×
193
                    : [$jsonSchema['type'], 'null'],
×
194
            ]];
×
195
        }
196

197
        return [...$jsonSchema, ...['nullable' => true]];
×
198
    }
199
}
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