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

api-platform / core / 18118486316

30 Sep 2025 04:11AM UTC coverage: 0.0% (-22.0%) from 21.956%
18118486316

Pull #7397

github

web-flow
Merge e92aeff57 into 55fd65795
Pull Request #7397: fix(jsonschema/jsonld): make `@id` and `@type` properties required only in the JSON-LD schema for output

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

12143 existing lines in 402 files now uncovered.

0 of 53916 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/Metadata/Property/Factory/AttributePropertyMetadataFactory.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\Property\Factory;
15

16
use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory;
17
use ApiPlatform\Metadata\ApiProperty;
18
use ApiPlatform\Metadata\Exception\PropertyNotFoundException;
19
use ApiPlatform\Metadata\Util\Reflection;
20
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
21
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
22

23
/**
24
 * Creates a property metadata from {@see ApiProperty} attribute.
25
 *
26
 * @author Antoine Bluchet <soyuka@gmail.com>
27
 */
28
final class AttributePropertyMetadataFactory implements PropertyMetadataFactoryInterface
29
{
30
    public function __construct(
31
        private readonly ?PropertyMetadataFactoryInterface $decorated = null,
32
        private readonly ?NameConverterInterface $nameConverter = null,
33
    ) {
UNCOV
34
    }
×
35

36
    /**
37
     * {@inheritdoc}
38
     */
39
    public function create(string $resourceClass, string $property, array $options = []): ApiProperty
40
    {
UNCOV
41
        $parentPropertyMetadata = null;
×
UNCOV
42
        if ($this->decorated) {
×
43
            try {
UNCOV
44
                $parentPropertyMetadata = $this->decorated->create($resourceClass, $property, $options);
×
45
            } catch (PropertyNotFoundException) {
×
46
                // Ignore not found exception from decorated factories
47
            }
48
        }
49

UNCOV
50
        $reflectionClass = null;
×
UNCOV
51
        $reflectionEnum = null;
×
52

53
        try {
UNCOV
54
            $reflectionClass = new \ReflectionClass($resourceClass);
×
55
        } catch (\ReflectionException) {
×
56
        }
57
        try {
UNCOV
58
            $reflectionEnum = new \ReflectionEnum($resourceClass);
×
UNCOV
59
        } catch (\ReflectionException) {
×
60
        }
61

UNCOV
62
        if (!$reflectionClass && !$reflectionEnum) {
×
63
            return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
×
64
        }
65

UNCOV
66
        if ($reflectionEnum && $reflectionEnum->hasCase($property)) {
×
UNCOV
67
            $reflectionCase = $reflectionEnum->getCase($property);
×
UNCOV
68
            if ($attributes = $reflectionCase->getAttributes(ApiProperty::class)) {
×
UNCOV
69
                return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
×
70
            }
71
        }
72

UNCOV
73
        if ($reflectionClass->hasProperty($property)) {
×
UNCOV
74
            $reflectionProperty = $reflectionClass->getProperty($property);
×
UNCOV
75
            if ($attributes = $reflectionProperty->getAttributes(ApiProperty::class)) {
×
UNCOV
76
                return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
×
77
            }
78
        }
79

UNCOV
80
        foreach (array_merge(Reflection::ACCESSOR_PREFIXES, Reflection::MUTATOR_PREFIXES) as $prefix) {
×
UNCOV
81
            $methodName = $prefix.ucfirst($property);
×
UNCOV
82
            if (!$reflectionClass->hasMethod($methodName) && !$reflectionEnum?->hasMethod($methodName)) {
×
UNCOV
83
                continue;
×
84
            }
85

UNCOV
86
            $reflectionMethod = $reflectionClass->hasMethod($methodName) ? $reflectionClass->getMethod($methodName) : $reflectionEnum?->getMethod($methodName);
×
UNCOV
87
            if (!$reflectionMethod->isPublic()) {
×
88
                continue;
×
89
            }
90

UNCOV
91
            if ($attributes = $reflectionMethod->getAttributes(ApiProperty::class)) {
×
UNCOV
92
                return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
×
93
            }
94
        }
95

UNCOV
96
        $attributes = $reflectionClass->getAttributes(ApiProperty::class);
×
UNCOV
97
        foreach ($attributes as $attribute) {
×
UNCOV
98
            $instance = $attribute->newInstance();
×
UNCOV
99
            if ($instance->getProperty() === ($this->nameConverter?->denormalize($property) ?? $property)) {
×
UNCOV
100
                return $this->createMetadata($instance, $parentPropertyMetadata);
×
101
            }
102
        }
103

UNCOV
104
        return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
×
105
    }
106

107
    /**
108
     * Returns the metadata from the decorated factory if available or throws an exception.
109
     *
110
     * @throws PropertyNotFoundException
111
     */
112
    private function handleNotFound(?ApiProperty $parentPropertyMetadata, string $resourceClass, string $property): ApiProperty
113
    {
UNCOV
114
        if (null !== $parentPropertyMetadata) {
×
UNCOV
115
            return $parentPropertyMetadata;
×
116
        }
117

118
        throw new PropertyNotFoundException(\sprintf('Property "%s" of class "%s" not found.', $property, $resourceClass));
×
119
    }
120

121
    private function createMetadata(ApiProperty $attribute, ?ApiProperty $propertyMetadata = null): ApiProperty
122
    {
UNCOV
123
        if (null === $propertyMetadata) {
×
124
            return $this->handleUserDefinedSchema($attribute);
×
125
        }
126

UNCOV
127
        foreach (get_class_methods(ApiProperty::class) as $method) {
×
UNCOV
128
            if (preg_match('/^(?:get|is)(.*)/', (string) $method, $matches)) {
×
129
                // BC layer, to remove in 5.0
UNCOV
130
                if ('getBuiltinTypes' === $method) {
×
UNCOV
131
                    if (method_exists(PropertyInfoExtractor::class, 'getType')) {
×
UNCOV
132
                        continue;
×
133
                    }
134

135
                    if ($builtinTypes = $attribute->getBuiltinTypes()) {
×
136
                        $propertyMetadata = $propertyMetadata->withBuiltinTypes($builtinTypes);
×
137
                    }
138

139
                    continue;
×
140
                }
141

UNCOV
142
                if (null !== $val = $attribute->{$method}()) {
×
UNCOV
143
                    $propertyMetadata = $propertyMetadata->{"with{$matches[1]}"}($val);
×
144
                }
145
            }
146
        }
147

UNCOV
148
        return $this->handleUserDefinedSchema($propertyMetadata);
×
149
    }
150

151
    private function handleUserDefinedSchema(ApiProperty $propertyMetadata): ApiProperty
152
    {
153
        // can't know later if the schema has been defined by the user or by API Platform
154
        // store extra key to make this difference
UNCOV
155
        if (null !== $propertyMetadata->getSchema()) {
×
UNCOV
156
            $extraProperties = $propertyMetadata->getExtraProperties();
×
UNCOV
157
            $propertyMetadata = $propertyMetadata->withExtraProperties([SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true] + $extraProperties);
×
158
        }
159

UNCOV
160
        return $propertyMetadata;
×
161
    }
162
}
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