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

api-platform / core / 6353211137

29 Sep 2023 02:43PM UTC coverage: 36.978% (+0.1%) from 36.863%
6353211137

push

github

web-flow
fix(jsonschema): do not override nor complete ApiProperty::schema user value (#5855)

* fix(jsonschema): do not override nor complete ApiProperty::schema user value

* chore(metadata): improve ExtractorPropertyMetadataFactory

26 of 26 new or added lines in 7 files covered. (100.0%)

10109 of 27338 relevant lines covered (36.98%)

20.02 hits per line

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

80.43
/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

21
/**
22
 * Creates a property metadata from {@see ApiProperty} attribute.
23
 *
24
 * @author Antoine Bluchet <soyuka@gmail.com>
25
 */
26
final class AttributePropertyMetadataFactory implements PropertyMetadataFactoryInterface
27
{
28
    public function __construct(private readonly ?PropertyMetadataFactoryInterface $decorated = null)
29
    {
30
    }
108✔
31

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

46
        $reflectionClass = null;
30✔
47
        $reflectionEnum = null;
30✔
48

49
        try {
50
            $reflectionClass = new \ReflectionClass($resourceClass);
30✔
51
        } catch (\ReflectionException) {
×
52
        }
53
        try {
54
            $reflectionEnum = new \ReflectionEnum($resourceClass);
30✔
55
        } catch (\ReflectionException) {
30✔
56
        }
57

58
        if (!$reflectionClass && !$reflectionEnum) {
30✔
59
            return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
×
60
        }
61

62
        if ($reflectionEnum) {
30✔
63
            if ($reflectionEnum->hasCase($property)) {
3✔
64
                $reflectionCase = $reflectionEnum->getCase($property);
×
65
                if ($attributes = $reflectionCase->getAttributes(ApiProperty::class)) {
×
66
                    return $this->createMetadata($attributes[0]->newInstance(), $parentPropertyMetadata);
×
67
                }
68
            }
69

70
            return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
3✔
71
        }
72

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

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

86
            $reflectionMethod = $reflectionClass->getMethod($methodName);
24✔
87
            if (!$reflectionMethod->isPublic()) {
24✔
88
                continue;
×
89
            }
90

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

96
        return $this->handleNotFound($parentPropertyMetadata, $resourceClass, $property);
30✔
97
    }
98

99
    /**
100
     * Returns the metadata from the decorated factory if available or throws an exception.
101
     *
102
     * @throws PropertyNotFoundException
103
     */
104
    private function handleNotFound(?ApiProperty $parentPropertyMetadata, string $resourceClass, string $property): ApiProperty
105
    {
106
        if (null !== $parentPropertyMetadata) {
30✔
107
            return $parentPropertyMetadata;
30✔
108
        }
109

110
        throw new PropertyNotFoundException(sprintf('Property "%s" of class "%s" not found.', $property, $resourceClass));
×
111
    }
112

113
    private function createMetadata(ApiProperty $attribute, ApiProperty $propertyMetadata = null): ApiProperty
114
    {
115
        if (null === $propertyMetadata) {
12✔
116
            return $this->handleUserDefinedSchema($attribute);
×
117
        }
118

119
        foreach (get_class_methods(ApiProperty::class) as $method) {
12✔
120
            if (preg_match('/^(?:get|is)(.*)/', (string) $method, $matches) && null !== $val = $attribute->{$method}()) {
12✔
121
                $propertyMetadata = $propertyMetadata->{"with{$matches[1]}"}($val);
12✔
122
            }
123
        }
124

125
        return $this->handleUserDefinedSchema($propertyMetadata);
12✔
126
    }
127

128
    private function handleUserDefinedSchema(ApiProperty $propertyMetadata): ApiProperty
129
    {
130
        // can't know later if the schema has been defined by the user or by API Platform
131
        // store extra key to make this difference
132
        if (null !== $propertyMetadata->getSchema()) {
12✔
133
            $extraProperties = $propertyMetadata->getExtraProperties() ?? [];
6✔
134
            $propertyMetadata = $propertyMetadata->withExtraProperties([SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED => true] + $extraProperties);
6✔
135
        }
136

137
        return $propertyMetadata;
12✔
138
    }
139
}
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