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

api-platform / core / 15023181448

14 May 2025 02:19PM UTC coverage: 0.0% (-8.4%) from 8.418%
15023181448

Pull #7139

github

web-flow
Merge 9f45709da into 1862d03b7
Pull Request #7139: refactor(symfony): remove obsolete option `validator.query-parameter-validation`

0 of 4 new or added lines in 1 file covered. (0.0%)

11266 existing lines in 366 files now uncovered.

0 of 50828 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
        $typeIsResourceClass = function (Type $type) use (&$className): bool {
×
UNCOV
110
            return $type instanceof ObjectType && $this->resourceClassResolver->isResourceClass($className = $type->getClassName());
×
UNCOV
111
        };
×
112

UNCOV
113
        if (!\array_key_exists('default', $propertySchema) && !empty($default = $propertyMetadata->getDefault()) && !$type?->isSatisfiedBy($typeIsResourceClass)) {
×
UNCOV
114
            if ($default instanceof \BackedEnum) {
×
UNCOV
115
                $default = $default->value;
×
116
            }
UNCOV
117
            $propertySchema['default'] = $default;
×
118
        }
119

UNCOV
120
        if (!\array_key_exists('example', $propertySchema) && !empty($example = $propertyMetadata->getExample())) {
×
UNCOV
121
            $propertySchema['example'] = $example;
×
122
        }
123

UNCOV
124
        if (!\array_key_exists('example', $propertySchema) && \array_key_exists('default', $propertySchema)) {
×
UNCOV
125
            $propertySchema['example'] = $propertySchema['default'];
×
126
        }
127

128
        // never override the following keys if at least one is already set or if there's a custom openapi context
129
        if (
UNCOV
130
            null === $type
×
UNCOV
131
            || ($propertySchema['type'] ?? $propertySchema['$ref'] ?? $propertySchema['anyOf'] ?? $propertySchema['allOf'] ?? $propertySchema['oneOf'] ?? false)
×
UNCOV
132
            || \array_key_exists('type', $propertyMetadata->getOpenapiContext() ?? [])
×
133
        ) {
UNCOV
134
            return $propertySchema;
×
135
        }
136

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

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

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

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

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

UNCOV
174
            return $schema;
×
175
        }
176

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

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

UNCOV
184
                return $schema;
×
185
            }
186

UNCOV
187
            return $schema;
×
188
        }
189

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

328
        // If it's a resource and links are not readable, represent as IRI string.
