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

api-platform / core / 16091628082

05 Jul 2025 08:07PM UTC coverage: 21.769% (-0.4%) from 22.122%
16091628082

push

github

web-flow
chore: symfony deprecations (#7277)

0 of 8 new or added lines in 2 files covered. (0.0%)

186 existing lines in 13 files now uncovered.

11371 of 52234 relevant lines covered (21.77%)

11.51 hits per line

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

80.29
/src/Metadata/ApiProperty.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\Metadata;
15

16
use ApiPlatform\Metadata\Util\PropertyInfoToTypeInfoHelper;
17
use Symfony\Component\PropertyInfo\Type as LegacyType;
18
use Symfony\Component\Serializer\Attribute\Context;
19
use Symfony\Component\Serializer\Attribute\Groups;
20
use Symfony\Component\Serializer\Attribute\Ignore;
21
use Symfony\Component\Serializer\Attribute\MaxDepth;
22
use Symfony\Component\Serializer\Attribute\SerializedName;
23
use Symfony\Component\Serializer\Attribute\SerializedPath;
24
use Symfony\Component\TypeInfo\Type;
25

26
/**
27
 * ApiProperty annotation.
28
 *
29
 * @author Kévin Dunglas <dunglas@gmail.com>
30
 */
31
#[\Attribute(\Attribute::TARGET_PROPERTY | \Attribute::TARGET_METHOD | \Attribute::TARGET_PARAMETER | \Attribute::TARGET_CLASS_CONSTANT | \Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
32
final class ApiProperty
33
{
34
    private ?array $types;
35
    private ?array $serialize;
36
    private ?Type $nativeType;
37

38
    /**
39
     * @param bool|null                                                                                                                                   $readableLink            https://api-platform.com/docs/core/serialization/#force-iri-with-relations-of-the-same-type-parentchilds-relations
40
     * @param bool|null                                                                                                                                   $writableLink            https://api-platform.com/docs/core/serialization/#force-iri-with-relations-of-the-same-type-parentchilds-relations
41
     * @param bool|null                                                                                                                                   $required                https://api-platform.com/docs/admin/validation/#client-side-validation
42
     * @param bool|null                                                                                                                                   $identifier              https://api-platform.com/docs/core/identifiers/
43
     * @param mixed                                                                                                                                       $example                 https://api-platform.com/docs/core/openapi/#using-the-openapi-and-swagger-contexts
44
     * @param string|null                                                                                                                                 $deprecationReason       https://api-platform.com/docs/core/deprecations/#deprecating-resource-classes-operations-and-properties
45
     * @param bool|null                                                                                                                                   $fetchEager              https://api-platform.com/docs/core/performance/#eager-loading
46
     * @param array|null                                                                                                                                  $jsonldContext           https://api-platform.com/docs/core/extending-jsonld-context/#extending-json-ld-and-hydra-contexts
47
     * @param array|null                                                                                                                                  $openapiContext          https://api-platform.com/docs/core/openapi/#using-the-openapi-and-swagger-contexts
48
     * @param bool|null                                                                                                                                   $push                    https://api-platform.com/docs/core/push-relations/
49
     * @param string|\Stringable|null                                                                                                                     $security                https://api-platform.com/docs/core/security
50
     * @param string|\Stringable|null                                                                                                                     $securityPostDenormalize https://api-platform.com/docs/core/security/#executing-access-control-rules-after-denormalization
51
     * @param string[]|null                                                                                                                               $types                   the RDF types of this property
52
     * @param string[]|null                                                                                                                               $iris
53
     * @param LegacyType[]|null                                                                                                                           $builtinTypes
54
     * @param string|null                                                                                                                                 $uriTemplate             (experimental) whether to return the subRessource collection IRI instead of an iterable of IRI
55
     * @param string|null                                                                                                                                 $property                The property name
56
     * @param Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth|array<array-key, Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth> $serialize               Serializer attributes
57
     * @param Type|null                                                                                                                                   $nativeType              The internal PHP type
58
     */
59
    public function __construct(
60
        private ?string $description = null,
61
        private ?bool $readable = null,
62
        private ?bool $writable = null,
63
        private ?bool $readableLink = null,
64
        private ?bool $writableLink = null,
65
        private ?bool $required = null,
66
        private ?bool $identifier = null,
67
        private mixed $default = null,
68
        private mixed $example = null,
69
        /**
70
         * The `deprecationReason` option deprecates the current operation with a deprecation message.
71
         *
72
         * <div data-code-selector>
73
         *
74
         * ```php
75
         * <?php
76
         * // api/src/Entity/Review.php
77
         * use ApiPlatform\Metadata\ApiProperty;
78
         * use ApiPlatform\Metadata\ApiResource;
79
         *
80
         * #[ApiResource]
81
         * class Review
82
         * {
83
         *     #[ApiProperty(deprecationReason: "Use the rating property instead")]
84
         *     public string $letter;
85
         * }
86
         * ```
87
         *
88
         * ```yaml
89
         * # api/config/api_platform/properties.yaml
90
         * properties:
91
         *     App\Entity\Review:
92
         *         letter:
93
         *             deprecationReason: 'Create a Book instead'
94
         * ```
95
         *
96
         * ```xml
97
         * <?xml version="1.0" encoding="UTF-8" ?>
98
         * <!-- api/config/api_platform/properties.xml -->
99
         *
100
         * <properties
101
         *         xmlns="https://api-platform.com/schema/metadata/properties-3.0"
102
         *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
103
         *         xsi:schemaLocation="https://api-platform.com/schema/metadata/properties-3.0
104
         *         https://api-platform.com/schema/metadata/properties-3.0.xsd">
105
         *     <property resource="App\Entity\Review" name="letter" deprecationReason="Create a Book instead" />
106
         * </properties>
107
         * ```
108
         *
109
         * </div>
110
         *
111
         * - With JSON-lD / Hydra, [an `owl:deprecated` annotation property](https://www.w3.org/TR/owl2-syntax/#Annotation_Properties) will be added to the appropriate data structure
112
         * - With Swagger / OpenAPI, [a `deprecated` property](https://swagger.io/docs/specification/2-0/paths-and-operations/) will be added
113
         * - With GraphQL, the [`isDeprecated` and `deprecationReason` properties](https://facebook.github.io/graphql/June2018/#sec-Deprecation) will be added to the schema
114
         */
115
        private ?string $deprecationReason = null,
116
        private ?bool $fetchable = null,
117
        private ?bool $fetchEager = null,
118
        private ?array $jsonldContext = null,
119
        private ?array $openapiContext = null,
120
        private ?array $jsonSchemaContext = null,
121
        private ?bool $push = null,
122
        /**
123
         * The `security` option defines the access to the current property, on normalization process, based on Symfony Security.
124
         * It receives an `object` variable related to the current object, and a `property` variable related to the current property.
125
         *
126
         * <div data-code-selector>
127
         *
128
         * ```php
129
         * <?php
130
         * // api/src/Entity/Review.php
131
         * use ApiPlatform\Metadata\ApiProperty;
132
         * use ApiPlatform\Metadata\ApiResource;
133
         *
134
         * #[ApiResource]
135
         * class Review
136
         * {
137
         *     #[ApiProperty(security: 'is_granted("ROLE_ADMIN")')]
138
         *     public string $letter;
139
         * }
140
         * ```
141
         *
142
         * ```yaml
143
         * # api/config/api_platform/properties.yaml
144
         * properties:
145
         *     App\Entity\Review:
146
         *         letter:
147
         *             security: 'is_granted("ROLE_ADMIN")'
148
         * ```
149
         *
150
         * ```xml
151
         * <?xml version="1.0" encoding="UTF-8" ?>
152
         * <!-- api/config/api_platform/properties.xml -->
153
         *
154
         * <properties
155
         *         xmlns="https://api-platform.com/schema/metadata/properties-3.0"
156
         *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
157
         *         xsi:schemaLocation="https://api-platform.com/schema/metadata/properties-3.0
158
         *         https://api-platform.com/schema/metadata/properties-3.0.xsd">
159
         *     <property resource="App\Entity\Review" name="letter" security="is_granted('ROLE_ADMIN')" />
160
         * </properties>
161
         * ```
162
         *
163
         * </div>
164
         */
165
        private string|\Stringable|null $security = null,
166
        /**
167
         * The `securityPostDenormalize` option defines access to the current property after the denormalization process, based on Symfony Security.
168
         * It receives an `object` variable related to the current object, and a `property` variable related to the current property.
169
         *
170
         * <div data-code-selector>
171
         *
172
         * ```php
173
         * <?php
174
         * // api/src/Entity/Review.php
175
         * use ApiPlatform\Metadata\ApiProperty;
176
         * use ApiPlatform\Metadata\ApiResource;
177
         *
178
         * #[ApiResource]
179
         * class Review
180
         * {
181
         *     #[ApiProperty(securityPostDenormalize: 'is_granted("ROLE_ADMIN")')]
182
         *     public string $letter;
183
         * }
184
         * ```
185
         *
186
         * ```yaml
187
         * # api/config/api_platform/properties.yaml
188
         * properties:
189
         *     App\Entity\Review:
190
         *         letter:
191
         *             securityPostDenormalize: 'is_granted("ROLE_ADMIN")'
192
         * ```
193
         *
194
         * ```xml
195
         * <?xml version="1.0" encoding="UTF-8" ?>
196
         * <!-- api/config/api_platform/properties.xml -->
197
         *
198
         * <properties
199
         *         xmlns="https://api-platform.com/schema/metadata/properties-3.0"
200
         *         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
201
         *         xsi:schemaLocation="https://api-platform.com/schema/metadata/properties-3.0
202
         *         https://api-platform.com/schema/metadata/properties-3.0.xsd">
203
         *     <property resource="App\Entity\Review" name="letter" securityPostDenormalize="is_granted('ROLE_ADMIN')" />
204
         * </properties>
205
         * ```
206
         *
207
         * </div>
208
         */
209
        private string|\Stringable|null $securityPostDenormalize = null,
210
        array|string|null $types = null,
211
        /*
212
         * The related php types.
213
         *
214
         * deprecated since 4.2, use "nativeType" instead.
215
         */
216
        private ?array $builtinTypes = null,
217
        private ?array $schema = null,
218
        private ?bool $initializable = null,
219
        private $iris = null,
220
        private ?bool $genId = null,
221
        private ?string $uriTemplate = null,
222
        private ?string $property = null,
223
        private ?string $policy = null,
224
        array|Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth|null $serialize = null,
225
        /**
226
         * Whether to document this property as a hydra:supportedProperty.
227
         */
228
        private ?bool $hydra = null,
229
        ?Type $nativeType = null,
230
        private array $extraProperties = [],
231
    ) {
232
        $this->types = \is_string($types) ? (array) $types : $types;
108✔
233
        $this->serialize = (null === $serialize || \is_array($serialize)) ? $serialize : [$serialize];
108✔
234
        $this->nativeType = $nativeType;
108✔
235

236
        if ($this->builtinTypes) {
108✔
237
            trigger_deprecation('api_platform/metadata', '4.2', \sprintf('The "builtinTypes" argument of "%s" is deprecated, use "nativeType" instead.', __CLASS__));
×
238
            $this->nativeType ??= PropertyInfoToTypeInfoHelper::convertLegacyTypesToType($this->builtinTypes);
×
239
        } elseif ($this->nativeType) {
108✔
240
            $this->builtinTypes = PropertyInfoToTypeInfoHelper::convertTypeToLegacyTypes($this->nativeType) ?? [];
×
241
        }
242
    }
243

244
    public function getProperty(): ?string
245
    {
246
        return $this->property;
32✔
247
    }
248

249
    public function withProperty(string $property): static
250
    {
251
        $self = clone $this;
2✔
252
        $self->property = $property;
2✔
253

254
        return $self;
2✔
255
    }
256

257
    public function getDescription(): ?string
258
    {
259
        return $this->description;
93✔
260
    }
261

262
    public function withDescription(string $description): static
263
    {
264
        $self = clone $this;
40✔
265
        $self->description = $description;
40✔
266

267
        return $self;
40✔
268
    }
269

270
    public function isReadable(): ?bool
271
    {
272
        return $this->readable;
284✔
273
    }
274

275
    public function withReadable(bool $readable): static
276
    {
277
        $self = clone $this;
102✔
278
        $self->readable = $readable;
102✔
279

280
        return $self;
102✔
281
    }
282

283
    public function isWritable(): ?bool
284
    {
285
        return $this->writable;
107✔
286
    }
287

288
    public function withWritable(bool $writable): static
289
    {
290
        $self = clone $this;
100✔
291
        $self->writable = $writable;
100✔
292

293
        return $self;
100✔
294
    }
295

296
    public function isReadableLink(): ?bool
297
    {
298
        return $this->readableLink;
100✔
299
    }
300

301
    public function withReadableLink(bool $readableLink): static
302
    {
303
        $self = clone $this;
43✔
304
        $self->readableLink = $readableLink;
43✔
305

306
        return $self;
43✔
307
    }
308

309
    public function isWritableLink(): ?bool
310
    {
311
        return $this->writableLink;
44✔
312
    }
313

314
    public function withWritableLink(bool $writableLink): static
315
    {
316
        $self = clone $this;
41✔
317
        $self->writableLink = $writableLink;
41✔
318

319
        return $self;
41✔
320
    }
321

322
    public function isRequired(): ?bool
323
    {
324
        return $this->required;
115✔
325
    }
326

327
    public function withRequired(bool $required): static
328
    {
329
        $self = clone $this;
87✔
330
        $self->required = $required;
87✔
331

332
        return $self;
87✔
333
    }
334

335
    public function isIdentifier(): ?bool
336
    {
337
        return $this->identifier;
98✔
338
    }
339

340
    public function withIdentifier(bool $identifier): static
341
    {
342
        $self = clone $this;
72✔
343
        $self->identifier = $identifier;
72✔
344

345
        return $self;
72✔
346
    }
347

348
    public function getDefault(): mixed
349
    {
350
        return $this->default;
87✔
351
    }
352

353
    public function withDefault(mixed $default): static
354
    {
355
        $self = clone $this;
60✔
356
        $self->default = $default;
60✔
357

358
        return $self;
60✔
359
    }
360

361
    public function getExample(): mixed
362
    {
363
        return $this->example;
87✔
364
    }
365

366
    public function withExample(mixed $example): static
367
    {
368
        $self = clone $this;
1✔
369
        $self->example = $example;
1✔
370

371
        return $self;
1✔
372
    }
373

374
    public function getDeprecationReason(): ?string
375
    {
376
        return $this->deprecationReason;
93✔
377
    }
378

379
    public function withDeprecationReason($deprecationReason): static
380
    {
381
        $self = clone $this;
4✔
382
        $self->deprecationReason = $deprecationReason;
4✔
383

384
        return $self;
4✔
385
    }
386

387
    public function isFetchable(): ?bool
388
    {
389
        return $this->fetchable;
32✔
390
    }
391

392
    public function withFetchable($fetchable): static
393
    {
394
        $self = clone $this;
×
395
        $self->fetchable = $fetchable;
×
396

397
        return $self;
×
398
    }
399

400
    public function getFetchEager(): ?bool
401
    {
402
        return $this->fetchEager;
38✔
403
    }
404

405
    public function withFetchEager($fetchEager): static
406
    {
407
        $self = clone $this;
×
408
        $self->fetchEager = $fetchEager;
×
409

410
        return $self;
×
411
    }
412

413
    public function getJsonldContext(): ?array
414
    {
415
        return $this->jsonldContext;
47✔
416
    }
417

418
    public function withJsonldContext($jsonldContext): static
419
    {
420
        $self = clone $this;
8✔
421
        $self->jsonldContext = $jsonldContext;
8✔
422

423
        return $self;
8✔
424
    }
425

426
    public function getOpenapiContext(): ?array
427
    {
428
        return $this->openapiContext;
94✔
429
    }
430

431
    public function withOpenapiContext($openapiContext): static
432
    {
433
        $self = clone $this;
2✔
434
        $self->openapiContext = $openapiContext;
2✔
435

436
        return $self;
2✔
437
    }
438

439
    public function getJsonSchemaContext(): ?array
440
    {
441
        return $this->jsonSchemaContext;
106✔
442
    }
443

444
    public function withJsonSchemaContext($jsonSchemaContext): static
445
    {
446
        $self = clone $this;
7✔
447
        $self->jsonSchemaContext = $jsonSchemaContext;
7✔
448

449
        return $self;
7✔
450
    }
451

452
    public function getPush(): ?bool
453
    {
454
        return $this->push;
36✔
455
    }
456

457
    public function withPush($push): static
458
    {
459
        $self = clone $this;
5✔
460
        $self->push = $push;
5✔
461

462
        return $self;
5✔
463
    }
464

465
    public function getSecurity(): ?string
466
    {
467
        return $this->security instanceof \Stringable ? (string) $this->security : $this->security;
255✔
468
    }
469

470
    public function withSecurity($security): static
471
    {
472
        $self = clone $this;
2✔
473
        $self->security = $security;
2✔
474

475
        return $self;
2✔
476
    }
477

478
    public function getSecurityPostDenormalize(): ?string
479
    {
480
        return $this->securityPostDenormalize instanceof \Stringable ? (string) $this->securityPostDenormalize : $this->securityPostDenormalize;
39✔
481
    }
482

483
    public function withSecurityPostDenormalize($securityPostDenormalize): static
484
    {
485
        $self = clone $this;
2✔
486
        $self->securityPostDenormalize = $securityPostDenormalize;
2✔
487

488
        return $self;
2✔
489
    }
490

491
    public function getTypes(): ?array
492
    {
493
        return $this->types;
87✔
494
    }
495

496
    /**
497
     * @param string[]|string $types
498
     */
499
    public function withTypes(array|string $types = []): static
500
    {
501
        $self = clone $this;
4✔
502
        $self->types = (array) $types;
4✔
503

504
        return $self;
4✔
505
    }
506

507
    /**
508
     * deprecated since 4.2, use "getNativeType" instead.
509
     *
510
     * @return LegacyType[]|null
511
     */
512
    public function getBuiltinTypes(): ?array
513
    {
514
        trigger_deprecation('api-platform/metadata', '4.2', 'The "%s()" method is deprecated, use "%s::getNativeType()" instead.', __METHOD__, self::class);
×
515

NEW
516
        if (null === $this->builtinTypes && null !== $this->nativeType) {
×
NEW
517
            $this->builtinTypes = PropertyInfoToTypeInfoHelper::convertTypeToLegacyTypes($this->nativeType) ?? [];
×
518
        }
519

UNCOV
520
        return $this->builtinTypes;
×
521
    }
522

523
    /**
524
     * deprecated since 4.2, use "withNativeType" instead.
525
     *
526
     * @param LegacyType[] $builtinTypes
527
     */
528
    public function withBuiltinTypes(array $builtinTypes = []): static
529
    {
530
        trigger_deprecation('api-platform/metadata', '4.2', 'The "%s()" method is deprecated, use "%s::withNativeType()" instead.', __METHOD__, self::class);
×
531

532
        $self = clone $this;
×
533
        $self->builtinTypes = $builtinTypes;
×
534
        $self->nativeType = PropertyInfoToTypeInfoHelper::convertLegacyTypesToType($builtinTypes);
×
535

536
        return $self;
×
537
    }
538

539
    public function getNativeType(): ?Type
540
    {
541
        return $this->nativeType;
285✔
542
    }
543

544
    public function withNativeType(?Type $nativeType): self
545
    {
546
        $self = clone $this;
101✔
547
        $self->nativeType = $nativeType;
101✔
548

549
        return $self;
101✔
550
    }
551

552
    public function getSchema(): ?array
553
    {
554
        return $this->schema;
113✔
555
    }
556

557
    public function withSchema(array $schema = []): static
558
    {
559
        $self = clone $this;
87✔
560
        $self->schema = $schema;
87✔
561

562
        return $self;
87✔
563
    }
564

565
    public function withInitializable(?bool $initializable): static
566
    {
567
        $self = clone $this;
87✔
568
        $self->initializable = $initializable;
87✔
569

570
        return $self;
87✔
571
    }
572

573
    public function isInitializable(): ?bool
574
    {
575
        return $this->initializable;
94✔
576
    }
577

578
    public function getExtraProperties(): array
579
    {
580
        return $this->extraProperties;
117✔
581
    }
582

583
    public function withExtraProperties(array $extraProperties = []): static
584
    {
585
        $self = clone $this;
32✔
586
        $self->extraProperties = $extraProperties;
32✔
587

588
        return $self;
32✔
589
    }
590

591
    /**
592
     * Gets IRI of this property.
593
     *
594
     * @return string[]|null
595
     */
596
    public function getIris()
597
    {
598
        return $this->iris;
47✔
599
    }
600

601
    /**
602
     * Returns a new instance with the given IRI.
603
     *
604
     * @param string|string[] $iris
605
     */
606
    public function withIris(string|array $iris): static
607
    {
608
        $metadata = clone $this;
10✔
609
        $metadata->iris = (array) $iris;
10✔
610

611
        return $metadata;
10✔
612
    }
613

614
    /**
615
     * Whether to generate a skolem iri on anonymous resources.
616
     */
617
    public function getGenId(): ?bool
618
    {
619
        return $this->genId;
145✔
620
    }
621

622
    public function withGenId(bool $genId): static
623
    {
624
        $metadata = clone $this;
4✔
625
        $metadata->genId = $genId;
4✔
626

627
        return $metadata;
4✔
628
    }
629

630
    /**
631
     * Whether to return the subRessource collection IRI instead of an iterable of IRI.
632
     *
633
     * @experimental
634
     */
635
    public function getUriTemplate(): ?string
636
    {
637
        return $this->uriTemplate;
104✔
638
    }
639

640
    public function withUriTemplate(?string $uriTemplate): static
641
    {
642
        $metadata = clone $this;
×
643
        $metadata->uriTemplate = $uriTemplate;
×
644

645
        return $metadata;
×
646
    }
647

648
    public function getPolicy(): ?string
649
    {
650
        return $this->policy;
253✔
651
    }
652

653
    public function withPolicy(?string $policy): static
654
    {
655
        $self = clone $this;
×
656
        $self->policy = $policy;
×
657

658
        return $self;
×
659
    }
660

661
    public function getSerialize(): ?array
662
    {
663
        return $this->serialize;
32✔
664
    }
665

666
    /**
667
     * @param Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth|array<array-key, Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth> $serialize
668
     */
669
    public function withSerialize(array|Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth $serialize): static
670
    {
671
        $self = clone $this;
×
672
        $self->serialize = (array) $serialize;
×
673

674
        return $self;
×
675
    }
676

677
    public function getHydra(): ?bool
678
    {
679
        return $this->hydra;
32✔
680
    }
681

682
    public function withHydra(bool $hydra): static
683
    {
684
        $self = clone $this;
2✔
685
        $self->hydra = $hydra;
2✔
686

687
        return $self;
2✔
688
    }
689
}
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