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

api-platform / core / 14635100171

24 Apr 2025 06:39AM UTC coverage: 8.271% (+0.02%) from 8.252%
14635100171

Pull #6904

github

web-flow
Merge c9cefd82e into a3e5e53ea
Pull Request #6904: feat(graphql): added support for graphql subscriptions to work for actions

0 of 73 new or added lines in 3 files covered. (0.0%)

1999 existing lines in 144 files now uncovered.

13129 of 158728 relevant lines covered (8.27%)

13.6 hits per line

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

91.18
/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[]                                                                                                                                    $types                   the RDF types of this property
52
     * @param string[]                                                                                                                                    $iris
53
     * @param LegacyType[]                                                                                                                                $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                                                                                                                                        $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;
414✔
233
        $this->serialize = \is_array($serialize) ? $serialize : [$serialize];
414✔
234
        $this->nativeType = $nativeType;
414✔
235

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

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

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

254
        return $self;
7✔
255
    }
256

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

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

267
        return $self;
147✔
268
    }
269

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

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

280
        return $self;
350✔
281
    }
282

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

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

293
        return $self;
344✔
294
    }
295

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

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

306
        return $self;
127✔
307
    }
308

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

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

319
        return $self;
125✔
320
    }
321

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

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

332
        return $self;
398✔
333
    }
334

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

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

345
        return $self;
259✔
346
    }
347

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

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

358
        return $self;
136✔
359
    }
360

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

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

371
        return $self;
5✔
372
    }
373

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

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

384
        return $self;
14✔
385
    }
386

387
    public function isFetchable(): ?bool
388
    {
389
        return $this->fetchable;
93✔
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;
103✔
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;
156✔
416
    }
417

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

423
        return $self;
22✔
424
    }
425

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

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

436
        return $self;
7✔
437
    }
438

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

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

449
        return $self;
13✔
450
    }
451

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

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

462
        return $self;
7✔
463
    }
464

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

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

475
        return $self;
10✔
476
    }
477

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

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

488
        return $self;
10✔
489
    }
490

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

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

504
        return $self;
47✔
505
    }
506

507
    /**
508
     * deprecated since 4.2, use "getNativeType" instead.
509
     *
510
     * @return LegacyType[]
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);
1,164✔
515

516
        return $this->builtinTypes;
1,164✔
517
    }
518

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

528
        $self = clone $this;
50✔
529
        $self->builtinTypes = $builtinTypes;
50✔
530
        $self->nativeType = PropertyInfoToTypeInfoHelper::convertLegacyTypesToType($builtinTypes);
50✔
531

532
        return $self;
50✔
533
    }
534

535
    public function getNativeType(): ?Type
536
    {
537
        return $this->nativeType;
671✔
538
    }
539

540
    public function withNativeType(?Type $nativeType): self
541
    {
542
        $self = clone $this;
312✔
543
        $self->nativeType = $nativeType;
312✔
544
        $self->builtinTypes = PropertyInfoToTypeInfoHelper::convertTypeToLegacyTypes($nativeType) ?? [];
312✔
545

546
        return $self;
312✔
547
    }
548

549
    public function getSchema(): ?array
550
    {
551
        return $this->schema;
471✔
552
    }
553

554
    public function withSchema(array $schema = []): static
555
    {
556
        $self = clone $this;
380✔
557
        $self->schema = $schema;
380✔
558

559
        return $self;
380✔
560
    }
561

562
    public function withInitializable(?bool $initializable): static
563
    {
564
        $self = clone $this;
304✔
565
        $self->initializable = $initializable;
304✔
566

567
        return $self;
304✔
568
    }
569

570
    public function isInitializable(): ?bool
571
    {
572
        return $this->initializable;
441✔
573
    }
574

575
    public function getExtraProperties(): ?array
576
    {
577
        return $this->extraProperties;
531✔
578
    }
579

580
    public function withExtraProperties(array $extraProperties = []): static
581
    {
582
        $self = clone $this;
103✔
583
        $self->extraProperties = $extraProperties;
103✔
584

585
        return $self;
103✔
586
    }
587

588
    /**
589
     * Gets IRI of this property.
590
     */
591
    public function getIris()
592
    {
593
        return $this->iris;
156✔
594
    }
595

596
    /**
597
     * Returns a new instance with the given IRI.
598
     *
599
     * @param string|string[] $iris
600
     */
601
    public function withIris(string|array $iris): static
602
    {
603
        $metadata = clone $this;
20✔
604
        $metadata->iris = (array) $iris;
20✔
605

606
        return $metadata;
20✔
607
    }
608

609
    /**
610
     * Whether to generate a skolem iri on anonymous resources.
611
     */
612
    public function getGenId()
613
    {
614
        return $this->genId;
515✔
615
    }
616

617
    public function withGenId(bool $genId): static
618
    {
619
        $metadata = clone $this;
8✔
620
        $metadata->genId = $genId;
8✔
621

622
        return $metadata;
8✔
623
    }
624

625
    /**
626
     * Whether to return the subRessource collection IRI instead of an iterable of IRI.
627
     *
628
     * @experimental
629
     */
630
    public function getUriTemplate(): ?string
631
    {
632
        return $this->uriTemplate;
519✔
633
    }
634

635
    public function withUriTemplate(?string $uriTemplate): static
636
    {
UNCOV
637
        $metadata = clone $this;
5✔
UNCOV
638
        $metadata->uriTemplate = $uriTemplate;
5✔
639

UNCOV
640
        return $metadata;
5✔
641
    }
642

643
    public function getPolicy(): ?string
644
    {
645
        return $this->policy;
997✔
646
    }
647

648
    public function withPolicy(?string $policy): static
649
    {
650
        $self = clone $this;
×
651
        $self->policy = $policy;
×
652

653
        return $self;
×
654
    }
655

656
    public function getSerialize(): ?array
657
    {
658
        return $this->serialize;
93✔
659
    }
660

661
    /**
662
     * @param Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth|array<array-key, Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth> $serialize
663
     */
664
    public function withSerialize(array|Context|Groups|Ignore|SerializedName|SerializedPath|MaxDepth $serialize): static
665
    {
666
        $self = clone $this;
93✔
667
        $self->serialize = (array) $serialize;
93✔
668

669
        return $self;
93✔
670
    }
671

672
    public function getHydra(): ?bool
673
    {
674
        return $this->hydra;
96✔
675
    }
676

677
    public function withHydra(bool $hydra): static
678
    {
679
        $self = clone $this;
7✔
680
        $self->hydra = $hydra;
7✔
681

682
        return $self;
7✔
683
    }
684
}
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