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

api-platform / core / 16531587208

25 Jul 2025 09:05PM UTC coverage: 0.0% (-22.1%) from 22.07%
16531587208

Pull #7225

github

web-flow
Merge 23f449a58 into 02a764950
Pull Request #7225: feat: json streamer

0 of 294 new or added lines in 31 files covered. (0.0%)

11514 existing lines in 375 files now uncovered.

0 of 51976 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/src/JsonSchema/Metadata/Property/Factory/SchemaPropertyMetadataFactory.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\Metadata\Property\Factory;
15

16
use ApiPlatform\JsonSchema\Schema;
17
use ApiPlatform\Metadata\ApiProperty;
18
use ApiPlatform\Metadata\Exception\PropertyNotFoundException;
19
use ApiPlatform\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
20
use ApiPlatform\Metadata\ResourceClassResolverInterface;
21
use ApiPlatform\Metadata\Util\ResourceClassInfoTrait;
22
use Doctrine\Common\Collections\ArrayCollection;
23
use Ramsey\Uuid\UuidInterface;
24
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
25
use Symfony\Component\PropertyInfo\Type as LegacyType;
26
use Symfony\Component\TypeInfo\Type;
27
use Symfony\Component\TypeInfo\Type\BuiltinType;
28
use Symfony\Component\TypeInfo\Type\CollectionType;
29
use Symfony\Component\TypeInfo\Type\IntersectionType;
30
use Symfony\Component\TypeInfo\Type\ObjectType;
31
use Symfony\Component\TypeInfo\Type\UnionType;
32
use Symfony\Component\TypeInfo\Type\WrappingTypeInterface;
33
use Symfony\Component\TypeInfo\TypeIdentifier;
34
use Symfony\Component\Uid\Ulid;
35
use Symfony\Component\Uid\Uuid;
36

37
/**
38
 * Build ApiProperty::schema.
39
 */