UNCOV
329
        if ($isResourceClass && true !== $readableLink) {
×
UNCOV
330
            return [
×
UNCOV
331
                'type' => 'string',
×
UNCOV
332
                'format' => 'iri-reference',
×
UNCOV
333
                'example' => 'https://example.com/', // Add a generic example
×
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

344
        if (!\array_key_exists('default', $propertySchema) && !empty($default = $propertyMetadata->getDefault()) && (!\count($types) || null === ($className = $types[0]->getClassName()) || !$this->isResourceClass($className))) {
×
345
            if ($default instanceof \BackedEnum) {
×
346
                $default = $default->value;
×
347
            }
348
            $propertySchema['default'] = $default;
×
349
        }
350

351
        if (!\array_key_exists('example', $propertySchema) && !empty($example = $propertyMetadata->getExample())) {
×
352
            $propertySchema['example'] = $example;
×
353
        }
354

355
        if (!\array_key_exists('example', $propertySchema) && \array_key_exists('default', $propertySchema)) {
×
356
            $propertySchema['example'] = $propertySchema['default'];
×
357
        }
358

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

368
        $valueSchema = [];
×
369
        foreach ($types as $type) {
×
370
            // Temp fix for https://github.com/symfony/symfony/pull/52699
371
            if (ArrayCollection::class === $type->getClassName()) {
×
372
                $type = new LegacyType($type->getBuiltinType(), $type->isNullable(), $type->getClassName(), true, $type->getCollectionKeyTypes(), $type->getCollectionValueTypes());
×
373
            }
374

375
            if ($isCollection = $type->isCollection()) {
×
376
                $keyType = $type->getCollectionKeyTypes()[0] ?? null;
×
377
                $valueType = $type->getCollectionValueTypes()[0] ?? null;
×
378
            } else {
379
                $keyType = null;
×
380
                $valueType = $type;
×
381
            }
382

383
            if (null === $valueType) {
×
384
                $builtinType = 'string';
×
385
                $className = null;
×
386
            } else {
387
                $builtinType = $valueType->getBuiltinType();
×
388
                $className = $valueType->getClassName();
×
389
            }
390

391
            if ($isCollection && null !== $propertyMetadata->getUriTemplate()) {
×
392
                $keyType = null;
×
393
                $isCollection = false;
×
394
            }
395

396
            $propertyType = $this->getLegacyType(new LegacyType($builtinType, $type->isNullable(), $className, $isCollection, $keyType, $valueType), $link);
×
397
            if (!\in_array($propertyType, $valueSchema, true)) {
×
398
                $valueSchema[] = $propertyType;
×
399
            }
400
        }
401

402
        if (1 === \count($valueSchema)) {
×
403
            return $propertySchema + $valueSchema[0];
×
404
        }
405

406
        // multiple builtInTypes detected: determine oneOf/allOf if union vs intersect types
407
        try {
408
            $reflectionClass = new \ReflectionClass($resourceClass);
×
409
            $reflectionProperty = $reflectionClass->getProperty($property);
×
410
            $composition = $reflectionProperty->getType() instanceof \ReflectionUnionType ? 'oneOf' : 'allOf';
×
411
        } catch (\ReflectionException) {
×
412
            // cannot detect types
413
            $composition = 'anyOf';
×
414
        }
415

416
        return $propertySchema + [$composition => $valueSchema];
×
417
    }
418

419
    private function getLegacyType(LegacyType $type, ?bool $readableLink = null): array
420
    {
421
        if (!$type->isCollection()) {
×
422
            return $this->addNullabilityToTypeDefinition($this->legacyTypeToArray($type, $readableLink), $type);
×
423
        }
424

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

428
        if (null !== $keyType && LegacyType::BUILTIN_TYPE_STRING === $keyType->getBuiltinType()) {
×
429
            return $this->addNullabilityToTypeDefinition([
×
430
                'type' => 'object',
×
431
                'additionalProperties' => $this->getLegacyType($subType, $readableLink),
×
432
            ], $type);
×
433
        }
434

435
        return $this->addNullabilityToTypeDefinition([
×
436
            'type' => 'array',
×
437
            'items' => $this->getLegacyType($subType, $readableLink),
×
438
        ], $type);
×
439
    }
440

441
    private function legacyTypeToArray(LegacyType $type, ?bool $readableLink = null): array
442
    {
443
        return match ($type->getBuiltinType()) {
×
444
            LegacyType::BUILTIN_TYPE_INT => ['type' => 'integer'],
×
445
            LegacyType::BUILTIN_TYPE_FLOAT => ['type' => 'number'],
×
446
            LegacyType::BUILTIN_TYPE_BOOL => ['type' => 'boolean'],
×
447
            LegacyType::BUILTIN_TYPE_OBJECT => $this->getLegacyClassType($type->getClassName(), $type->isNullable(), $readableLink),
×
448
            default => ['type' => 'string'],
×
449
        };
×
450
    }
451

452
    /**
453
     * 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.
454
     *
455
     * Note: if the class is not part of exceptions listed above, any class is considered as a resource.
456
     *
457
     * @throws PropertyNotFoundException
458
     */
459
    private function getLegacyClassType(?string $className, bool $nullable, ?bool $readableLink): array
460
    {
461
        if (null === $className) {
×
462
            return ['type' => 'string'];
×
463
        }
464

465
        if (is_a($className, \DateTimeInterface::class, true)) {
×
466
            return [
×
467
                'type' => 'string',
×
468
                'format' => 'date-time',
×
469
            ];
×
470
        }
471

472
        if (is_a($className, \DateInterval::class, true)) {
×
473
            return [
×
474
                'type' => 'string',
×
475
                'format' => 'duration',
×
476
            ];
×
477
        }
478

479
        if (is_a($className, UuidInterface::class, true) || is_a($className, Uuid::class, true)) {
×
480
            return [
×
481
                'type' => 'string',
×
482
                'format' => 'uuid',
×
483
            ];
×
484
        }
485

486
        if (is_a($className, Ulid::class, true)) {
×
487
            return [
×
488
                'type' => 'string',
×
489
                'format' => 'ulid',
×
490
            ];
×
491
        }
492

493
        if (is_a($className, \SplFileInfo::class, true)) {
×
494
            return [
×
495
                'type' => 'string',
×
496
                'format' => 'binary',
×
497
            ];
×
498
        }
499

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

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

506
            if ($nullable) {
×
507
                $enumCases[] = null;
×
508
            }
509

510
            return [
×
511
                'type' => $type,
×
512
                'enum' => $enumCases,
×
513
            ];
×
514
        }
515

516
        if (true !== $readableLink && $isResourceClass) {
×
517
            return [
×
518
                'type' => 'string',
×
519
                'format' => 'iri-reference',
×
520
                'example' => 'https://example.com/',
×
521
            ];
×
522
        }
523

524
        return ['type' => Schema::UNKNOWN_TYPE];
×
525
    }
526

527
    /**
528
     * @param array<string, mixed> $jsonSchema
529
     *
530
     * @return array<string, mixed>
531
     */
532
    private function addNullabilityToTypeDefinition(array $jsonSchema, LegacyType $type): array
533
    {
534
        if (!$type->isNullable()) {
×
535
            return $jsonSchema;
×
536
        }
537

538
        if (\array_key_exists('$ref', $jsonSchema)) {
×
539
            return ['anyOf' => [$jsonSchema, ['type' => 'null']]];
×
540
        }
541

542
        return [...$jsonSchema, ...[
×
543
            'type' => \is_array($jsonSchema['type'])
×
544
                ? array_merge($jsonSchema['type'], ['null'])
×
545
                : [$jsonSchema['type'], 'null'],
×
546
        ]];
×
547
    }
548
}
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