40
final class SchemaPropertyMetadataFactory implements PropertyMetadataFactoryInterface
41
{
42
    use ResourceClassInfoTrait;
43

44
    public const JSON_SCHEMA_USER_DEFINED = 'user_defined_schema';
45

46
    public function __construct(
47
        ResourceClassResolverInterface $resourceClassResolver,
48
        private readonly ?PropertyMetadataFactoryInterface $decorated = null,
49
    ) {
UNCOV
50
        $this->resourceClassResolver = $resourceClassResolver;
×
51
    }
52

53
    public function create(string $resourceClass, string $property, array $options = []): ApiProperty
54
    {
UNCOV
55
        if (null === $this->decorated) {
×
56
            $propertyMetadata = new ApiProperty();
×
57
        } else {
58
            try {
UNCOV
59
                $propertyMetadata = $this->decorated->create($resourceClass, $property, $options);
×
60
            } catch (PropertyNotFoundException) {
×
61
                $propertyMetadata = new ApiProperty();
×
62
            }
63
        }
64

UNCOV
65
        $extraProperties = $propertyMetadata->getExtraProperties();
×
66
        // see AttributePropertyMetadataFactory
UNCOV
67
        if (true === ($extraProperties[self::JSON_SCHEMA_USER_DEFINED] ?? false)) {
×
68
            // schema seems to have been declared by the user: do not override nor complete user value
UNCOV
69
            return $propertyMetadata;
×
70
        }
71

UNCOV
72
        $link = (($options['schema_type'] ?? null) === Schema::TYPE_INPUT) ? $propertyMetadata->isWritableLink() : $propertyMetadata->isReadableLink();
×
UNCOV
73
        $propertySchema = $propertyMetadata->getSchema() ?? [];
×
74

UNCOV
75
        if (null !== $propertyMetadata->getUriTemplate() || (!\array_key_exists('readOnly', $propertySchema) && false === $propertyMetadata->isWritable() && !$propertyMetadata->isInitializable())) {
×
UNCOV
76
            $propertySchema['readOnly'] = true;
×
77
        }
78

UNCOV
79
        if (!\array_key_exists('writeOnly', $propertySchema) && false === $propertyMetadata->isReadable()) {
×
UNCOV
80
            $propertySchema['writeOnly'] = true;
×
81
        }
82

UNCOV
83
        if (!\array_key_exists('description', $propertySchema) && null !== ($description = $propertyMetadata->getDescription())) {
×
UNCOV
84
            $propertySchema['description'] = $description;
×
85
        }
86

87
        // see https://github.com/json-schema-org/json-schema-spec/pull/737
UNCOV
88
        if (!\array_key_exists('deprecated', $propertySchema) && null !== $propertyMetadata->getDeprecationReason()) {
×
UNCOV
89
            $propertySchema['deprecated'] = true;
×
90
        }
91

92
        // externalDocs is an OpenAPI specific extension, but JSON Schema allows additional keys, so we always add it
93
        // See https://json-schema.org/latest/json-schema-core.html#rfc.section.6.4
UNCOV
94
        if (!\array_key_exists('externalDocs', $propertySchema) && null !== ($iri = $propertyMetadata->getTypes()[0] ?? null)) {
×
UNCOV
95
            $propertySchema['externalDocs'] = ['url' => $iri];
×
96
        }
97

UNCOV
98
        if (!method_exists(PropertyInfoExtractor::class, 'getType')) {
×
99
            return $propertyMetadata->withSchema($this->getLegacyTypeSchema($propertyMetadata, $propertySchema, $resourceClass, $property, $link));
×
100
        }
101

UNCOV
102
        return $propertyMetadata->withSchema($this->getTypeSchema($propertyMetadata, $propertySchema, $link));
×
103
    }
104

105
    private function getTypeSchema(ApiProperty $propertyMetadata, array $propertySchema, ?bool $link): array
106
    {
UNCOV
107
        $type = $propertyMetadata->getNativeType();
×
108

UNCOV
109
        $className = null;
×
UNCOV
110
        $typeIsResourceClass = function (Type $type) use (&$className): bool {
×
UNCOV
111
            return $type instanceof ObjectType && $this->resourceClassResolver->isResourceClass($className = $type->getClassName());
×
UNCOV
112
        };
×
UNCOV
113
        $isResourceClass = $type?->isSatisfiedBy($typeIsResourceClass);
×
114

UNCOV
115
        if (null !== $propertyMetadata->getUriTemplate() || (!\array_key_exists('readOnly', $propertySchema) && false === $propertyMetadata->isWritable() && !$propertyMetadata->isInitializable()) && !$className) {
×
116
            $propertySchema['readOnly'] = true;
×
117
        }
118

UNCOV
119
        if (!\array_key_exists('default', $propertySchema) && null !== ($default = $propertyMetadata->getDefault()) && false === (\is_array($default) && empty($default)) && !$isResourceClass) {
×
UNCOV
120
            if ($default instanceof \BackedEnum) {
×
UNCOV
121
                $default = $default->value;
×
122
            }
UNCOV
123
            $propertySchema['default'] = $default;
×
124
        }
125

UNCOV
126
        if (!\array_key_exists('example', $propertySchema) && null !== ($example = $propertyMetadata->getExample()) && false === (\is_array($example) && empty($example))) {
×
UNCOV
127
            $propertySchema['example'] = $example;
×
128
        }
129

UNCOV
130
        $hasType = $this->getSchemaValue($propertySchema, 'type') || $this->getSchemaValue($propertyMetadata->getJsonSchemaContext() ?? [], 'type') || $this->getSchemaValue($propertyMetadata->getOpenapiContext() ?? [], 'type');
×
UNCOV
131
        $hasRef = $this->getSchemaValue($propertySchema, '$ref') || $this->getSchemaValue($propertyMetadata->getJsonSchemaContext() ?? [], '$ref') || $this->getSchemaValue($propertyMetadata->getOpenapiContext() ?? [], '$ref');
×
132

133
        // never override the following keys if at least one is already set or if there's a custom openapi context
UNCOV
134
        if ($hasType || $hasRef || !$type) {
×
UNCOV
135
            return $propertySchema;
×
136
        }
137

UNCOV
138
        if ($type instanceof CollectionType && null !== $propertyMetadata->getUriTemplate()) {
×
139
            $type = $type->getCollectionValueType();
×
140
        }
141

UNCOV
142
        return $propertySchema + $this->getJsonSchemaFromType($type, $link);
×
143
    }
144

145
    /**
146
     * Applies nullability rules to a generated JSON schema based on the original type's nullability.
147
     *
148
     * @param array<string, mixed> $schema     the base JSON schema generated for the non-null type
149
     * @param bool                 $isNullable whether the original type allows null
150
     *
151
     * @return array<string, mixed> the JSON schema with nullability applied
152
     */
153
    private function applyNullability(array $schema, bool $isNullable): array
154
    {
UNCOV
155
        if (!$isNullable) {
×
UNCOV
156
            return $schema;
×
157
        }
158

UNCOV
159
        if (isset($schema['type']) && 'null' === $schema['type'] && 1 === \count($schema)) {
×
UNCOV
160
            return $schema;
×
161
        }
162

UNCOV
163
        if (isset($schema['anyOf']) && \is_array($schema['anyOf'])) {
×
UNCOV
164
            $hasNull = false;
×
UNCOV
165
            foreach ($schema['anyOf'] as $anyOfSchema) {
×
UNCOV
166
                if (isset($anyOfSchema['type']) && 'null' === $anyOfSchema['type']) {
×
167
                    $hasNull = true;
×
168
                    break;
×
169
                }
170
            }
UNCOV
171
            if (!$hasNull) {
×
UNCOV
172
                $schema['anyOf'][] = ['type' => 'null'];
×
173
            }
174

UNCOV
175
            return $schema;
×
176
        }
177

UNCOV
178
        if (isset($schema['type'])) {
×
UNCOV
179
            $currentType = $schema['type'];
×
UNCOV
180
            $schema['type'] = \is_array($currentType) ? array_merge($currentType, ['null']) : [$currentType, 'null'];
×
181

UNCOV
182
            if (isset($schema['enum'])) {
×
UNCOV
183
                $schema['enum'][] = null;
×
184

UNCOV
185
                return $schema;
×
186
            }
187

UNCOV
188
            return $schema;
×
189
        }
190

191
        return ['anyOf' => [$schema, ['type' => 'null']]];
×
192
    }
193

194
    /**
195
     * Converts a TypeInfo Type into a JSON Schema definition array.
196
     *
197
     * @return array<string, mixed>
198
     */
199
    private function getJsonSchemaFromType(Type $type, ?bool $readableLink = null): array
200
    {
UNCOV
201
        $isNullable = $type->isNullable();
×
202

UNCOV
203
        if ($type instanceof UnionType) {
×
UNCOV
204
            $subTypes = array_filter($type->getTypes(), fn ($t) => !($t instanceof BuiltinType && $t->isIdentifiedBy(TypeIdentifier::NULL)));
×
205

UNCOV
206
            foreach ($subTypes as $t) {
×
UNCOV
207
                $s = $this->getJsonSchemaFromType($t, $readableLink);
×
208
                // We can not find what type this is, let it be computed at runtime by the SchemaFactory
UNCOV
209
                if (($s['type'] ?? null) === Schema::UNKNOWN_TYPE) {
×
UNCOV
210
                    return $s;
×
211
                }
212
            }
213

UNCOV
214
            $schemas = array_map(fn ($t) => $this->getJsonSchemaFromType($t, $readableLink), $subTypes);
×
215

UNCOV
216
            if (0 === \count($schemas)) {
×
217
                $schema = [];
×
UNCOV
218
            } elseif (1 === \count($schemas)) {
×
UNCOV
219
                $schema = current($schemas);
×
220
            } else {
UNCOV
221
                $schema = ['anyOf' => $schemas];
×
222
            }
223

UNCOV
224
            return $this->applyNullability($schema, $isNullable);
×
225
        }
226

UNCOV
227
        if ($type instanceof IntersectionType) {
×
UNCOV
228
            $schemas = [];
×
UNCOV
229
            foreach ($type->getTypes() as $t) {
×
UNCOV
230
                while ($t instanceof WrappingTypeInterface) {
×
231
                    $t = $t->getWrappedType();
×
232
                }
233

UNCOV
234
                $subSchema = $this->getJsonSchemaFromType($t, $readableLink);
×
UNCOV
235
                if (!empty($subSchema)) {
×
UNCOV
236
                    $schemas[] = $subSchema;
×
237
                }
238
            }
239

UNCOV
240
            return $this->applyNullability(['allOf' => $schemas], $isNullable);
×
241
        }
242

UNCOV
243
        if ($type instanceof CollectionType) {
×
UNCOV
244
            $valueType = $type->getCollectionValueType();
×
UNCOV
245
            $valueSchema = $this->getJsonSchemaFromType($valueType, $readableLink);
×
UNCOV
246
            $keyType = $type->getCollectionKeyType();
×
247

248
            // Associative array (string keys)
UNCOV
249
            if ($keyType->isSatisfiedBy(fn (Type $t) => $t instanceof BuiltinType && $t->isIdentifiedBy(TypeIdentifier::INT))) {
×
UNCOV
250
                $schema = [
×
UNCOV
251
                    'type' => 'array',
×
UNCOV
252
                    'items' => $valueSchema,
×
UNCOV
253
                ];
×
254
            } else { // List (int keys)
UNCOV
255
                $schema = [
×
UNCOV
256
                    'type' => 'object',
×
UNCOV
257
                    'additionalProperties' => $valueSchema,
×
UNCOV
258
                ];
×
259
            }
260

UNCOV
261
            return $this->applyNullability($schema, $isNullable);
×
262
        }
263

UNCOV
264
        if ($type instanceof ObjectType) {
×
UNCOV
265
            $schema = $this->getClassSchemaDefinition($type->getClassName(), $readableLink);
×
266

UNCOV
267
            return $this->applyNullability($schema, $isNullable);
×
268
        }
269

UNCOV
270
        if ($type instanceof BuiltinType) {
×
UNCOV
271
            $schema = match ($type->getTypeIdentifier()) {
×
UNCOV
272
                TypeIdentifier::INT => ['type' => 'integer'],
×
UNCOV
273
                TypeIdentifier::FLOAT => ['type' => 'number'],
×
UNCOV
274
                TypeIdentifier::BOOL => ['type' => 'boolean'],
×
UNCOV
275
                TypeIdentifier::TRUE => ['type' => 'boolean', 'const' => true],
×
UNCOV
276
                TypeIdentifier::FALSE => ['type' => 'boolean', 'const' => false],
×
UNCOV
277
                TypeIdentifier::STRING => ['type' => 'string'],
×
UNCOV
278
                TypeIdentifier::ARRAY => ['type' => 'array', 'items' => []],
×
UNCOV
279
                TypeIdentifier::ITERABLE => ['type' => 'array', 'items' => []],
×
UNCOV
280
                TypeIdentifier::OBJECT => ['type' => 'object'],
×
UNCOV
281
                TypeIdentifier::RESOURCE => ['type' => 'string'],
×
UNCOV
282
                TypeIdentifier::CALLABLE => ['type' => 'string'],
×
UNCOV
283
                default => ['type' => 'null'],
×
UNCOV
284
            };
×
285

UNCOV
286
            return $this->applyNullability($schema, $isNullable);
×
287
        }
288

289
        return ['type' => Schema::UNKNOWN_TYPE];
×
290
    }
291

292
    /**
293
     * Gets the JSON Schema definition for a class.
294
     */
295
    private function getClassSchemaDefinition(?string $className, ?bool $readableLink): array
296
    {
UNCOV
297
        if (null === $className) {
×
298
            return ['type' => 'string'];
×
299
        }
300

UNCOV
301
        if (is_a($className, \DateTimeInterface::class, true)) {
×
UNCOV
302
            return ['type' => 'string', 'format' => 'date-time'];
×
303
        }
304

UNCOV
305
        if (is_a($className, \DateInterval::class, true)) {
×
306
            return ['type' => 'string', 'format' => 'duration'];
×
307
        }
308

UNCOV
309
        if (is_a($className, UuidInterface::class, true) || is_a($className, Uuid::class, true)) {
×
UNCOV
310
            return ['type' => 'string', 'format' => 'uuid'];
×
311
        }
312

UNCOV
313
        if (is_a($className, Ulid::class, true)) {
×
314
            return ['type' => 'string', 'format' => 'ulid'];
×
315
        }
316

UNCOV
317
        if (is_a($className, \SplFileInfo::class, true)) {
×
318
            return ['type' => 'string', 'format' => 'binary'];
×
319
        }
320

UNCOV
321
        $isResourceClass = $this->isResourceClass($className);
×
UNCOV
322
        if (!$isResourceClass && is_a($className, \BackedEnum::class, true)) {
×
UNCOV
323
            $enumCases = array_map(static fn (\BackedEnum $enum): string|int => $enum->value, $className::cases());
×
UNCOV
324
            $type = \is_string($enumCases[0] ?? '') ? 'string' : 'integer';
×
325

UNCOV
326
            return ['type' => $type, 'enum' => $enumCases];
×
327
        }
328

UNCOV
329
        if (false === $readableLink && $isResourceClass) {
×
UNCOV
330
            return [
×
UNCOV
331
                'type' => 'string',
×
UNCOV
332
                'format' => 'iri-reference',
×
UNCOV
333
                'example' => 'https://example.com/',
×
UNCOV
334
            ];
×
335
        }
336

UNCOV
337
        return ['type' => Schema::UNKNOWN_TYPE];
×
338
    }
339

340
    private function getLegacyTypeSchema(ApiProperty $propertyMetadata, array $propertySchema, string $resourceClass, string $property, ?bool $link): array
341
    {
342
        $types = $propertyMetadata->getBuiltinTypes() ?? [];
×
343
        $className = ($types[0] ?? null)?->getClassName() ?? null;
×
344

345
        if (null !== $propertyMetadata->getUriTemplate() || (!\array_key_exists('readOnly', $propertySchema) && false === $propertyMetadata->isWritable() && !$propertyMetadata->isInitializable()) && !$className) {
×
346
            $propertySchema['readOnly'] = true;
×
347
        }
348

349
        if (!\array_key_exists('default', $propertySchema) && !empty($default = $propertyMetadata->getDefault()) && (!$className || !$this->isResourceClass($className))) {
×
350
            if ($default instanceof \BackedEnum) {
×
351
                $default = $default->value;
×
352
            }
353
            $propertySchema['default'] = $default;
×
354
        }
355

356
        if (!\array_key_exists('example', $propertySchema) && !empty($example = $propertyMetadata->getExample())) {
×
357
            $propertySchema['example'] = $example;
×
358
        }
359

360
        // never override the following keys if at least one is already set or if there's a custom openapi context
361
        if (
362
            [] === $types
×
363
            || ($propertySchema['type'] ?? $propertySchema['$ref'] ?? $propertySchema['anyOf'] ?? $propertySchema['allOf'] ?? $propertySchema['oneOf'] ?? false)
×
364
            || \array_key_exists('type', $propertyMetadata->getOpenapiContext() ?? [])
×
365
        ) {
366
            return $propertySchema;
×
367
        }
368

369
        if ($propertyMetadata->getUriTemplate()) {
×
370
            return $propertySchema + [
×
371
                'type' => 'string',
×
372
                'format' => 'iri-reference',
×
373
                'example' => 'https://example.com/',
×
374
            ];
×
375
        }
376

377
        $valueSchema = [];
×
378
        foreach ($types as $type) {
×
379
            // Temp fix for https://github.com/symfony/symfony/pull/52699
380
            if (ArrayCollection::class === $type->getClassName()) {
×
381
                $type = new LegacyType($type->getBuiltinType(), $type->isNullable(), $type->getClassName(), true, $type->getCollectionKeyTypes(), $type->getCollectionValueTypes());
×
382
            }
383

384
            if ($isCollection = $type->isCollection()) {
×
385
                $keyType = $type->getCollectionKeyTypes()[0] ?? null;
×
386
                $valueType = $type->getCollectionValueTypes()[0] ?? null;
×
387
            } else {
388
                $keyType = null;
×
389
                $valueType = $type;
×
390
            }
391

392
            if (null === $valueType) {
×
393
                $builtinType = 'string';
×
394
                $className = null;
×
395
            } else {
396
                $builtinType = $valueType->getBuiltinType();
×
397
                $className = $valueType->getClassName();
×
398
            }
399

400
            if ($isCollection && null !== $propertyMetadata->getUriTemplate()) {
×
401
                $keyType = null;
×
402
                $isCollection = false;
×
403
            }
404

405
            $propertyType = $this->getLegacyType(new LegacyType($builtinType, $type->isNullable(), $className, $isCollection, $keyType, $valueType), $link);
×
406
            if (!\in_array($propertyType, $valueSchema, true)) {
×
407
                $valueSchema[] = $propertyType;
×
408
            }
409
        }
410

411
        if (1 === \count($valueSchema)) {
×
412
            return $propertySchema + $valueSchema[0];
×
413
        }
414

415
        // multiple builtInTypes detected: determine oneOf/allOf if union vs intersect types
416
        try {
417
            $reflectionClass = new \ReflectionClass($resourceClass);
×
418
            $reflectionProperty = $reflectionClass->getProperty($property);
×
419
            $composition = $reflectionProperty->getType() instanceof \ReflectionUnionType ? 'oneOf' : 'allOf';
×
420
        } catch (\ReflectionException) {
×
421
            // cannot detect types
422
            $composition = 'anyOf';
×
423
        }
424

425
        return $propertySchema + [$composition => $valueSchema];
×
426
    }
427

428
    private function getLegacyType(LegacyType $type, ?bool $readableLink = null): array
429
    {
430
        if (!$type->isCollection()) {
×
431
            return $this->addNullabilityToTypeDefinition($this->legacyTypeToArray($type, $readableLink), $type);
×
432
        }
433

434
        $keyType = $type->getCollectionKeyTypes()[0] ?? null;
×
435
        $subType = ($type->getCollectionValueTypes()[0] ?? null) ?? new LegacyType($type->getBuiltinType(), false, $type->getClassName(), false);
×
436

437
        if (null !== $keyType && LegacyType::BUILTIN_TYPE_STRING === $keyType->getBuiltinType()) {
×
438
            return $this->addNullabilityToTypeDefinition([
×
439
                'type' => 'object',
×
440
                'additionalProperties' => $this->getLegacyType($subType, $readableLink),
×
441
            ], $type);
×
442
        }
443

444
        return $this->addNullabilityToTypeDefinition([
×
445
            'type' => 'array',
×
446
            'items' => $this->getLegacyType($subType, $readableLink),
×
447
        ], $type);
×
448
    }
449

450
    private function legacyTypeToArray(LegacyType $type, ?bool $readableLink = null): array
451
    {
452
        return match ($type->getBuiltinType()) {
×
453
            LegacyType::BUILTIN_TYPE_INT => ['type' => 'integer'],
×
454
            LegacyType::BUILTIN_TYPE_FLOAT => ['type' => 'number'],
×
455
            LegacyType::BUILTIN_TYPE_BOOL => ['type' => 'boolean'],
×
456
            LegacyType::BUILTIN_TYPE_OBJECT => $this->getLegacyClassType($type->getClassName(), $type->isNullable(), $readableLink),
×
457
            default => ['type' => 'string'],
×
458
        };
×
459
    }
460

461
    /**
462
     * 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.
463
     *
464
     * Note: if the class is not part of exceptions listed above, any class is considered as a resource.
465
     *
466
     * @throws PropertyNotFoundException
467
     *
468
     * @return array<string, mixed>
469
     */
470
    private function getLegacyClassType(?string $className, bool $nullable, ?bool $readableLink): array
471
    {
472
        if (null === $className) {
×
473
            return ['type' => 'string'];
×
474
        }
475

476
        if (is_a($className, \DateTimeInterface::class, true)) {
×
477
            return [
×
478
                'type' => 'string',
×
479
                'format' => 'date-time',
×
480
            ];
×
481
        }
482

483
        if (is_a($className, \DateInterval::class, true)) {
×
484
            return [
×
485
                'type' => 'string',
×
486
                'format' => 'duration',
×
487
            ];
×
488
        }
489

490
        if (is_a($className, UuidInterface::class, true) || is_a($className, Uuid::class, true)) {
×
491
            return [
×
492
                'type' => 'string',
×
493
                'format' => 'uuid',
×
494
            ];
×
495
        }
496

497
        if (is_a($className, Ulid::class, true)) {
×
498
            return [
×
499
                'type' => 'string',
×
500
                'format' => 'ulid',
×
501
            ];
×
502
        }
503

504
        if (is_a($className, \SplFileInfo::class, true)) {
×
505
            return [
×
506
                'type' => 'string',
×
507
                'format' => 'binary',
×
508
            ];
×
509
        }
510

511
        $isResourceClass = $this->isResourceClass($className);
×
512
        if (!$isResourceClass && is_a($className, \BackedEnum::class, true)) {
×
513
            $enumCases = array_map(static fn (\BackedEnum $enum): string|int => $enum->value, $className::cases());
×
514

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

517
            if ($nullable) {
×
518
                $enumCases[] = null;
×
519
            }
520

521
            return [
×
522
                'type' => $type,
×
523
                'enum' => $enumCases,
×
524
            ];
×
525
        }
526

527
        if (false === $readableLink && $isResourceClass) {
×
528
            return [
×
529
                'type' => 'string',
×
530
                'format' => 'iri-reference',
×
531
                'example' => 'https://example.com/',
×
532
            ];
×
533
        }
534

535
        // When this is set, we compute the schema at SchemaFactory::buildPropertySchema as it
536
        // will end up being a $ref to another class schema, we don't have enough informations here
537
        return ['type' => Schema::UNKNOWN_TYPE];
×
538
    }
539

540
    /**
541
     * @param array<string, mixed> $jsonSchema
542
     *
543
     * @return array<string, mixed>
544
     */
545
    private function addNullabilityToTypeDefinition(array $jsonSchema, LegacyType $type): array
546
    {
547
        if (!$type->isNullable()) {
×
548
            return $jsonSchema;
×
549
        }
550

551
        if (\array_key_exists('$ref', $jsonSchema)) {
×
552
            return ['anyOf' => [$jsonSchema, ['type' => 'null']]];
×
553
        }
554

555
        return [...$jsonSchema, ...[
×
556
            'type' => \is_array($jsonSchema['type'])
×
557
                ? array_merge($jsonSchema['type'], ['null'])
×
558
                : [$jsonSchema['type'], 'null'],
×
559
        ]];
×
560
    }
561

562
    private function getSchemaValue(array $schema, string $key): array|string|null
563
    {
UNCOV
564
        if (isset($schema['items'])) {
×
UNCOV
565
            $schema = $schema['items'];
×
566
        }
567

UNCOV
568
        return $schema[$key] ?? $schema['allOf'][0][$key] ?? $schema['anyOf'][0][$key] ?? $schema['oneOf'][0][$key] ?? null;
×
569
    }
570
}
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